Student
Student

Reputation: 61

Implementing checkbox to select all date columns in Shiny

I have a dataset that looks something like this:

 lat      lon        2020-01-22 2020-01-23 2020-01-24 2020-01-25 2020-01-26 2020-01-27
 32.53953 -86.64408           0          0          0          0          0          0
 30.72775 -87.72207           0          0          0          0          0          0
 31.86826 -85.38713           0          0          0          0          0          0
 32.99642 -87.12511           0          0          0          0          0          0
 33.98211 -86.56791           0          0          0          0          0          0
 32.10031 -85.71266           0          0          0          0          0          0

I am working on a shiny app that allows users to specify a date range using dateRangeInput. However, I would also like to give the users the option to select all of the dates available with the use of a checkboxInput, but I am not sure how to implement this. Here is what I have so far:

ui <- fluidPage(    

  sidebarLayout(
    sidebarPanel(
      dateRangeInput("date", "Date Range:",
                     start = as.character(Sys.Date() - 30),
                     end = as.character(Sys.Date())),
      checkboxInput("checkBox", "Select all dates", FALSE),
      textOutput("warning")
    ),

    mainPanel(
         ## Plot & Table will go here
    )
  )
)

server <- function(input, output) {
  
  output$warning <- renderText({
    validate(
      need(input$date[2] > input$date[1], "Error in date specification")
    )
  })

Upvotes: 0

Views: 140

Answers (1)

KM_83
KM_83

Reputation: 727

For a date range to be selected for all possible dates, you need to specify min and max. Then, use updateDateRangeInput(). https://shiny.rstudio.com/reference/shiny/latest/updateDateInput.html

library(shiny)

ui <- fluidPage(    
  
  sidebarLayout(
    sidebarPanel(
      dateRangeInput("date", "Date Range:",
                     start = as.character(Sys.Date() - 30),
                     end = as.character(Sys.Date()),
                     min = "2020-01-01", 
                     max =Sys.Date()),
      checkboxInput("checkBox", "Select all dates", FALSE),
      textOutput("warning")
    ),
    
    mainPanel(
      ## Plot & Table will go here
    )
  )
)

server <- function(input, output, session) {
  
  observe({
    if (input$checkBox==TRUE) {
      updateDateRangeInput(session, 
                           "date",
                           "Date Range:",
                           start =  "2020-01-01", # set equal to  min 
                           end = Sys.Date(),  # set equal to  max 
                           min = "2020-01-01", 
                           max =Sys.Date())
  
    }
      
  })
  
  output$warning <- renderText({
    validate(
      need(input$date[2] > input$date[1], "Error in date specification")
    )
  })
}

shinyApp(ui = ui, server = server)


Upvotes: 1

Related Questions