Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a value of fileInput in Shiny?

Tags:

r

shiny

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.

like image 568
Pawel Avatar asked May 26 '17 14:05

Pawel


Video Answer


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)
})
...
like image 163
Felipe Gerard Avatar answered Oct 18 '22 23:10

Felipe Gerard


input$file1 is cached by Shiny so will not change before next upload.

Since you want a file name variable which map to input$file1$name in most time but reset to NULL when reset button is clicked, you need to create another layer and maintain this relationship.

  • You can create a variable upload_state, set it to uploaded with file upload event, and reset with reset button.

  • use a reactive expression which will take input$file1$name or NULL according to upload_state value.

There is no need for the submit button.

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)
like image 28
dracodoc Avatar answered Oct 18 '22 23:10

dracodoc