Reputation: 35
I have a DF with a field for weekday dates between 2021-01-01 and yesterday. I have a SliderInput that has the min and max values set for the same range. In the SliderInput, it is still possible to see weekend dates and pick a date that does not exist in the DF, which is weekdays only. If someone does this, they see an error instead of the plots. Any way to specify only weekdays in the SliderInput or point it just to the values in the DF?
SliderInput:
sliderInput(inputId = "trajectory",
label = "Date Range:",
min = as.Date(min(DF$dates),"%Y-%m-%d"),
max = as.Date(max(DF$dates),"%Y-%m-%d"),
value = c(as.Date(today() - 180), as.Date(Sys.Date()-1)))
Upvotes: 0
Views: 451
Reputation: 26
I was facing the same problem and this worked for me. The solution relies on shinyWidgets::sliderTextInput()
. For this to work you must define your choices of dates and default values up front (I'd recommend somewhere before the UI):
#packages
library(shinyWidgets)
library(lubridate)
#date choices
choices <- seq.Date(date("2021-01-01", today() - 1, by = 1)
choices <- choices[!wday(choices) %in% c(1, 7)] #removes weekends
default <- seq.Date(today() - 182, today() - 180, by = 1)
default <- default[!wday(default) %in% c(1, 7)]
default <- max(default) #most recent weekday
Then you want to stick this within the appropriate place in your UI in place of sliderInput()
:
sliderTextInput("trajectory", "Date Range:", choices = choices,
selected = c(default, max(choices)))
You'll retain many of the benefits of sliderInput
doing it this way, but you may have to work with the grid
option to get tick marks and labels to your liking.
Upvotes: 1