Antonio
Antonio

Reputation: 1111

Reset FileInputi in Shiny

I have a shiny app with fileInput.

I created a reset button to clear the file from the fileInput. I know the file is cached in the shiny memory, but I just want to reset the FileInput object to look like how it originally did before I uploaded the file, when I press the reset button.

I googled a bit and found most people get around this problem using shinyjs, but shinyjs does not work in the R package I am creating so I am trying to find a work around.

I insert a executable code below!

library(shiny)
library(shinythemes)

ui <- fluidPage(
                        
                        
                        titlePanel("Old Faithful Geyser Data"),
                        
                        fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
                                  width = "20%", buttonLabel = "Browse...",
                                  placeholder = "You didn't choose any files to test, so select beside"),
                        
                        sidebarLayout(
                            
                            sidebarPanel(h4("Select the best the number of bins"),
                                         br(),
                                         br(),
                                sliderInput("bins",
                                            "Number of bins:",
                                            min = 1,
                                            max = 50,
                                            value = 30),
                                actionButton("reset", "Reset"),
                            ),
                            
                            
                            
                            mainPanel(
                                plotOutput("distPlot")
                            )
                        )
)


server <- function(input, output,session) {
    
    output$distPlot <- renderPlot({
        
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
    
    observeEvent(input$reset, {
      updateSliderInput(session,"bins",value = 1)
})
       
}


shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 239

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

One way would be to use fileInput dynamically from server side, so when the reset button is clicked you can reload the input.

library(shiny)
library(shinythemes)

ui <- fluidPage(
  
  
  titlePanel("Old Faithful Geyser Data"),
  
  uiOutput('file'),
  sidebarLayout(
    
    sidebarPanel(h4("Select the best the number of bins"),
                 br(),
                 br(),
                 sliderInput("bins",
                             "Number of bins:",
                             min = 1,
                             max = 50,
                             value = 30),
                 actionButton("reset", "Reset"),
    ),
    
    
    
    mainPanel(
      plotOutput("distPlot")
    )
  )
)


server <- function(input, output,session) {
  
  output$distPlot <- renderPlot({
    
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
  
  output$file <- renderUI({
    fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
              width = "20%", buttonLabel = "Browse...",
              placeholder = "You didn't choose any files to test, so select beside")
  })
  
  observeEvent(input$reset, {
    updateSliderInput(session,"bins",value = 1)
    
    output$file <- renderUI({
      fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
                width = "20%", buttonLabel = "Browse...",
                placeholder = "You didn't choose any files to test, so select beside")
    })
    
  })
  
}


shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 2

Related Questions