stakowerflol
stakowerflol

Reputation: 1079

Make dependency between inputs in UI

There are 2 dateinputs: date1 and date2. The date2 should not be before date1. How to make the dependency in UI?

What should I write in min = of date2?
I have tried: min = input$date1, min = 'input$date1', min = 'input.date1', 'min = 'input.date1'', "min = 'input$date1'", "min = 'input.date1'" but it doesn't work.

library(shiny)

ui <- fluidPage(
   dateInput("date1", "date 1"),
   dateInput("date2", "date 2 = not be before date 1", min = 'input.date1') 
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 115

Answers (2)

Pork Chop
Pork Chop

Reputation: 29387

Also can use observeEvent

library(shiny)

ui <- fluidPage(
  dateInput("date1", "date 1"),
  dateInput("date2", "date 2 = not be before date 1") 
)

server <- function(input, output, session) {
  observeEvent(input$date1,{
    updateDateInput(session, "date2", min = input$date1 + 1,value = input$date1 + 1)
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 2

thothal
thothal

Reputation: 20329

Not sure whether you can achieve this purely in the UI, but you can use updateDateInput in the server logic to get the required behaviour:

library(shiny)

ui <- fluidPage(
  dateInput("date1", "date 1"),
  dateInput("date2", "date 2 = not be before date 1") 
)

server <- function(input, output, session) {
  observe({
    req(input$date1)
    ## whenever input$date1 changes, change min of input$date2
    updateDateInput(session, "date2", min = input$date1)
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions