Leave no options selected in selecInput

If you used the code below, see that it will have a date and two selectInput. If I select a Sunday on the calendar, for example, the Morning option is already selected in the second selecInput , would it be possible to leave this option unchecked, both in relation to the work shift and the chosen market? Therefore, these options would only be selected by the user.

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) {
  week_day <- reactive({
    wday(input$date, label = TRUE, abbr = FALSE)
  })
  
  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"])
    )
  })
}
shinyApp(ui = ui, server = server)

Yes, you can remove both observe() statements and list the actual choices in the selectInput() for both hours and market. This way, any calendar selection will have no impact on the other two inputs.

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 = c('', sort(unique(df1$Openinghours))), 
                                             selected = ''),
                                 selectInput("market", label = h5("Choose a market??"), 
                                             choices = c('', sort(unique(df1$Marketname))), 
                                             selected = '')
                               ),
                               mainPanel(
                               )
                             ))
  ))

server <- function(input, output, session) {
  week_day <- reactive({
    wday(input$date, label = TRUE, abbr = FALSE)
  })
  
}
shinyApp(ui = ui, server = server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.