thomasp85 / shinyFiles

A shiny extension for server side file access
196 stars 47 forks source link

shinyFileChoose crashes on loading .RData file in locally ran application #147

Closed pakom closed 4 years ago

pakom commented 4 years ago

I am having a trouble loading an .RData file using a shinyFileChoose button. The app I am building is supposed to run on a local machine using files stored on the local machine. Here is sample code of the app:

library(shiny)
library(shinyFiles)

ui <- fluidPage(
  shinyFilesButton("getFile", "Choose file", title = "Select .RData file", multiple = FALSE),
  br(), br(),
  textOutput("columnNames")
)

server <- function(input,output,session){

  sys.volumes <- getVolumes()
  shinyFileChoose(input, "getFile", roots = sys.volumes, session = session)
  loaded.set <- reactiveValues(data = NULL)

  observeEvent(input$getFile, {
    loaded.set$data <- get(load(parseFilePaths(sys.volumes, input$getFile)$datapath))
  })

  output$columnNames <- renderText({
    colnames(loaded.set$data)
  })

}

shinyApp(ui = ui, server = server)

When I press the Choose file button, the app brings the file choose dialog and then immediately crashes with the following message in the RStudio console:

Warning: Error in gzfile: invalid 'description' argument

The app I am building uses other buttons (shinyDirChoose, shinyFileSave), but I never experienced such issues. I have tried this on both Windows and Linux. The comment posted under the accepted answer here touches on the issue with files stored locally and on server. Is this a bug or it is just not possible to use shinyFileChoose with locally stored files? Or I just miss something?

pakom commented 4 years ago

Oh, I think I figured it out: the observeEvent immediately looks for the file path when the button is pressed, but at this time it is not existing (character(0)). So if I add a condition to look for the length of the path, then everything is OK. So the full app will look like this:

library(shiny)
library(shinyFiles)

ui <- fluidPage(
  shinyFilesButton("getFile", "Choose file", title = "Select .RData file", multiple = FALSE),
  br(), br(),
  textOutput("columnNames")
)

server <- function(input,output,session){

  sys.volumes <- getVolumes()()
  shinyFileChoose(input, "getFile", roots = sys.volumes, session = session)
  loaded.set <- reactiveValues(data = NULL)

  observeEvent(input$getFile, {
    if(length(parseFilePaths(sys.volumes, input$getFile)$datapath) > 0) {
      loaded.set$data <- get(load(parseFilePaths(sys.volumes, input$getFile)$datapath))
    }
  })

  output$columnNames <- renderText({
    colnames(loaded.set$data)
  })

}

shinyApp(ui = ui, server = server)

Problem solved. I will leave it here for further reference if someone stumbles across the same issue.

vnijs commented 4 years ago

@pakom Also take a look at the code in the shinyFiles example.

https://github.com/thomasp85/shinyFiles/blob/master/inst/example/server.R#L30-L36