Home > Blockchain >  How to fix reset button function in shiny code
How to fix reset button function in shiny code

Time:11-01

Could you help me adjust my reset button from the code Shiny below? I would like that when I press the reset button, only the fileInput would be left, that way I wouldn't have the daterange of PAGE1 and neither the calendar of PAGE2.

In order for you to test the code below, you will need the database that is at this link: enter image description here

CodePudding user response:

Try this

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

ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("PAGE1",
                             sidebarLayout(
                               sidebarPanel(
                                 uiOutput('fileInput'),
                                 uiOutput('daterange'),
                                 actionButton("reset", "Reset")
                               ),
                               mainPanel(
                                 DTOutput('table')
                               )
                             )),
                    
                    tabPanel("PAGE2",
                             sidebarLayout(
                               sidebarPanel(
                                 uiOutput('date'),
                                 uiOutput("mycode")
                                 
                               ),
                               mainPanel(
                                 tabsetPanel(
                                   tabPanel("", plotOutput("graph",width = "100%", height = "600")
                                   )
                                 ))
                             ))))

server <- function(input, output,session) {
  rv <- reactiveValues(data=NULL, data2=NULL)
  
  output$fileInput <- renderUI({
    fileInput("file",h4("Import file"), multiple = T, accept = ".xlsx")
  })
  
  sheetnames <- eventReactive(input$file, {
    available_sheets = openxlsx::getSheetNames(input$file$datapath)
  })
  
  data <- reactive({
    if (is.null(input$file)) {
      return(NULL)
    }
    
    else {
      df3 <- read_excel(input$file$datapath,sheetnames()[1])
      validate(need(all(c('date1', 'date2') %in% colnames(df3)), "Incorrect file"))  
      df4 <- df3 %>% mutate_if(~inherits(., what = "POSIXct"), as.Date) 
      return(df4)
    }
  })
  
  data2 <- reactive({
    req(input$file)
    df1 <- read_excel(input$file$datapath,sheetnames()[2])
    df1
  })
  
  output$daterange <- renderUI({
    req(rv$data)
    data <- rv$data
    dateRangeInput("daterange1", "Period you want to see:",
                   min = min(data$date2),
                   max = max(data$date2))
  })
  
  data_subset <- reactive({
    req(input$daterange1, rv$data)
    df1 <- rv$data
    req(input$daterange1[1] <= input$daterange1[2])
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(df1, date2 %in% days)
  })
  
  observe({
    rv$data <- data()
    rv$data2 <- data2()
  })
  
  output$table <- renderDT({
    datatable(data_subset())
  })
  proxy <- dataTableProxy("table")
  
  output$date <- renderUI({
    req(rv$data2)
    data2 <- rv$data2
    all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
    disabled <- as.Date(setdiff(all_dates, as.Date(data2$date2)), origin = "1970-01-01")
    entrydate<- dateInput(input = "Date2", 
                          label = h4("Choose"),
                          min = min(data2$date2),
                          max = max(data2$date2),
                          format = "dd-mm-yyyy",
                          datesdisabled = disabled)
    entrydate$children[[2]]$attribs$placeholder <- "No selected date"
    entrydate
  })
  
  output$mycode <- renderUI({
    req(input$Date2,rv$data2)
    df1 <- rv$data2
    df5 <- df1[as.Date(df1$date2) %in% input$Date2,]
    selectInput("code", label = h4("Category"),choices=c("No code selected" = "", sort(unique(df5$Category))))
  })
  
  output$graph <- renderPlot({
    req(input$Date2,input$code,rv$data2 )
    df1 <- rv$data2
    if (input$code=="") return(NULL)
    
    plot(x=df1$DR02,y=df1$DR1)
  })
  
  observeEvent(input$reset, {
    rv$data <- NULL
    rv$data2 <- NULL
    replaceData(proxy,NULL)
  })
}

shinyApp(ui = ui, server = server) 
  • Related