滑块的代码如下:
UI:
fluidRow(column(12,
uiOutput("slider")))
服务器
mindate <- "2011-04-01"
maxdate <- "2017-03-31"
output$slider <- renderUI({
sliderInput("timeperiod", "Time Period:",
min=as.Date(mindate, origin='1970-01-01'),
max=as.Date(maxdate, origin='1970-01-01'),
value=c(as.Date(mindate, origin='1970-01-01'),
as.Date(maxdate, origin='1970-01-01')),
timeFormat='%b-%y', dragRange = TRUE, width='700px')
})
目前,如果您移动滑块输入,则可以将它们设置为相同的值,如下所示:
有没有办法可以让我总是将滑块的上限值保持在滑块底部值以上的一定数量的刻度线上?
答案 0 :(得分:3)
您可以将31天添加到您拥有的日期时间对象,但这是粗略的。其他添加月份的方法可以在这里查看:Add a month to a Date
observeEvent(input$timeperiod,{
if(input$timeperiod[1] == input$timeperiod[2]){
updateSliderInput(session, "timeperiod", value=c(input$timeperiod[1],(input$timeperiod[1]+31)))
}
})
编辑:稍后使用日期
您可以通过我创建的sliderMonth$Month
被动
rm(list=ls())
library(shiny)
monthStart <- function(x) {
x <- as.POSIXlt(x)
x$mday <- 1
as.Date(x)
}
mindate <- "2011-04-01"
maxdate <- "2017-03-31"
ui <- fluidPage(
mainPanel(uiOutput("slider"),textOutput("SliderText"))
)
server <- function(input, output, session) {
observeEvent(input$timeperiod,{
if(input$timeperiod[1] == input$timeperiod[2]){
updateSliderInput(session, "timeperiod", value=c(input$timeperiod[1],(input$timeperiod[1]+31)))
}
})
output$slider <- renderUI({
sliderInput("timeperiod", "Time Period:",
min=as.Date(mindate, origin='1970-01-01'),
max=as.Date(maxdate, origin='1970-01-01'),
value=c(as.Date(mindate, origin='1970-01-01'),as.Date(maxdate, origin='1970-01-01')),
timeFormat='%b-%y', dragRange = TRUE, width='700px')
})
sliderMonth <- reactiveValues()
observeEvent(input$timeperiod,{
full.date <- as.POSIXct(input$timeperiod, tz="GMT")
sliderMonth$Month <- as.character(monthStart(full.date))
})
output$SliderText <- renderText({sliderMonth$Month})
}
shinyApp(ui, server)