Search code examples
rdateshinyshinywidgets

Shiny - Limit date range selection to X number of days


I need an excact 2 day range for ShinyWidgets airDatepickerInput. Meaning I can only select 1 day in front of the first date selected.

How to update maxDate and minDate options for input$Dates[2] to be reactive and equal input$Dates[1] + 1

Are there better methods? How do you use updateAirDateInput ?

# Libraries
library(shiny)
library(shinyWidgets)


# UI
ui <- navbarPage(title = "Title - V1.0",
                 tabPanel("Tab 1",
                          sidebarLayout(
                            sidebarPanel(width = 4,
                                         airDatepickerInput(inputId = "Dates",
                                                            multiple = 2,
                                                            range = TRUE)),
                            mainPanel())))

# Server
server <- function(input, output, session) {}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

Solution

  • I found the answer myself.

    daterangepicker(
      inputId = "Dates",
      label = "Select Dates",
      start = as_date(Sys.Date()-60),
      end = as_date(Sys.Date()),
      min = '2021-01-01',
      max = Sys.Date(),
      options = daterangepickerOptions(maxSpan = list("days" = 2)),
      ranges = list("Today" = Sys.Date(),
                    "Yesterday" = Sys.Date() - 1,
                    "Yesterday & Today" = c(Sys.Date() - 1, Sys.Date()),
                    "Last 7 days" = c(Sys.Date() - 6, Sys.Date())))
    

    Refer here for basic and advanced examples:

    https://github.com/trafficonese/daterangepicker/tree/master/inst/examples