Shiny: Constrain dateRangeInput

Hi guys.
I am looking for a quick solution to the fact that you can select max date which is lower than min date in shiny dateRangeInput. I didn't find any useful parameter in the function documentation, which would contrain the behavior of dateRangeInput. It is such a no-brainer that I am in awe, that it's not there. Thanks.

This might not be the most intuitive way and I agree that such a constraint should be built into the function, but the validate() function would work here as in this simple example (in this case the plot won't render unless the date range makes sense). You can of course change the message that the user sees when the condition is not satisfied:


# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            dateRangeInput('date', "Date", start = NULL, end = NULL, min = NULL,
                           max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0,
                           language = "en", separator = " to ", width = NULL)

        # Show a plot of the generated distribution

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
            need(input$date[1] <= input$date[2], 'Make sure dates are correct.')
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')

# Run the application 
shinyApp(ui = ui, server = server)
1 Like

Thanks valeri. I have tried to raise an issue for native solution on shiny github here. I don't consider error message a good practice in this particular case.

Currently the best workaround seems to be to implement shinyalert (git by Colin Fay ). Sadly, no true solution within the scope of dateRangeInput function.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.