【问题标题】:Force user to select at least n number of options from pickerInput (R shiny)?强制用户从pickerInput(R闪亮)中选择至少n个选项?
【发布时间】:2021-11-30 12:15:27
【问题描述】:

您好,有帮助的 R 社区,

问题:我有两个不同的pickerInputs-list_1list_2 中的两种不同类型组织的列表。我想强制用户从两个列表中选择至少 5 个(例如,他们可以从 list_1 中选择 3 个组织,从 list_2 中选择 2 个组织)。当他们选择至少 5 个组织时,我想在 mainPanel 中渲染他们选择的内容。如果他们没有选择至少 5 个组织,我希望消息是“请选择至少 5 个组织以继续!”

这是一个代表:

# LIBRARIES ----
library(shiny)
library(shinyWidgets)
library(glue)



# USER INTERFACE ----
ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      width = 4,
      p("Peer Group Comparisons"),
      
      pickerInput(
        inputId  = "list_1",
        label    = "Organizations from List 1",
        choices  = c("a", "b", "c", "d"),
        options  = pickerOptions(
          actionsBox = TRUE,
          liveSearch = TRUE),
           multiple = TRUE 
      ),
      pickerInput(
        inputId  = "list_2",
        label    = "Organizations from List 2",
        choices  = c("e", "f", "g", "h", "x", "y", "z"),
        options  = pickerOptions(
          actionsBox = TRUE,
          liveSearch = TRUE),
        multiple = TRUE 
      )
      ),
      
     
      mainPanel = mainPanel(width = 8,
                            textOutput("results")
      )
      
    )
    
  )
  
  
  # SERVER ----
  server <- function(input, output, session) {
    
    output$list1_and_list2 <- reactive({
      glue(input$list1, input$list2)
    })
    
    output$results <- renderText(
      list1_and_list2() 
    )
  }
  
  shinyApp(ui, server)

【问题讨论】:

    标签: r shiny conditional-statements selectinput pickerinput


    【解决方案1】:
    library(shiny)
    library(shinyWidgets)
    library(glue)
    
    
    
    # USER INTERFACE ----
    ui <- fluidPage(
    
    sidebarLayout(
        sidebarPanel = sidebarPanel(
            width = 4,
            p("Peer Group Comparisons"),
            
            pickerInput(
                inputId  = "list_1",
                label    = "Organizations from List 1",
                choices  = c("a", "b", "c", "d"),
                options  = pickerOptions(
                    actionsBox = TRUE,
                    liveSearch = TRUE),
                multiple = TRUE 
            ),
            pickerInput(
                inputId  = "list_2",
                label    = "Organizations from List 2",
                choices  = c("e", "f", "g", "h", "x", "y", "z"),
                options  = pickerOptions(
                    actionsBox = TRUE,
                    liveSearch = TRUE),
                multiple = TRUE 
            )
        ),
        
        
        mainPanel = mainPanel(width = 8,
                              textOutput("results")
        )
        
    )
    
    )
    
    
    # SERVER ----
    server <- function(input, output, session) {
    
    output$results <- renderText(
              if(length(c(input$list_1, input$list_2))<5) {
             "Please select a minimum of 5 organizations to proceed!"
              } else {
                c(input$list_1, input$list_2)
              }
     
    )
    }
    
    shinyApp(ui=ui, server=server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 2020-10-19
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多