How to reset the fileInput value in Shiny?

There are already similar posts on this (like how can I update the shiny fileInput object? ), But I still can't figure out how I can get Shiny to forget the value input$file

after using the widget fileInput

.

The problem gets nagging when one has to initiate a file upload with actionButton

(called "Submit"

in my case) and then reset its value with another actionButton

( "Reset"

here). When you click Submit again, it becomes apparent that the value of the input file remains.

I have tried some recommended solutions: shinyjs

package and update module fileInput

with renderUI

/ uiOutput

- but still 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 would be grateful for any help.

+4


source to share


2 answers


input$file1

cached by Shiny, so will not change before next boot.

Since you want the filename variable to appear in input$file1$name

most cases, but reset to NULL

when the reset button is clicked, you need to create another layer and maintain this relationship.

  • You can create a variable upload_state

    , set it in uploaded

    with the file upload event and reset

    using the reset button.

  • use a reactive expression that will take input$file1$name

    or NULL

    according to the value upload_state

    .



No need for a button submit

.

library(shiny)
ui <- shinyUI(bootstrapPage(
  headerPanel("Reset / Submit file input example"),
  sidebarPanel(
    fileInput('file1', label = NULL),
    fluidRow(
      column(4,
             actionButton('reset', 'Reset Input')
      ))
  ),

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

server <- shinyServer(function(input, output, session) {
  values <- reactiveValues(
    upload_state = NULL
  )

  observeEvent(input$file1, {
    values$upload_state <- 'uploaded'
  })

  observeEvent(input$reset, {
    values$upload_state <- 'reset'
  })

  file_input <- reactive({
    if (is.null(values$upload_state)) {
      return(NULL)
    } else if (values$upload_state == 'uploaded') {
      return(input$file1)
    } else if (values$upload_state == 'reset') {
      return(NULL)
    }
  })

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

shinyApp(ui = ui, server = server)

      

+5


source


I know this is an old post, but you can reset the input by restoring it from scratch on 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)
})
...

      

+1


source







All Articles