Debian Codegraph @

R/input-daterange.R

raw
1 #' Create date range input
2 #'
3 #' Creates a pair of text inputs which, when clicked on, bring up calendars that
4 #' the user can click on to select dates.
5 #'
6 #' The date `format` string specifies how the date will be displayed in
7 #' the browser. It allows the following values:
8 #'
9 #' \itemize{
10 #' \item `yy` Year without century (12)
11 #' \item `yyyy` Year with century (2012)
12 #' \item `mm` Month number, with leading zero (01-12)
13 #' \item `m` Month number, without leading zero (1-12)
14 #' \item `M` Abbreviated month name
15 #' \item `MM` Full month name
16 #' \item `dd` Day of month with leading zero
17 #' \item `d` Day of month without leading zero
18 #' \item `D` Abbreviated weekday name
19 #' \item `DD` Full weekday name
20 #' }
21 #'
22 #' @inheritParams dateInput
23 #' @param start The initial start date. Either a Date object, or a string in
24 #' `yyyy-mm-dd` format. If NULL (the default), will use the current
25 #' date in the client's time zone.
26 #' @param end The initial end date. Either a Date object, or a string in
27 #' `yyyy-mm-dd` format. If NULL (the default), will use the current
28 #' date in the client's time zone.
29 #' @param separator String to display between the start and end input boxes.
30 #'
31 #' @family input elements
32 #' @seealso [dateInput()], [updateDateRangeInput()]
33 #'
34 #' @examples
35 #' ## Only run examples in interactive R sessions
36 #' if (interactive()) {
37 #'
38 #' ui <- fluidPage(
39 #' dateRangeInput("daterange1", "Date range:",
40 #' start = "2001-01-01",
41 #' end = "2010-12-31"),
42 #'
43 #' # Default start and end is the current date in the client's time zone
44 #' dateRangeInput("daterange2", "Date range:"),
45 #'
46 #' # start and end are always specified in yyyy-mm-dd, even if the display
47 #' # format is different
48 #' dateRangeInput("daterange3", "Date range:",
49 #' start = "2001-01-01",
50 #' end = "2010-12-31",
51 #' min = "2001-01-01",
52 #' max = "2012-12-21",
53 #' format = "mm/dd/yy",
54 #' separator = " - "),
55 #'
56 #' # Pass in Date objects
57 #' dateRangeInput("daterange4", "Date range:",
58 #' start = Sys.Date()-10,
59 #' end = Sys.Date()+10),
60 #'
61 #' # Use different language and different first day of week
62 #' dateRangeInput("daterange5", "Date range:",
63 #' language = "de",
64 #' weekstart = 1),
65 #'
66 #' # Start with decade view instead of default month view
67 #' dateRangeInput("daterange6", "Date range:",
68 #' startview = "decade")
69 #' )
70 #'
71 #' shinyApp(ui, server = function(input, output) { })
72 #' }
73 #'
74 #' @section Server value:
75 #' A [Date] vector of length 2.
76 #'
77 #' @export
78 dateRangeInput <- function(inputId, label, start = NULL, end = NULL,
79 min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month",
80 weekstart = 0, language = "en", separator = " to ", width = NULL,
81 autoclose = TRUE) {
82
83 start <- dateYMD(start, "start")
84 end <- dateYMD(end, "end")
85 min <- dateYMD(min, "min")
86 max <- dateYMD(max, "max")
87
88 restored <- restoreInput(id = inputId, default = list(start, end))
89 start <- restored[[1]]
90 end <- restored[[2]]
91
92 attachDependencies(
93 div(id = inputId,
94 class = "shiny-date-range-input form-group shiny-input-container",
95 style = css(width = validateCssUnit(width)),
96
97 shinyInputLabel(inputId, label),
98 # input-daterange class is needed for dropdown behavior
99 div(class = "input-daterange input-group input-group-sm",
100 tags$input(
101 class = "form-control",
102 type = "text",
103 # `aria-labelledby` attribute is required for accessibility to avoid doubled labels (#2951).
104 `aria-labelledby` = paste0(inputId, "-label"),
105 # title attribute is announced for screen readers for date format.
106 title = paste("Date format:", format),
107 `data-date-language` = language,
108 `data-date-week-start` = weekstart,
109 `data-date-format` = format,
110 `data-date-start-view` = startview,
111 `data-min-date` = min,
112 `data-max-date` = max,
113 `data-initial-date` = start,
114 `data-date-autoclose` = if (autoclose) "true" else "false"
115 ),
116 # input-group-prepend and input-group-append are for bootstrap 4 forward compat
117 span(class = "input-group-addon input-group-prepend input-group-append",
118 span(class = "input-group-text",
119 separator
120 )
121 ),
122 tags$input(
123 class = "form-control",
124 type = "text",
125 # `aria-labelledby` attribute is required for accessibility to avoid doubled labels (#2951).
126 `aria-labelledby` = paste0(inputId, "-label"),
127 # title attribute is announced for screen readers for date format.
128 title = paste("Date format:", format),
129 `data-date-language` = language,
130 `data-date-week-start` = weekstart,
131 `data-date-format` = format,
132 `data-date-start-view` = startview,
133 `data-min-date` = min,
134 `data-max-date` = max,
135 `data-initial-date` = end,
136 `data-date-autoclose` = if (autoclose) "true" else "false"
137 )
138 )
139 ),
140 datePickerDependency()
141 )
142 }