Debian Codegraph @

R/input-date.R

raw
1 #' Create date input
2 #'
3 #' Creates a text input which, when clicked on, brings up a calendar 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 textInput
23 #' @param value The starting date. Either a Date object, or a string in
24 #' `yyyy-mm-dd` format. If NULL (the default), will use the current date
25 #' in the client's time zone.
26 #' @param min The minimum allowed date. Either a Date object, or a string in
27 #' `yyyy-mm-dd` format.
28 #' @param max The maximum allowed date. Either a Date object, or a string in
29 #' `yyyy-mm-dd` format.
30 #' @param format The format of the date to display in the browser. Defaults to
31 #' `"yyyy-mm-dd"`.
32 #' @param startview The date range shown when the input object is first clicked.
33 #' Can be "month" (the default), "year", or "decade".
34 #' @param weekstart Which day is the start of the week. Should be an integer
35 #' from 0 (Sunday) to 6 (Saturday).
36 #' @param language The language used for month and day names. Default is "en".
37 #' Other valid values include "ar", "az", "bg", "bs", "ca", "cs", "cy", "da",
38 #' "de", "el", "en-AU", "en-GB", "eo", "es", "et", "eu", "fa", "fi", "fo",
39 #' "fr-CH", "fr", "gl", "he", "hr", "hu", "hy", "id", "is", "it-CH", "it",
40 #' "ja", "ka", "kh", "kk", "ko", "kr", "lt", "lv", "me", "mk", "mn", "ms",
41 #' "nb", "nl-BE", "nl", "no", "pl", "pt-BR", "pt", "ro", "rs-latin", "rs",
42 #' "ru", "sk", "sl", "sq", "sr-latin", "sr", "sv", "sw", "th", "tr", "uk",
43 #' "vi", "zh-CN", and "zh-TW".
44 #' @param autoclose Whether or not to close the datepicker immediately when a
45 #' date is selected.
46 #' @param datesdisabled Which dates should be disabled. Either a Date object,
47 #' or a string in `yyyy-mm-dd` format.
48 #' @param daysofweekdisabled Days of the week that should be disabled. Should be
49 #' a integer vector with values from 0 (Sunday) to 6 (Saturday).
50 #'
51 #' @family input elements
52 #' @seealso [dateRangeInput()], [updateDateInput()]
53 #'
54 #' @examples
55 #' ## Only run examples in interactive R sessions
56 #' if (interactive()) {
57 #'
58 #' ui <- fluidPage(
59 #' dateInput("date1", "Date:", value = "2012-02-29"),
60 #'
61 #' # Default value is the date in client's time zone
62 #' dateInput("date2", "Date:"),
63 #'
64 #' # value is always yyyy-mm-dd, even if the display format is different
65 #' dateInput("date3", "Date:", value = "2012-02-29", format = "mm/dd/yy"),
66 #'
67 #' # Pass in a Date object
68 #' dateInput("date4", "Date:", value = Sys.Date()-10),
69 #'
70 #' # Use different language and different first day of week
71 #' dateInput("date5", "Date:",
72 #' language = "ru",
73 #' weekstart = 1),
74 #'
75 #' # Start with decade view instead of default month view
76 #' dateInput("date6", "Date:",
77 #' startview = "decade"),
78 #'
79 #' # Disable Mondays and Tuesdays.
80 #' dateInput("date7", "Date:", daysofweekdisabled = c(1,2)),
81 #'
82 #' # Disable specific dates.
83 #' dateInput("date8", "Date:", value = "2012-02-29",
84 #' datesdisabled = c("2012-03-01", "2012-03-02"))
85 #' )
86 #'
87 #' shinyApp(ui, server = function(input, output) { })
88 #' }
89 #'
90 #' @section Server value:
91 #' A [Date] vector of length 1.
92 #'
93 #' @export
94 dateInput <- function(inputId, label, value = NULL, min = NULL, max = NULL,
95 format = "yyyy-mm-dd", startview = "month", weekstart = 0,
96 language = "en", width = NULL, autoclose = TRUE,
97 datesdisabled = NULL, daysofweekdisabled = NULL) {
98
99 value <- dateYMD(value, "value")
100 min <- dateYMD(min, "min")
101 max <- dateYMD(max, "max")
102 datesdisabled <- dateYMD(datesdisabled, "datesdisabled")
103
104 value <- restoreInput(id = inputId, default = value)
105
106 tags$div(id = inputId,
107 class = "shiny-date-input form-group shiny-input-container",
108 style = css(width = validateCssUnit(width)),
109
110 shinyInputLabel(inputId, label),
111 tags$input(type = "text",
112 class = "form-control",
113 # `aria-labelledby` attribute is required for accessibility to avoid doubled labels (#2951).
114 `aria-labelledby` = paste0(inputId, "-label"),
115 # title attribute is announced for screen readers for date format.
116 title = paste("Date format:", format),
117 `data-date-language` = language,
118 `data-date-week-start` = weekstart,
119 `data-date-format` = format,
120 `data-date-start-view` = startview,
121 `data-min-date` = min,
122 `data-max-date` = max,
123 `data-initial-date` = value,
124 `data-date-autoclose` = if (autoclose) "true" else "false",
125 `data-date-dates-disabled` =
126 # Ensure NULL is not sent as `{}` but as 'null'
127 jsonlite::toJSON(datesdisabled, null = 'null'),
128 `data-date-days-of-week-disabled` =
129 jsonlite::toJSON(daysofweekdisabled, null = 'null')
130 ),
131 datePickerDependency()
132 )
133 }
134
135
136 datePickerDependency <- function(theme) {
137 list(
138 htmlDependency(
139 name = "bootstrap-datepicker-js",
140 version = version_bs_date_picker,
141 src = "www/shared/datepicker",
142 package = "shiny",
143 script = if (getOption("shiny.minified", TRUE)) "js/bootstrap-datepicker.min.js"
144 else "js/bootstrap-datepicker.js",
145 # Need to enable noConflict mode. See #1346.
146 head = "<script>(function() {
147 var datepicker = $.fn.datepicker.noConflict();
148 $.fn.bsDatepicker = datepicker;
149 })();
150 </script>"
151 ),
152 bslib::bs_dependency_defer(datePickerCSS)
153 )
154 }
155
156 datePickerSass <- function() {
157 sass::sass_file(
158 system_file(package = "shiny", "www/shared/datepicker/scss/build3.scss")
159 )
160 }
161
162 datePickerCSS <- function(theme) {
163 if (!is_bs_theme(theme)) {
164 return(htmlDependency(
165 name = "bootstrap-datepicker-css",
166 version = version_bs_date_picker,
167 src = "www/shared/datepicker",
168 package = "shiny",
169 stylesheet = "css/bootstrap-datepicker3.min.css"
170 ))
171 }
172
173 bslib::bs_dependency(
174 input = datePickerSass(),
175 theme = theme,
176 name = "bootstrap-datepicker",
177 version = version_bs_date_picker,
178 cache_key_extra = get_package_version("shiny")
179 )
180 }