Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make conditionalPanel depend on files uploaded with fileInput

Tags:

r

shiny

So I'm trying to make a shiny app where I have a button which only shows up if files have been uploaded; for this im using conditionalPanel.

ui.R:

require(shiny) shinyUI(pageWithSidebar(   headerPanel("My App"),    sidebarPanel(     fileInput("files", "Choose file"),     conditionalPanel(       condition = "input.files",       actionButton("submitFiles", "Submit files for processing"))),    mainPanel(h3("Nothing to see here")) )) 

I don't think there's anything to care about in my server.R, since the above example doesn't do anything. With the above condition, the button never shows up, i.e. the condition is never true.

Some things I've tried for my condition are input.files.length > 0, input.files.size() > 0, both of which result in the button being present before I upload a file. I'm guessing this is because input$files is an empty data.frame before choosing files, and so has a non-zero length/size, is that right?

What condition can I use to hide the button until at least one file is done uploading?

I think another option would be to replace conditionalPanel with uiOutput, and call renderUI({actionButton(...)}) inside of an observe/isolate block in server.R which is watching input.files (if (nrow(input$files) < 1) return()); is that the only way? If I can do this either way, what would make me pick one or the other (beyond conditionalPanel resulting in less code)?

like image 986
ClaytonJY Avatar asked Oct 30 '13 15:10

ClaytonJY


1 Answers

You have to make a reactive output returning the status of the uploading and set the option suspendWhenHidden of this output to FALSE.

More precisely, in server.R you surely have a reactive function, say getData() to make a dataframe from the uploaded file. Then do this:

  getData <- reactive({     if(is.null(input$files)) return(NULL)     ......   })   output$fileUploaded <- reactive({     return(!is.null(getData()))   })   outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE) 

And in ui.R you can use conditionalPanel() by doing:

conditionalPanel("output.fileUploaded",    ...... 
like image 120
Stéphane Laurent Avatar answered Oct 04 '22 19:10

Stéphane Laurent