Clear/Reset fileInput()

4,456 views
Skip to first unread message

Harold Doran

unread,
Aug 3, 2016, 10:31:19 AM8/3/16
to Shiny - Web Framework for R
Have there been any recent advances that would allow for a user to clear out a file that was read in using fileInput()?

I can see how something like "pattern 4" at the link below might work, but not sure if this would "clear out" the progress bar that shows the prior file was read in, that might require some .css fix?

http://shiny.rstudio.com/articles/action-buttons.html

I have seen prior discussions on this over at stack overflow, but they are older so I'm curious if there are recent advancements here.

Thanks,
Harold

Dean Attali

unread,
Nov 3, 2016, 9:23:33 PM11/3/16
to Shiny - Web Framework for R
For anyone who needs this functionality in the future: you can use the reset() function from the shinyjs package (since version 0.8) https://github.com/daattali/shinyjs

mattek

unread,
Feb 26, 2017, 1:23:31 PM2/26/17
to Shiny - Web Framework for R
That's a great feature! I needed it badly to be able to reload a file that was updated. Here's a sample code for future usage/reference:

mattek

unread,
Mar 6, 2017, 11:49:44 AM3/6/17
to Shiny - Web Framework for R
Turns out my excitement was premature. shinyjs::reset does clear the text in fileInput but doesn't set the associated dt to NULL. Is there any reason the reset doesn't support fileInput?

Bárbara Borges

unread,
Mar 7, 2017, 3:46:04 AM3/7/17
to Shiny - Web Framework for R
This may not be related at all, but I recently answered a question on SO about a user not being able to hide their datatable (gist with both not-working and working versions). This is perhaps relevant to this issue in that I think the underlying problem is that Shiny does not forget about outputs when their "reset", only when their overwritten. This was a feature included by design, but that has bitten us before with insertUI and removeUI...

mattek

unread,
Mar 7, 2017, 7:35:33 AM3/7/17
to Shiny - Web Framework for R
Interesting. This indeed might be the issue. Any idea how to clear ("overwrite" with NULL) the fileInput? 

Bárbara Borges

unread,
Mar 20, 2017, 12:44:13 PM3/20/17
to Shiny - Web Framework for R
The easiest way around it is to take care of both resetting the fileInput widget AND the underlying data (treat it as two different things). For example:

library(shiny)
library(shinyjs) 

ui <- fluidPage(
  useShinyjs(),
  fileInput('inFile', 'Choose file'),
  actionButton('reset', 'Reset'),
  tableOutput('tbl')
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(data = NULL)
  
  observe({
    req(input$inFile)
    rv$data <- read.csv(input$inFile$datapath)
  })
  
  observeEvent(input$reset, {
    rv$data <- NULL
    reset('inFile')
  })
  
  output$tbl <- renderTable({
    rv$data
  })
}
  
shinyApp(ui, server)
Reply all
Reply to author
Forward
0 new messages