1

I have a column in my data frame which is nothing but the combination of year and months

Yearmonth 
----------
200912
201001
201002
201003
201004
201005
.
.
.
.

I need a slider in my R shiny dashboard which goes from min value of this column to max but in increments of values in the column.

So for example, I'd like my slider to go from 200912 to 201001 to 201002 etc all the way to the maximum value.

I've already tried this but in that case, my step is 1 so I get all intermediate values between the values.

ui <- fluidPage(
  titlePanel("censusVis"),

  sidebarLayout(
    sidebarPanel(

      sliderInput("month",
                  "Month:",
                  min = min(Yearmonth),
                  max = max(Yearmonth),
                  value =min(Yearmonth),
                  step = 1),
    ),
    mainPanel(
    )
  )
)

Can someone help me over here? I've very recently started using Shiny!

Jeet
  • 188
  • 12
  • There is no step value that will help you in this case since you are not selecting over a range of continuous values. I thought maybe the formatting options would let you map a simple integer to a year/month value but it doesn't seem to be that flexible. You'd likely have to write a custom HTML UI element to get that exact behavior. – MrFlick Oct 07 '19 at 14:57
  • Possible duplicate: https://stackoverflow.com/questions/40908808/how-to-sliderinput-for-dates – MrFlick Oct 07 '19 at 14:58
  • Another possible duplicate: https://stackoverflow.com/questions/37689689/shiny-slider-input-step-by-month – MrFlick Oct 07 '19 at 14:58

1 Answers1

3

I have previously used sliderTextInput from the package shinyWidgets to achieve this effect. Depending what your data.frame is called it should translate to something like the following

  sliderTextInput("month",
                  "Month:",
                  choices = sort(unique(my_data$Yearmonth)))

note if you do this you will need to convert the value of input$month back to a numeric if that's what you want.

Martin
  • 66
  • 4
  • Thanks! This works fine! Do you know how do I change this to a slider where I can select a range? Like what if I want to select everything between 201001 and 201005 @Martin – Jeet Oct 07 '19 at 15:10
  • Currently, this is just selecting individual values – Jeet Oct 07 '19 at 15:18
  • 1
    to do a range, you just pass a vector of length 2 to the selected parameter, e.g. selected=c("201001", "201005") then you can get them in your server code as: input$month[1] and input$month[2] – Martin Oct 07 '19 at 15:19
  • Thanks! Can I pass this range from 201001 to 201005 to my histogram so that it only shows y values for these X values 201001, 201002, 201003, 201004,201005? – Jeet Oct 07 '19 at 15:26
  • yes you should be able to access them in your plotOutput() function. You'll need to add some lines to filter your dataset to where yearmonth is >= as.numeric(input$month[1]) and <= as.numeric(input$month[2]), and then plot what's left – Martin Oct 07 '19 at 15:32