I would like to have in my Shiny App
sliderInput
with datetimes
. I found out that it is possible to do by timeFormat
option. I add step (1/24) to jump by 1 hour and on timeline it looks fine, but when I checked what sliderInput
return I found out that it returns only date
. It is possbile to get the same value that we have shown on sliderInput
?
Simply code to show example:
rm(list=ls())
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"Time",
min = as.Date("2010-12-31 00:00:00"),
max = as.Date("2014-12-31 23:00:00"),
value = as.Date("2010-12-31 00:00:00"),
timeFormat="%F %T",
step = (1/24),
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({as.character(input$slider)})
})
shinyApp(ui = ui, server = server)
The values in min
, max
and value
should be of type POSIXct
if you want output from slider to be of datetime. In case of datetime values in slider the step
then becomes value in seconds so we need to change it to 86400 if we want it to represent a day.
library(shiny)
ui <- basicPage(
sliderInput(
"slider",
"Time",
min = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
max = as.POSIXct("2014-12-31 23:00:00", tz = "UTC"),
value = as.POSIXct("2010-12-31 00:00:00", tz = "UTC"),
step = 86400,
animate = TRUE
),
fluidRow(textOutput("SliderText"))
)
server <- shinyServer(function(input, output, session){
output$SliderText <- renderText({format(input$slider, "%F %T")})
})
shinyApp(ui = ui, server = server)
Note that input$slider
returns output of type POSIXct
. However, when you have time as 00:00:00
then it is not displayed by default(Try as.POSIXct("2010-12-31 00:00:00", tz = "UTC")
). To avoid that we are using format
which will show 00:00:00
in time but will return output of type character.