0

I would like to tweak my second selectInput(market). Note that when choosing Sunday or Tuesday in the calendar, the corresponding work shift appears, which in this case is Morning or Evening. These values are considering information from my df1 database. Therefore, for example, if I choose a Sunday on the calendar, then choose Morning shift, I would like it to show in the second selectInput the markets that work Sunday Morning, which in this case would be Market1 and Market2.

library(shiny)
library(shinythemes)
library(lubridate)


df1<- structure(
  list(
    Marketname = c("Market1","Market1", "Market2","Market2", "Market3", "Market3"),
    Days = c("Sunday","Sunday","Sunday","Sunday", "Sunday","Tuesday"),
    Openinghours = c("Morning","Evening", "Morning","Evening","Evening","Evening")
  ), row.names = c(NA, 6L), class = "data.frame")


ui <- fluidPage(  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 dateInput("date", "Which day shift do you choose?"),
                                 selectInput("hours", label = h5("Which work shift do you choose??"), choices = NULL, 
                                             selected = ""),
                                 selectInput("market", label = h5("Choose a market??"), choices = NULL, 
                                             selected = "")
                               ),
                               mainPanel(
                               )
                             ))
  ))

server <- function(input, output,session) {
  
  observe({
    week_day <- wday(input$date, label = TRUE, abbr = FALSE)
    
    updateSelectInput(session, "hours", choices = unique(df1[df1$Days == week_day, 3]))
  })
}

shinyApp(ui = ui, server = server) 

1 Answers1

1

You could use a second observer.

Note: I also added a reactive to store the weekday and which can be used in both observers.

server <- function(input, output, session) {
  week_day <- reactive({
    wday(input$date, label = TRUE, abbr = FALSE, locale = "en_US")
  })
  
  observe({
    updateSelectInput(session, "hours",
      choices = unique(df1[df1$Days == week_day(), "Openinghours"])
    )
  })

  observe({
    updateSelectInput(session, "market",
      choices = unique(df1[df1$Days == week_day() & df1$Openinghours %in% input$hours, "Marketname"])
    )
  })
}

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Perfect @stefan, that's exactly what I wanted, thank you very much for that. Just one doubt, see that when I select a Sunday, for example, Morning is already selected in the second `selecInput`, would it be possible to leave this option unselected, both in terms of the work shift and the chosen market? Therefore, these options would be selected by the user only. – Carlo Soares Feb 01 '23 at 12:11
  • Hi Carlo. Unfortunately a `selectInput` will by default show the first option. A workaround would be to set `multiple=TRUE` whichs defaults to no selection. But from an UI perspective I would that only if multiple selections make sense. Perhaps a `selectizeInput` might help. See https://stackoverflow.com/questions/24175997/force-no-default-selection-in-selectinput for a related post and options. – stefan Feb 01 '23 at 12:40