Rdata user upload in a Shiny app

Hi everyone,
I'm trying to use a Rdata file as a user input in my app, which contains multiple dataframes, which I know the names and organisation of. Thus, I have not used a new environment for loading, as I have seen elsewhere.
My problem is as follows : I can't seem to use validate to check if the app is happy with the file type.

library(shiny)

ui <- fluidPage(
    titlePanel("TestValidate"),
    actionButton("generate", "generate RData file"),
    fileInput("table", NULL),#, accept = ".rdata"),
    tableOutput("table")
)

server <- function(input, output, session) {
  mtcars_cop <- reactiveVal()
  
  observeEvent(input$generate, {
    mtcars_copy <- mtcars
    save(mtcars_copy, file = "mtcars.Rdata")
  })
  observeEvent(input$table,
          {
            req(input$table)
            extension <- tools::file_ext(input$table$name)
            switch(extension,
                   Rdata = load(input$table$datapath),
                   RData = load(input$table$datapath),
                   validate("Invalid file : Need a .RData file")
            ) 
            mtcars_cop(mtcars_copy)
          })
  
  output$table <- renderTable({
    req(mtcars_cop())
    mtcars_cop()
  })
}
shinyApp(ui = ui, server = server)

In this simple app, one can upload any file type without triggering the validate line. Moreover, the fileInput shows a misleading upload complete message. Is it because I replaced the reactive from the example in Mastering Shiny - Chapter 9 by an observeEvent ? I figured it would be okay, as I'm obviously more interested by the side effects.

PS : I did also try the accept parameter in fileInput, but it does not seem to work, a least on my computer. If I add accept = ".rdata", my file navigator (Nautilus) filters out every file, including Rdata one. This is also the case if I display the app in a browser. If someone reproduces this bug, I will post an issue on github.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.