Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

javascript - Shiny Slider Input step by month

I am writing a shiny and and wanted a slider for the date. The date in my data are monthly and I would like to step forward one month at a time. The docs for the slider input say that the step value is either in second or days depending on the min/max parameter types. Currently I have:

sliderInput("slider", "Time", min=as.Date("2005-01-01"),
                              max=as.Date("2014-12-01"),
                              value=as.Date("2005-01-01"), step = 30,...)

I want to be able to step by month instead of by day but it doesn't seem possible from what they give me. Is there a snippet of js I could add that would give me this functionality?

Clarification Note: I have read the docs for this function and to my best understanding there is no base functionality for this. The time format parameter, upon testing, only changes the labels not the values. I have seen a couple posts that access the values of certain widgets and was wondering if this was possible. Eg)

<script type="text/javascript">
    $(document).ready(function() {
    var slider = $("#slider").slider();
 // override the default "nice" function.
    slider.nice = function(value) {
    var ref_date = new Date("2005-01-01");
 // each slider step is 1 day, translating to 24 * 3600 * 1000 milliseconds
    var slider_date = new Date(ref_date.getTime() + value * 24 * 3600 * 1000);
                          return [slider_date.getUTCFullYear(), 
                          slider_date.getUTCMonth() + 1, 
                          slider_date.getUTCDate()].join("-");
                          }
                          })
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a timeFormat function within the sliderInput. For more information visit Slider Input Widget.

EDIT:

To get the dates out and use them later on in your analysis, much credit goes to this question First day of the month from a POSIXct date time using lubridate and the function provided by Roland.

rm(list=ls())
library(shiny)
monthStart <- function(x) {
  x <- as.POSIXlt(x)
  x$mday <- 1
  as.Date(x)
}
ui <- basicPage(sliderInput("slider", "Time", min = as.Date("2010-01-01"),max =as.Date("2014-12-01"),value=as.Date("2014-12-01"),timeFormat="%b %Y"),
                textOutput("SliderText")
                )
server <- shinyServer(function(input, output, session){

  sliderMonth <- reactiveValues()
  observe({
    full.date <- as.POSIXct(input$slider, tz="GMT")
    sliderMonth$Month <- as.character(monthStart(full.date))
  })
  output$SliderText <- renderText({sliderMonth$Month})
})
shinyApp(ui = ui, server = server)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...