Skip to content

Commit d9c7f21

Browse files
committed
Make updateSliderInput work with date/datetimes. Fixes rstudio#904
1 parent 926e508 commit d9c7f21

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

R/update-input.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,39 @@ updateNumericInput <- function(session, inputId, label = NULL, value = NULL,
269269
#' )
270270
#' }
271271
#' @export
272-
updateSliderInput <- updateNumericInput
272+
updateSliderInput <- function(session, inputId, label = NULL, value = NULL,
273+
min = NULL, max = NULL, step = NULL)
274+
{
275+
# Make sure that value, min, max all have the same type, because we need
276+
# special handling for dates and datetimes.
277+
vals <- dropNulls(list(value, min, max))
278+
279+
type <- unique(lapply(vals, function(x) {
280+
if (inherits(x, "Date")) "date"
281+
else if (inherits(x, "POSIXt")) "datetime"
282+
else "number"
283+
}))
284+
if (length(type) > 1) {
285+
stop("Type mismatch for value, min, and max")
286+
}
287+
288+
if (type == "date" || type == "datetime") {
289+
to_ms <- function(x) 1000 * as.numeric(as.POSIXct(x))
290+
if (!is.null(min)) min <- to_ms(min)
291+
if (!is.null(max)) max <- to_ms(max)
292+
if (!is.null(value)) value <- to_ms(value)
293+
}
294+
295+
message <- dropNulls(list(
296+
label = label,
297+
value = formatNoSci(value),
298+
min = formatNoSci(min),
299+
max = formatNoSci(max),
300+
step = formatNoSci(step)
301+
))
302+
session$sendInputMessage(inputId, message)
303+
}
304+
273305

274306
updateInputOptions <- function(session, inputId, label = NULL, choices = NULL,
275307
selected = NULL, inline = FALSE,

0 commit comments

Comments
 (0)