Home > OS >  How to clear fileInput after a file is selected
How to clear fileInput after a file is selected

Time:10-19

I have code with a fileInput and a calendar. If I import an excel file by fileInput the calendar appears. So far Ok, but I would like to tweak a feature on my reset button. The resetbutton deletes the dates I selected in the calendar, but my fileInput remains as Upload complete. Is there a way to adjust this, that is, to clean my fileInput as well?

library(shiny)
library(shinythemes)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-07-01","2021-07-02","2021-07-04"),
       Category = c("ABC","ABC","ABC"),
       Week= c("Wednesday","Wednesday","Wednesday")),
  class = "data.frame", row.names = c(NA, -3L))


ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 fileInput("file",h4(("Import file"),
                                           multiple = T,accept = ".xlsx")),
                                 br(),
                                 uiOutput("date"),
                                 actionButton("reset", "Reset")
                               ),
                               mainPanel(
                                   ),
                             ))
  ))

server <- function(input, output,session) {
  data <- reactive(df1)
  
  data <- eventReactive(input$file, {
    if (is.null(input$file)) {
      return(NULL)
    }
    else {
      df <- read_excel(input$file$datapath)
      return(df)
    }
  })
  
  
  output$date <- renderUI({
    all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
    disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
    dateInput(input = "database", 
              label = h4("Choose"),
              min = min(data()$date2),
              max = max(data()$date2),
              value = NA,
              datesdisabled = disabled)
    
  })
  
  observeEvent(input$reset, {
    df1 <- data()
    updateDateInput(session, 'database', value = NA)
})
}

shinyApp(ui = ui, server = server) 

Example of how it looks:

enter image description here

CodePudding user response:

You may include fileInput from server side and reload it after click on reset.

library(shiny)
library(shinythemes)
library(readxl)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-07-01","2021-07-02","2021-07-04"),
       Category = c("ABC","ABC","ABC"),
       Week= c("Wednesday","Wednesday","Wednesday")),
  class = "data.frame", row.names = c(NA, -3L))


ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 uiOutput('fileInput'),
                                 br(),
                                 uiOutput("date"),
                                 actionButton("reset", "Reset")
                               ),
                               mainPanel(
                               ),
                             ))
  ))

server <- function(input, output,session) {
  data <- reactive(df1)
  
  data <- eventReactive(input$file, {
    if (is.null(input$file)) {
      return(NULL)
    }
    else {
      df <- read_excel(input$file$datapath)
      return(df)
    }
  })
  
  
  output$date <- renderUI({
    all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
    disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
    dateInput(input = "database", 
              label = h4("Choose"),
              min = min(data()$date2),
              max = max(data()$date2),
              value = NA,
              datesdisabled = disabled)
    
  })
  
  observeEvent(input$reset, {
    df1 <- data()
    updateDateInput(session, 'database', value = NA)
    
    output$fileInput <- renderUI({
      fileInput("file",h4(("Import file"), multiple = T,accept = ".xlsx"))
    })
  })
  
  output$fileInput <- renderUI({
    fileInput("file",h4(("Import file"), multiple = T,accept = ".xlsx"))
  })
}

shinyApp(ui = ui, server = server) 

enter image description here

  • Related