使 R 闪亮滑块范围的上限值始终高于下限值 [重复]

2024-02-14

I have a slider that looks like this: slider

滑块的代码如下:

UI:

fluidRow(column(12,
                    uiOutput("slider")))

SERVER:

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')
      })

Currently if you move the slider inputs they can be put as the same value like this: slider2

有没有办法让我始终将滑块的上限值保持在滑块下限值之上一定量的刻度?


您可以向现有的日期时间对象添加 31 天,但这很粗略。您可以在这里查看其他添加月份的方法:为日期添加一个月 https://stackoverflow.com/questions/14169620/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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使 R 闪亮滑块范围的上限值始终高于下限值 [重复] 的相关文章

随机推荐