How to reset a value of fileInput in Shiny?

Viewed 8074

There are already similar posts on this matter (ex. how can I update a shiny fileInput object?), but I still cannot figure out how one may force Shiny to forget a value of input$file after the fileInput widget is used.

The problem becomes nagging when one wants to trigger the upload of file with actionButton (called "Submit" in my case) and then reset its value with another actionButton ("Reset" here). When the "Submit" button is clicked again, it becomes evident that the input$file value is still there.

I tried to use some recommended solutions: shinyjs package and refreshing fileInput module with renderUI/uiOutput - but still with no result.

Here is my code:

server.R

shinyServer(function(input, output, session) {

values <- reactiveValues(
 file = NULL
)

observeEvent(input$submit, {
 values$file <- input$file1
})

observeEvent(input$reset, {
 values$file <- NULL
 output$resettableInput <- renderUI({
  fileInput('file1', label = NULL)
 })
}, ignoreNULL = F)

output$summary <- renderText({
 return(paste('Uploaded file:', values$file$name))
})
})

ui.R

shinyUI(bootstrapPage(

 headerPanel('Reset / Submit file input example'),

 sidebarPanel(

  uiOutput('resettableInput'),

  fluidRow(
          column(4,
                 actionButton('reset', 'Reset All')
          ),
          column(4,
                 actionButton('submit', 'Submit')
          )
  )
 ),

 mainPanel(
  h4('Summary'),
  verbatimTextOutput('summary')
 )
))

I will be grateful for any help.

2 Answers

I know this is an old post, but you can reset the input by rebuilding it from scratch in the server.

In ui.R you can put:

...
uiOutput('file1_ui') ## instead of fileInput('file1', label = NULL)
...

And in server.R add this:

...
output$file1_ui <- renderUI({
  input$reset ## Create a dependency with the reset button
  fileInput('file1', label = NULL)
})
...
Related