How to tell shiny to update value of fileInput

1,737 views
Skip to first unread message

Dean Attali

unread,
Jun 16, 2015, 5:58:18 PM6/16/15
to shiny-...@googlegroups.com
I've seen a lot of questions asking how to reset file inputs so I want to try to find a nice solution.  I noticed that if I set the value of the input to '' and then call `trigger('change')` on the input, shiny still thinks it has the previous value, until a new file is chosen.

Example

js <- 
"Shiny.addCustomMessageHandler('reset', function(message) {
  $('#file').val('');
  $('#file').trigger('change');
})"
  
runApp(shinyApp(
  ui = fluidPage(
    tags$head(tags$script(HTML(js))),
    fileInput("file", "File"),
    actionButton("show", "Show value"),
    actionButton("reset", "Reset")
  ),
  server = function(input, output, session) {
    observeEvent(input$show, cat(str(input$file), "\n"))
    observeEvent(input$reset, session$sendCustomMessage("reset", list()))
  }
))


Initially, "Show value" will output NULL. After selecting a file, it'll show that file. After clicking "reset", the input element has an empty value as far as JS is concerned, but clicking on "show value" shows that Shiny didn't update its value. Is there a way to force shiny to update the value even when it's NULL?

Dean Attali

unread,
Jun 16, 2015, 11:28:16 PM6/16/15
to shiny-...@googlegroups.com
Looking at the source code I don't quite understand how it works, but it looks like the input file is using a non standard way of implementing an input (since the "subscribe" function is calling a custom function, and "getValue" returns nothing)

https://github.com/rstudio/shiny/blob/master/srcjs/input_binding_fileinput.js#L191-L201

I don't quite understand how the information of what files are selected is transferred to shiny. Maybe this isn't doable?

Joe Cheng [RStudio]

unread,
Jun 17, 2015, 4:20:51 AM6/17/15
to shiny-...@googlegroups.com, daat...@gmail.com
"Isn't doable" is probably too strong, but certainly fileInput works differently than any other Shiny input and has its own custom codepath on both the client and the server. If you're motivated to implement this I can walk you through the code over vchat.

Dean Attali

unread,
Jun 17, 2015, 4:43:49 AM6/17/15
to shiny-...@googlegroups.com, daat...@gmail.com
I think I was able to follow the logic all the way to the `uploadEnd` RPC and I think that's where the value is being set? https://github.com/rstudio/shiny/blob/a6cd0fdb85d5d2175ebc4fcb590386e4cedcbbd9/R/shiny.R#L642

Thanks for the offer

Joe Cheng

unread,
Jun 17, 2015, 4:46:35 AM6/17/15
to Dean Attali, shiny-...@googlegroups.com
Yup that's right, except for older browsers that don't support the HTML5 File API; a few lines lower there's a "uploadie" code path that handles the upload in one shot.
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/6ce56151-58fc-43d7-89dd-34016344880c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dean Attali

unread,
Jun 17, 2015, 4:49:46 AM6/17/15
to Joe Cheng, shiny-...@googlegroups.com
Kudos for taking the effort to support old browsers, especially IE..
Reply all
Reply to author
Forward
0 new messages