dateRangeInput() returns NULL


#1

The dateRangeInput on my shiny app used to work earlier but now the function always returns NULL for some reason. I am adding the necessary bits of code. Would be really helpful if someone figures out the problem.

#Server.R

output$dateRangeSelect <- renderUI({
    dateRangeInput(
      inputId = "dateRangeSelect",
      label = "Date Range:",
      min = min(data$lossDate),
      max = max(data$featureOpen)
      start = floor_date(max(data$featureOpen), unit = "month") 
            %m-% days(1) %m-% months(1),
      end = floor_date(max(data$featureOpen), unit = "month") 
            %m-% days(1))
  })



getDateRange <- reactive({
    input$go

    isolate({
      dateRange <- list(input$dateRangeSelect[1], input$dateRangeSelect[2])
    })
    return(dateRange)
  })

  filterHistoricalData <- reactive({
    tableDims <- getTableDims()
    dateRange <- getDateRange()

#UI.R

uiOutput("dateRangeSelect"),
          br(),

#2

Hi,

your code is not really reproducible because I don't know what is:

min = min(data$lossDate),
max = max(data$featureOpen)
start = floor_date(max(data$featureOpen), unit = "month")
%m-% days(1) %m-% months(1),
end = floor_date(max(data$featureOpen), unit = "month")

However, does the following answer your question:

library(shiny)
app <- shinyApp(
  ui = fluidPage(
    uiOutput("dateRangeSelect"),
    actionButton("go", "Go!")
  ),
  server = function(input, output) {
    output$dateRangeSelect <- renderUI({
      tagList(
        dateRangeInput(
          inputId = "dateRangeSelect",
          label = "Date Range:"
        )
      )
    })
    
    getDateRange <- reactive({
      input$go
      isolate({
        dateRange <- list(input$dateRangeSelect[1], input$dateRangeSelect[2])
      })
      return(dateRange)
    })
    
    observe({
      req(input$dateRangeSelect)
      dateRange <- getDateRange()
      print(dateRange)
    })   
  }
)
runApp(app)

This code gives me:

[[1]]
[1] "2018-03-22"

[[2]]
[1] "2018-03-22"

#3

My getDateRange() is not under an observe () segment. Is that a problem?


#4

getDataRange() is a reactive expression, thus you will end up with an error if it is not in a reactive context. You can use it either inside an observe or observeEvent. See below where I replace my observe by:

dateRange <- getDateRange()

It will trigger an error:

Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    45: .getReactiveEnvironment()$currentContext
    44: .dependents$register
    43: getDateRange
    42: server [#26]
     1: runApp

Besides, if I remove req(input$dateRangeSelect) from my observe then the dateRangeInput is NULL:

[[1]]
NULL

[[2]]
NULL

This might solve your issue