0

I'm putting together a shiny app and trying something along the lines of the following:

require("shiny")

ui <- fluidPage(
  fluidRow(fileInput(inputId = "dataFile", label = NULL)),
  fluidRow(wellPanel(tableOutput(outputId = "rawText")))
)

server <- function(input, output) {
  observe({
    upFile <- input$dataFile

    if(!is.null(upFile)) {
      raw.dat <<- reactive({
        read.csv(file = upFile$datapath, header = TRUE, stringsAsFactors = FALSE)
      })
    } else raw.dat <<- reactive({})
  })

  output$rawFile <- renderTable(as.data.frame(raw.dat()))
}

shinyApp(ui = ui, server = server)

and yet even after I upload a file, raw.dat() always, always returns NULL.

What am I missing?

twieg
  • 53
  • 2
  • 9
  • 1
    Why are you using `<<-` with assignment here? Where are you trying to assign this value? Why isn't it just a reactive value in the server block itself? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that we can actually run to test and verify possible solutions. Important parts of the code are missing here. – MrFlick Jun 11 '18 at 18:44
  • possibly unrelated, but I've experienced issues with uploading files while viewing the Shiny app in RStudio. Opening the app in a web browser resolved the issue. – Adrian Jun 11 '18 at 18:48
  • @MrFlick Sorry, I thought this was reproducible enough; I'll put together a filler app with just this code. I'm using the `<<-` so that the raw.dat reactive would be assigned in the environment outside the observer, or is that unnecessary? @Adrian FWIW, I've been using Firefox to test. – twieg Jun 11 '18 at 19:34

2 Answers2

0

Just make your raw.dat object reactive. Because it's dependent on input$dataFile it will do the observing for you.

raw.dat <- reactive({
  upFile <- input$dataFile
  if (is.null(inFile)) return(NULL)
  read.csv(upFile$datapath, header = TRUE, stringsAsFactors = FALSE)
})
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I figured out the problem... I hadn't upped the max file size option (`options(shiny.maxRequestSize = fileSizeInMB*1024^2)`) and the files I was testing with were bigger than the 5MB default. Oops, that's embarassing. I actually previously had something remarkably similar to what you put (but was trying the `observe` in a vain attempt to fix it), so I'll accept your answer--thanks for the help! Your method is definitely more elegant than the one in the Question. – twieg Jun 11 '18 at 19:56
0

Your uploaded file is likely larger than the default size allowed, change the acceptable value using:

options(shiny.maxRequestSize = MB*1024^2) #Change MB to suit your needs
Victor Burnett
  • 588
  • 6
  • 10