Why won't renderDT() return a table on my Shiny app?

Viewed 105

I'm trying to create a function on my Shiny app which allows someone to upload a file. The uploaded file will always contain a column called "Index". I want the Shiny app to be able to take this Index (e.g. A0AVT1_Y44) and extract information to add three additional columns to the table: Protein_id (A0AVT1), Residue (Y), and Position (44). I've already created a successful function which does this. However, when I run this code and then renderDT(), no data table shows up even though there is no error message. Does anyone know what's happening?

Code:

library(dplyr)
library(viridis)
library(tidyverse)
library(rio)
library(shiny)

options(shiny.maxRequestSize = 30*1024^2)

ui <- fluidPage(
  titlePanel("File Upload"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Browse...",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/tab-separated-values",
                           ".csv",
                           "text/tsv",
                           "text/comma-separated-values",
                           ".tsv")),
                placeholder = "No file selected",
      tags$hr(),
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")
    ),
    mainPanel(
      DT::dataTableOutput(outputId = "exp_df")
    )
  )
)

server <- function(input, output) {
  exp_df <- reactive({return(
    cbind(
      iris[input$file1],
      Residue = nv(),
      Position = nvar(),
      Protein_id = newvar())
  )
  })

  return(exp_df)

  nv <- reactive({
    exp_df[[gsub('[0-9]*','', sub(".*?_(.*?)$", "\\1", 

input$file1$Index))]] })

  nvar <- reactive({
    exp_df[[gsub('[0-9.-]','', sub(".*?_(.*?)$","\\1", 

input$file1$Index))]] })

  newvar <-reactive({
    exp_df[[sub("_\\D(\\d)+$","",sub(".*?_(.*?)$","\\1", 

input$file1$Index))]] })

  output$exp_df <- DT::renderDT({
    exp_df()
    })
}

shinyApp(ui=ui, server=server)

I then ran options(shiny.trace = TRUE) and received the following output:

runApp('~/BioInf Proj')

Listening on http://127.0.0.1:4700
SEND {"config":{"workerId":"","sessionId":"2a9472a1d0a0da6562eac28841de4ff2","user":null}}
RECV {"method":"init","data":{"file1:shiny.file":null,"disp":"head",".clientdata_output_exp_df_hidden":false,".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"4700",".clientdata_url_pathname":"/",".clientdata_url_search":"",".clientdata_url_hash_initial":"",".clientdata_url_hash":"",".clientdata_singletons":""}}
SEND {"busy":"busy"}
SEND {"recalculating":{"name":"exp_df","status":"recalculating"}}
SEND {"recalculating":{"name":"exp_df","status":"recalculated"}}
SEND {"busy":"idle"}
SEND {"errors":{"exp_df":{"message":"","call":"NULL","type":["shiny.silent.error","validation"]}},"values":{},"inputMessages":[]}
RECV {"method":"uploadInit","args":[[{"name":"pY_cml.csv","size":336901,"type":"application/vnd.ms-excel"}]],"tag":0}
SEND {"response":{"tag":0,"value":{"jobId":"3ad1ee2355d914f1b88e13e5","uploadUrl":"session/2a9472a1d0a0da6562eac28841de4ff2/upload/3ad1ee2355d914f1b88e13e5?w="}}}
RECV {"method":"uploadEnd","args":["3ad1ee2355d914f1b88e13e5","file1"],"tag":1}
SEND {"progress":{"type":"binding","message":{"id":"exp_df"}}}
SEND {"busy":"busy"}
SEND {"response":{"tag":1,"value":null}}
SEND {"recalculating":{"name":"exp_df","status":"recalculating"}}
SEND {"recalculating":{"name":"exp_df","status":"recalculated"}}
SEND {"busy":"idle"}
SEND {"errors":{"exp_df":{"message":"","call":"NULL","type":["shiny.silent.error","validation"]}},"values":{},"inputMessages":[]}
2 Answers

If you read the file first and then use just the Index column, it works fine. Try this

library(dplyr)
library(viridis)
library(tidyverse)
#library(rio)
library(shiny)

options(shiny.maxRequestSize = 30*1024^2)

ui <- fluidPage(
  titlePanel("File Upload"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Browse...",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/tab-separated-values",
                           ".csv",
                           "text/tsv",
                           "text/comma-separated-values",
                           ".tsv")),
      placeholder = "No file selected",
      tags$hr(),
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")
    ),
    mainPanel(
      DT::dataTableOutput(outputId = "exp_df")
    )
  )
)

server <- function(input, output) {
  mydf <- eventReactive(input$file1,{
        infile <- input$file1
        req(infile)
        return(read.csv(infile$datapath, header=TRUE, sep=','))
  })
  
  exp_dff <- reactive({
    req(mydf(),newvar(),nv(),nvar())
    return(
    cbind(
      mydf(),
      Residue = nv(),
      Position = nvar(),
      Protein_id = newvar())
  )
  })
  
  nv <- reactive({
    req(mydf())
    eI <- mydf()$Index
    gsub('[0-9]*','', sub(".*?_(.*?)$", "\\1",eI))
    #exp_df[[gsub('[0-9]*','', sub(".*?_(.*?)$", "\\1",input$file1$Index))]] 
  })
  
  nvar <- reactive({
    req(mydf())
    eI <- mydf()$Index
    gsub('[A-Z.-]','', sub(".*?_(.*?)$","\\1", eI))
    #exp_df[[gsub('[0-9.-]','', sub(".*?_(.*?)$","\\1", input$file1$Index))]] 
  })
  
  newvar <-reactive({
    req(mydf())
    eI <- mydf()$Index
    sub("(.*?)_(.*?)$","\\1",eI)
    #exp_df[[sub("_\\D(\\d)+$","",sub(".*?_(.*?)$","\\1",input$file1$Index))]] 
  })
  
  output$exp_df <- DT::renderDT({
    exp_dff()
  })
}

shinyApp(ui=ui, server=server)

It turns out the issue was with the file I was inputting, not the code! Thanks for the help!

Related