R Shiny Date range input

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 06:22:42

问题


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.


回答1:


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")
      })
    }
  ))


来源:https://stackoverflow.com/questions/28709353/r-shiny-date-range-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!