Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny Date range input

I have a date range input function as follows in my ui for my shiny app.

  dateRangeInput("dates", 
    "Date range",
    start = "2015-01-01", 
    end = as.character(Sys.Date()))

However I want a pop up message to correct the user if the user chooses a start date that is later than the end date, instead of an error in the app. How do i do this?

Also is it possible to only allow the user to choose a date range which is more than, say x, days.

like image 906
Vik Avatar asked Feb 25 '15 01:02

Vik


1 Answers

You can provide custom error messages with a validate statement. Here is a simple example.

library(shiny)

runApp(
  list(
    ui = fluidPage(
      dateRangeInput("dates", 
                     "Date range",
                     start = "2015-01-01", 
                     end = as.character(Sys.Date())),
      textOutput("DateRange")
      ),

    server = function(input, output){
      output$DateRange <- renderText({
        # make sure end date later than start date
        validate(
          need(input$dates[2] > input$dates[1], "end date is earlier than start date"
               )
          )

        # make sure greater than 2 week difference
        validate(
          need(difftime(input$dates[2], input$dates[1], "days") > 14, "date range less the 14 days"
               )
          )

        paste("Your date range is", 
              difftime(input$dates[2], input$dates[1], units="days"),
              "days")
      })
    }
  ))
like image 146
cdeterman Avatar answered Oct 20 '22 00:10

cdeterman