3
votes

I'm using 'shiny' in RStudio.

I would like the pre-selected dates of the dateRangeInput widget to be updated with the min and max of a dataset.
When I try the code below (simplified for the start date only), the start date does not show up in the left box of the date range widget: instead, the box appears blank (but it is actually set as today's date - on clicking on the empty box, the calendar with today's date shows up).
ui.r: dateRangeInput("dates", label = "Date range", start='mydatestart', end = '2014-05-06')

The minimum date is actually selected since it shows up when using
ui.r: textOutput('mydatestart')

Here is a reproducible example:
ui.R

library(shiny)

shinyUI({
  sidebarPanel(
  dateRangeInput("dates", label = "Date range", start='mydatestart', end = '2014-05-06'),
  textOutput('mydatestart')
  )
}) 

server.R

  
shinyServer(function(input, output) {
  mydate<-c("2013-04-24", "2013-04-25", "2013-04-26", "2013-04-27", "2013-04-28", "2013-04-28", "2013-04-29", "2013-04-30")
  output$mydatestart<-renderText(min(mydate))
})

Using output$mydatestart<-renderText(as.Date(min(mydate))) gives the same issue.

Do you know why this is happening?
Thanks, Yvan

2

2 Answers

1
votes

You can put your dateRangeInputinto server.R so you can use any object defined there as arguments:

library(shiny)

ui <- shinyUI({
    sidebarPanel(
        htmlOutput("selector"),
        textOutput('mydatestart')
    )
})

server <- shinyServer(function(input, output) {
    mydate<-c("2013-04-24", "2013-04-25", "2013-04-26", "2013-04-27", "2013-04-28", "2013-04-28", "2013-04-29", "2013-04-30")
    output$mydatestart<-renderText({min(mydate)})

    output$selector <- renderUI({

        dateRangeInput("dates", label = "Date range"
                       , start=min(mydate), end = '2014-05-06')

    })

})

shinyApp(ui = ui, server = server)
4
votes
  1. You can't pass a string literal like mydatestart as an argument to dateRangeInput because it will interpret is as just that - a string - which is why the start field was empty. You need to give it a legitimate value.
  2. There were a couple of key functions missing from your shinyUI call, indicated with comments below.
  3. It's a little more idiomatic to put global data objects in a global.R file for your shiny applications, rather than defining them inside of the shinyServer function.

ui.R

library(shiny)

shinyUI({
  ## fluidPage(...)
  fluidPage(
    ## sidebarLayout(...)
    sidebarLayout(
      sidebarPanel(
        dateRangeInput(
          "dates", label = "Date range",
          start = min(mydate),
          end = '2014-05-06'),
        uiOutput("mydatestart2")
      ),

      mainPanel(textOutput('mydatestart'))
    )
  )

})

server.R

library(shiny)

shinyServer(function(input, output) {
  # not accessable in UI
  output$mydatestart <- renderText(min(mydate))
  # accessable in UI
  output$mydatestart2 <- renderUI({
    dateRangeInput(
      "dates2",
      label = "Reactive Start Date",
      start = as.Date(input$dates[1]) + 7,
      end = as.Date(input$dates[1]) + 14
    )
  })
})

global.R

mydate <- c("2013-04-24", "2013-04-25", "2013-04-26",
            "2013-04-27", "2013-04-28", "2013-04-28",
            "2013-04-29", "2013-04-30")

enter image description here


enter image description here