【问题标题】:How to branch processing using picker input如何使用选择器输入进行分支处理
【发布时间】:2021-02-09 01:00:33
【问题描述】:

我目前正在开发一个闪亮的应用程序。 我想使用多个选择器输入来分支处理流程。 具体来说,我想构建下图所示的处理流程。

例如 在 PickerInput1 中选择 Option1 时 → PickerInput2 显示中间类的向量。 → 将 PickerInput2 的 Multiple 选项设置为 False 以使其成为单选。 → 关闭数字输入,这样您就无法输入数字。 (首先让它变灰或隐藏在 UI 上) → 按下按钮显示“表达式1”。

当在 PickerInput1 中选择 Option2 时 → PickerInput2 显示中间类的向量。 → 将 PickerInput2 的 Multiple 选项设置为 False 以使其成为单选。 → 打开数字输入,这样您就无法输入数字。 → 按下按钮显示“表达式1”。

当在 PickerInput1 中选择 Option3 时 → PickerInput2 显示 Middle Class 和 Major Class 的向量。 → 将 PickerInput2 的 Multiple 选项设置为 True 以选择多个选项。 → 关闭数字输入,这样您就无法输入数字。 → 按下按钮显示“表达式2”。

当在 PickerInput1 中选择 Option4 时 → PickerInput2 显示 MajorClass 向量。 → 将 PickerInput2 的 Multiple 选项设置为 True 以选择多个选项。 → 关闭数字输入,这样您就无法输入数字。 → 按下按钮显示“表达式2”。

是否可以通过这种方式分支处理流程? 我特别想听 ・是否可以更改多个设置? (也许我会使用 updatePickerInput,但我可以用那个函数改变它吗?) ・ 数字输入可以打开/关闭吗?

示例代码如下。

library("shiny")
library("shinyWidgets")

major_class <- c("A1","A2","A3")
middle_class <- c("A1_1","A1_2","A2_1","A2_2","A3_1","A3_2")


ui <- fluidPage(

  fluidRow(
    column(
      width = 2, offset = 1,
      pickerInput(
        inputId = "p1",
        label = "PickerInput1",
        choices = c("Option1","Option2","Option3","Option4"),
        options = list(
          `live-Search` = TRUE,
          `actions-box` = TRUE,
          size = 7,
          `selected-text-format` = "count > 3"
        ),
        multiple =FALSE
      )
    ),
    column(
      width = 2,
      pickerInput(
        inputId = "p2",
        label = "PickerInput2",
        choices = "",
        options = list(
          `live-Search` = TRUE,
          `actions-box` = TRUE,
          size = 7,
          `selected-text-format` = "count > 3"
        ),
        multiple =TRUE
      ),
      numericInput("num", label = "Numeric input", value = 1)
    ),
    column(
      width = 2,
      actionButton("p3","finish")
      
      ),
    textOutput("resule_message")
      )
    )

  

server <- function(input, output, session) {

  observeEvent(input$p1 ,{
    
    if(input$p1 %in% c("Option1","Option2")){
      updatePickerInput(session = session, inputId = "p2",
                      choices = middle_class)
    }
    
    if(input$p1 %in% c("Option3")){
      updatePickerInput(session = session, inputId = "p2",
                        choices = list(major = major_class,
                                       middle = middle_class))
    }
    if(input$p1 %in% c("Option4")){
      updatePickerInput(session = session, inputId = "p2",
                        choices = major_class)
    }
    
    })
  
  observeEvent(input$p3 ,{
    
    if(input$p1 %in% c("Option1")){
      output$resule_message <- renderText({
        paste0("expression1")
      })
    }
    else{
      output$resule_message <- renderText({
        paste0("expression2")
      })
      
    }
  })

  
}

shinyApp(ui = ui, server = server)


能够解决这个问题的人,知道如何根据 PikcerInput 的输入条件改变处理流程的人, 如果你知道你可以用其他方法做类似的事情,请告诉我。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一种方法是使用renderUI 作为您的第二个pickerInput 并根据您的条件显示它。试试这个

    library("shiny")
    library("shinyWidgets")
    
    major_class <- c("A1","A2","A3")
    middle_class <- c("A1_1","A1_2","A2_1","A2_2","A3_1","A3_2")
    
    
    ui <- fluidPage(
      
      fluidRow(
        column(
          width = 2, offset = 1,
          pickerInput(
            inputId = "p1",
            label = "PickerInput1",
            choices = c("Option1","Option2","Option3","Option4"),
            options = list(
              `live-Search` = TRUE,
              `actions-box` = TRUE,
              size = 7,
              `selected-text-format` = "count > 3"
            ),
            multiple =FALSE
          )
        ),
        column(
          width = 2, uiOutput("pickr2"), uiOutput("numinput")
        ),
        column(
          width = 2,
          actionButton("p3","finish")
          
        ),
        textOutput("resule_message")
      )
    )
    
    
    server <- function(input, output, session) {
      
      output$pickr2 <- renderUI({
        req(input$p1)
        if(input$p1 %in% c("Option1","Option2")){
          choices <- middle_class
          TORF <- FALSE
        }else{
          TORF <- TRUE
          if(input$p1 %in% c("Option3")){
            choices <- c(middle_class,major_class)
          }else choices <- major_class
        }
        pickerInput(
          inputId = "p2",
          label = "PickerInput2",
          choices = choices,
          options = list(
            `live-Search` = TRUE,
            `actions-box` = TRUE,
            size = 7,
            `selected-text-format` = "count > 3"
          ),
          multiple =TORF
        )
        
      })
      
      output$numinput <- renderUI({
        req(input$p1)
        if(input$p1 %in% c("Option2")){
          numericInput("num", label = "Numeric input", value = 1)
        }else return(NULL)
        
      })
      
      observeEvent(input$p3 ,{
        req(input$p1)
        if(input$p1 %in% c("Option1")){
          output$resule_message <- renderText({
            paste0("expression1")
          })
        }else{
          output$resule_message <- renderText({
            paste0("expression2")
          })
          
        }
      })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2012-02-26
      • 1970-01-01
      • 2018-02-22
      • 2023-04-08
      相关资源
      最近更新 更多