I am making animation using animationOptions
in sliderInput()
. I want to use date/daterange in a slider, but sliderInput()
does not accept date. There was a post in the shiny group. As of March, 2013, there was no solution yet. So I searched around and learned that one can achieve this using JavaScript. Here is the post. Unfortunately, I cannot write in JS. So, I searched around more and came up with this link and wrote the following codes. This works, but the numbers on the slider are confusing. Is this the only way one can move around and use date in sliderInput()
? Thank you very much for taking your time.
server.R
library(shiny)
shinyServer(function(input, output, session) {
observe({
updateDateInput(session, "ana", value = as.Date("2014-07-01") + input$foo)
})
output$preImage <- renderImage({
filename <- normalizePath(file.path('./www',
paste(input$ana, '.png', sep='')))
list(src = filename,
alt = paste("There is no image for ", input$ana, ".", sep = ""))
}, deleteFile = FALSE)
})
ui.R
shinyUI(pageWithSidebar(
headerPanel("renderImage example"),
sidebarPanel(
sliderInput("foo", "Animation duration", min = -30,
max = 30, value = 0, step = 1,
animate = animationOptions(loop = TRUE, interval = 1000)),
dateInput("ana", "Choose a date:", value = as.Date("2014-07-01"))
),
mainPanel(
# Use imageOutput to place the image on the page
imageOutput("preImage")
)
))
I don't know when this feature was added, but now date values are automatically recognized in
sliderInput()
. And some related parameters aretimeFormat
andstep
(1-day by default)Reference: http://shiny.rstudio.com/reference/shiny/latest/sliderInput.html
The
sliderInput
control offers some limited formatting options. You can add aformat
parameter to yoursliderInput
to change the displayed text for the slider.Ideally, you would want the
sliderInput
to offer a custom mapping function that maps the current value to an exact date. But since that is not the option, the only wiggle room for improvement is to slightly change the text a little bit with theformat
parameter.Accepted
format
can be found in this document.Based on the above document, probably you can change your
sliderInput
into:This will display as
+10 Days
, when originally it was "+10".It may not be what you want exactly, but this is what you can do without writing Javascript.
EDIT: show customized slider value using Javascript
Say if you really want to change the current value label for the slider to indicate a somewhat sophisticated value (date, transformed values etc.), you can write some small Javascript snippet to do that.
One example provided by the
jslider
control (which is what Shiny uses for yoursliderInput
) indicates that by calling the constructor of a slider with acalculate
function, you can manually change the mapping from the current value (numeric) to a custom string (in this case, a date).Without being too verbose, let's just say that the slider's Javascript object is called
slider
. Which you can always get by calling:where
select
is a jQuery selector. Again, in this case it is#foo
because the slider has an idfoo
set inui.R
. When initiated, theslider.settings.calculate
function appeared in the example will be bound to be theslider.nice
function. So, we can simply override thenice
function to achieve our goal.Below is a modified
ui.R
with a piece of Javascript to do exactly thenice
function overriding.ui.R
Another detail we might want to tweak is the label on both ends of the slider. To achieve this we could:
slider.domNode
by callingslider.generateScales()
;<span>
's with classjslider-label
.For example if we take approach 2, we can modify the
HTML()
part ofui.R
as:And the labels will display the dates as expected.
This is of course some quick hacking and may not work if some other customization is made to the slider.
I think somehow the Shiny team should consider adding a
calculate
option insliderInput
which directly maps toslider.settings.calculate
, just to make things like this easier.