How to split dates in update range input in R shiny

So I am trying to have a shiny app where I can update the user date ranges. So once the user selects the date range I want to use the start date and end date in other text options. Heres what I got so far but I am unable to figure out how to use the range date values anywhere else.Belows the code for what I got so far :

## Only run examples in interactive R sessions
if (interactive()) {
  
  ui <- fluidPage(
    sliderInput("n", "Day of month", 1, 30, 10),
    dateRangeInput("inDateRange", "Input date range"),
    textInput("st","Start date is :"),
    textInput("end","End date is :")
  )
  
  server <- function(input, output, session) {
    observe({
      date <- as.Date(paste0("2013-04-", input$n))
      
      updateDateRangeInput(session, "inDateRange",
                           label = paste("Date range label", input$n),
                           start = date - 1,
                           end = date + 1,
                           min = date - 5,
                           max = date + 5
      )
    })
  }
  
  shinyApp(ui, server)
}

Whenever I want one input to depend on another input, I tend to use uiOutput() and renderUI() (the alternative is to use updateXYZInput(), which can be faster, but it's also a little harder to reason about). Here's how you could use these 'generic' UI containers to get the behavior you're looking for:

library(shiny)

ui <- fluidPage(
  sliderInput("n", "Day of month", 1, 30, 10),
  dateRangeInput("inDateRange", "Input date range", start = Sys.Date(), end = Sys.Date() + 1),
  uiOutput("start_date"),
  uiOutput("end_date")
)

server <- function(input, output, session) {
  observe({
    date <- as.Date(paste0("2013-04-", input$n))
    
    updateDateRangeInput(session, "inDateRange",
                         label = paste("Date range label", input$n),
                         start = date - 1,
                         end = date + 1,
                         min = date - 5,
                         max = date + 5
    )
  })
  
  output$start_date <- renderUI({
    textInput("start_date_input", "Start date", input$inDateRange[1])
  })
  
  output$end_date <- renderUI({
    textInput("end_date_input", "End date", input$inDateRange[2])
  })
  
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.