1

A question concerning dateRangeInput.

I am aware of the min/max, start/end parameters.

I need to put a constraint on the range that is given, viz. on the difference end-start. Specifically, I give the user the possibility of computing a rolling average, and need to make sure that there are at least 6 months between start and end.

How can I achieve this please?

(There is a way to have inputs depend on one another (see answer to this question). Here however, we want the datarangeInput[1] to depend upon daterangeInput[2]... but these two are not quite separate.)

EDIT: comments below pretty much answer the question as originally phrased (see below). For the code to be entirely satisfactory however, how can you make sure that the input range is corrected by the server BEFORE other computations that depend upon that input range are themselves started? With my existing solution (see below), the observer will correct the value too late: calculations based on the faulty range already started by that time

Community
  • 1
  • 1
hartmut
  • 934
  • 12
  • 25
  • 1
    the obvious solution is to calculate the values in the server, then put it control with those values in the ui with `renderUI`. Any reason not to do this? I also don't understand what you intend to do with the "rolling average". Rolling average of what goes where? – Mike Wise May 10 '17 at 07:07
  • 1
    see here: http://stackoverflow.com/questions/43785070/make-upper-value-of-r-shiny-slider-range-always-be-higher-than-lower-value/43787513#43787513. You can update the solution from 1m -> 6m – Tonio Liebrand May 10 '17 at 07:08
  • Thx much @MikeWise. I hope to have captured you comments below. Extending upon the solution though, how can you make sure that the input range is corrected by the server BEFORE other computations that depend upon that input range are themselves started? With my existing solution (see below), the observer will correct the value too late: calculations based on the faulty range already started by that time. – hartmut May 10 '17 at 08:01
  • 1
    You mean because it is reactive? Not really clear to me what calculations you mean, but it sounds like you might need to be using the `eventReactive` with an `actionButton` pattern so as to set up your values before a calculation is allowed to proceed. – Mike Wise May 10 '17 at 08:04
  • 1
    See these answers, particularly mine. http://stackoverflow.com/questions/33519816/shiny-what-is-the-difference-between-observeevent-and-eventreactive – Mike Wise May 10 '17 at 08:05
  • 1
    And you might want to add a sample dummy calculation to your example to illustrate your point. – Mike Wise May 10 '17 at 08:06

1 Answers1

3

Following BigDataScientist's and Mike Wise's comments, I adapted this answer to solve the issue. The result is a server code that makes sure that the range resulting from the daterangeInput is longer than a given value (here, smallestWindow).

rm(list=ls())
library(shiny)

mindate <- "1980-01-01"
maxdate <- Sys.Date()
smallestWindow <- 18 * 31
from <- maxdate - smallestWindow

ui <- fluidPage(
  mainPanel(uiOutput("slider"))
)

server <- function(input, output, session) {
  observeEvent(input$timeperiod,{
    if(input$timeperiod[1] > (input$timeperiod[2] - smallestWindow)){
      updateDateRangeInput(session, "timeperiod", start=input$timeperiod[2] - smallestWindow)
    }
  })

  output$slider <- renderUI({
    dateRangeInput("timeperiod", "Time Period:"
                   , min=as.Date(mindate), max=maxdate
                   , start = from , end = maxdate)
  })

}
shinyApp(ui, server)
Community
  • 1
  • 1
hartmut
  • 934
  • 12
  • 25