Date Range Selector

#| standalone: true
#| components: [viewer]
#| viewerHeight: 380

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(inputId = "date", label = "Date Range"),
  hr(),
  verbatimTextOutput("date"),
  verbatimTextOutput("date1"),
  verbatimTextOutput("date2"),

  verbatimTextOutput("date1_class"),
  verbatimTextOutput("date1_year")
)

server <- function(input, output, session) {
  # You can access the value of the widget with input$date
  output$date <- renderPrint({ input$date })
  output$date1 <- renderPrint({ input$date[[1]] })
  output$date2 <- renderPrint({ input$date[[2]] })

  output$date1_class <- renderPrint({ class(input$date[[1]]) })
  output$date1_year <- renderPrint({ year(input$date[[1]]) })
}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(inputId = "date", label = "Date Range"),
  hr(),
  verbatimTextOutput("date"),
  verbatimTextOutput("date1"),
  verbatimTextOutput("date2"),

  verbatimTextOutput("date1_class"),
  verbatimTextOutput("date1_year")
)

server <- function(input, output, session) {
  # You can access the value of the widget with input$date
  output$date <- renderPrint({ input$date })
  output$date1 <- renderPrint({ input$date[[1]] })
  output$date2 <- renderPrint({ input$date[[2]] })

  output$date1_class <- renderPrint({ class(input$date[[1]]) })
  output$date1_year <- renderPrint({ year(input$date[[1]]) })
}

shinyApp(ui, server)
No matching items

Relevant Functions

  • dateRangeInput()
    dateRangeInput(inputId, label, start = NULL, end = NULL, min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", separator = " to ", width = NULL, autoclose = TRUE)

  • dateInput()
    dateInput(inputId, label, value = NULL, min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", width = NULL, autoclose = TRUE, datesdisabled = NULL, daysofweekdisabled = NULL)

  • updateDateRangeInput()
    updateDateRangeInput(session = getDefaultReactiveDomain(), inputId, label = NULL, start = NULL, end = NULL, min = NULL, max = NULL)

No matching items

Details

A date range selector allows you to select a pair of dates from two calendars.

To add a date range selector to your app:

  1. Add dateRangeInput() to the UI of your app to create a date range selector. Where you call this function will determine where the date range selector will appear within the app’s layout.

  2. Specify the id and label parameters of dateRangeInput() to define the identifier and label of the daterange selector. dateRangeInput() also includes various optional parameters, including start and end, which set the initial start and end dates.

The value of an input component is accessible as a reactive value within the server() function. To access the value of a daterange selector:

  1. Use input$<date_range_id>() to access the value of a daterange selector (e.g., input$daterange()). The server value of a daterange selector is a vector of date objects. You can access the individual tuple elements using square brackets and indices (e.g., input$daterange()[[0]]).

The date format string specifies how the date will be displayed in the browser. It allows the following values:

  • yy Year without century (12)
  • yyyy Year with century (2012)
  • mm Month number, with leading zero (01-12)
  • m Month number, without leading zero (1-12)
  • M Abbreviated month name
  • MM Full month name
  • dd Day of month with leading zero
  • d Day of month without leading zero
  • D Abbreviated weekday name
  • DD Full weekday name

Variations

Stat, end, minimum and maximum date range

Set the initial start and end dates with the start and end parameters. You can limit the range of dates with min and max.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range start, end, min, max",
    start = Sys.Date() - 1,
    end = Sys.Date() + 1,
    min = Sys.Date() - 14,
    max = Sys.Date() + 14
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range start, end, min, max",
    start = Sys.Date() - 1,
    end = Sys.Date() + 1,
    min = Sys.Date() - 14,
    max = Sys.Date() + 14
  )
)

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

shinyApp(ui, server)

Change the displayed date range format

Use the format paramter to change the displayed format. The widget still returns a date in yyyy-mm-dd format.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range format",
    format = "MM dd, yyyy"
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range format",
    format = "MM dd, yyyy"
  )
)

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

shinyApp(ui, server)

Change the staring view

Use the startview paramter to change the view when the date is clicked. Defaults to “month”.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range starting view",
    startview = "decade"
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change Date Range starting view",
    startview = "decade"
  )
)

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

shinyApp(ui, server)

Change the starting day of the week.

Use the weekstart paramter to change the starting day of the week in the month view.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Starting day of the week to Monday",
    weekstart = 1
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Starting day of the week to Monday",
    weekstart = 1
  )
)

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

shinyApp(ui, server)

Change the language.

Use the language paramter to change calendar language.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change the Language",
    format = "MM dd, yyyy",
    language = "fr-CH"
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change the Language",
    format = "MM dd, yyyy",
    language = "fr-CH"
  )
)

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

shinyApp(ui, server)

Change the separator.

Use the separator paramter to change separator between the dates.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change the Language",
    separator = " - "
  )
)

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

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateRangeInput(
    inputId = "date",
    label = "Change the Language",
    separator = " - "
  )
)

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

shinyApp(ui, server)
No matching items