0

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 selectInput , 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)

1 Answers1

1

Unfortunately a selectInput will by default show the first option. But following this answer one option would be to use a selectizeInput with multiple=TRUE and options = list(maxItems = 1).

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?"),
          selectizeInput("hours",
            label = h5("Which work shift do you choose??"), choices = NULL,
            multiple = TRUE,
            options = list(maxItems = 1)
          ),
          selectizeInput("market",
            label = h5("Choose a market??"), choices = NULL,
            multiple = TRUE,
            options = list(maxItems = 1)
          )
        ),
        mainPanel()
      )
    )
  )
)

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

  observe({
    updateSelectizeInput(session, "hours",
      choices = unique(df1[df1$Days == week_day(), "Openinghours"])
    )
  })

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

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Hi @stefan would you be able to solve this question: https://stackoverflow.com/questions/75390637/adjust-selectinput-in-a-shiny-code-considering-two-dataframes thanks – Carlo Soares Feb 09 '23 at 11:44