R语言 在selecInput中不选择任何选项

fhity93d  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(147)

如果您使用下面的代码,可以看到它将有一个date和两个selectInput。例如,如果我在日历上选择了一个Sunday,而在第二个selectInput中已经选择了Morning选项,那么是否可以不选中此选项,无论是与轮班还是与所选市场相关的选项?因此,这些选项将仅由用户选择。

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)
anhgbhbe

anhgbhbe1#

不幸的是,selectInput默认显示第一个选项,但是在this answer之后的一个选项是使用selectizeInputmultiple=TRUEoptions = 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)

相关问题