2

I have a slider that looks like this: slider

The code for the slider is as follows:

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

Is there a way so that I can always keep the upper value of the slider a certain amount of ticks above the bottom value of the slider?

Dustin Knight
  • 350
  • 1
  • 4
  • 17
  • I want the minimum and the maximum to stay the same. I want the lower range of the upper value to be greater than the lower value and never be equal. Also, The end date cannot go before the start date by default, I am worried about showing too little data and I am not worried about it breaking because the range flips. – Dustin Knight May 04 '17 at 14:26

1 Answers1

3

You can add 31 days to the datetime object you have, however that is crude. Other ways of adding a month you can have a look here: 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)))
    }
  })

Edit: To use the dates later on You can access the dates via the sliderMonth$Month reactive I've created

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)
Community
  • 1
  • 1
Pork Chop
  • 28,528
  • 5
  • 63
  • 77