【问题标题】:Create an input variable that is dependent on another input variable in flexdashboard shiny widget在 flexdashboard 闪亮小部件中创建一个依赖于另一个输入变量的输入变量
【发布时间】:2019-11-29 07:19:00
【问题描述】:

我正在尝试在 flexdashboard 中创建一个依赖于另一个用户输入的用户输入。示例数据集:alphabet_data

用户在 selectizeInput 中选择“字母表”,比如 ABD,基于我希望用户获取“数字”的 selectizeInput 选项,以便仅显示 5、6、7、8。

  1. 我尝试在“字母表”上观察事件,以创建新的依赖输入新
  2. 我使用 NULL 选项创建了依赖输入“数字”,并使用观察事件来更新selectizeInput。
  3. 我根据字母选择创建了一个新表,然后在响应式中使用该表来创建“数字”输入。
  4. 下面有重现问题的代码。

title: "未命名" 输出: flexdashboard::flex_dashboard: 方向:列 垂直布局:填充

运行时:闪亮

library(flexdashboard)
library(tidyverse)
alphabet_data <- read.table(
        text = "Alphabet       Number
        ABC       1
        DEF       4
        ABD       5
        ABC       2
        ABC       3
        ABD       6
        ABD       7
        ABD       8",
        header = TRUE,
        stringsAsFactors = FALSE)

列 {.sidebar data-width=650}

图表 A


selectizeInput(
    inputId  = "alphabet",
    label    = "Alphabet",
    choices  = unique(alphabet_data$Alphabet),
    multiple = TRUE,
    options  = list(maxItems = 2)
)

selectizeInput(
        inputId  = "number",
        label    = "Number",
        choices  = NULL,
        multiple = TRUE,
        options  = list(maxItems = 2)
)

selected_alphabet <- eventReactive(
    eventExpr = input$alphabet,

    valueExpr = {
    alphabet_data %>% 
            filter(Alphabet %in% input$alphabet)
})

reactive({
    observeEvent(
        eventExpr   = input$alphabet,
        handlerExpr = {
            updateSelectizeInput(
                inputId = "number",
                choices = selected_alphabet()$number
            )
        }
    )
})


列 {data-width=350}

图表 B

output$alphabet <- renderPrint(input$alphabet)
textOutput(outputId = "alphabet")

图表 C

renderPrint(selected_alphabet())

图表 D

output$number <- renderPrint(input$number)
textOutput(outputId = "number")

我希望当用户选择 ABD 字母表时,数字选项显示为 5、6、7、8。

【问题讨论】:

    标签: r shiny shinydashboard shinyjs flexdashboard


    【解决方案1】:

    我在运行你的示例脚本时遇到了问题,所以我写了一个类似的。

    你有两个选择:

    1. 使用renderUI()insertUI()在服务器中生成UI组件。
    2. 使用updateSelectInput() 更新UI 组件。

    我用 shiny 写了一个演示,虽然它没有使用 flexdashboard,但它做了同样的事情:

    library(shiny)
    
    ui <- fluidPage(
      fluidRow(
          tags$h1("level 1"),
          column(
              width = 6,
              selectizeInput("selectizeInput1","Input 1",choices = letters,selected = "",multiple = TRUE)
          ),
          column(
              width = 6,
              textOutput("textOutput1")
          )
      ),
      fluidRow(
          tags$h1("level 2"),
          column(
              width = 6,
              selectizeInput("selectizeInput2","Input 2",choices = "",selected = "",multiple = TRUE)
          ),
          column(
              width = 6,
              textOutput("textOutput2")
          )
      ),
      fluidRow(
          tags$h1("level 3"),
          column(
              width = 6,
              selectizeInput("selectizeInput3","Input 3",choices = "",selected = "",multiple = TRUE)
          ),
          column(
              width = 6,
              textOutput("textOutput3")
          )
      )
    
    )
    
    server <- function(input, output, session) {
        # level 1
        output$textOutput1 <- renderText(input$selectizeInput1)
    
        # level 2
        observe({
            updateSelectInput(
                session = session,
                inputId = "selectizeInput2",
                choices = input$selectizeInput1,
                selected = input$selectizeInput1
            )
            output$textOutput2 <- renderText(input$selectizeInput2)
    
        })
    
    
        # level 3
        observe({
            updateSelectInput(
                session = session,
                inputId = "selectizeInput3",
                choices = input$selectizeInput2,
                selected = input$selectizeInput2
            )
            output$textOutput3 <- renderText(input$selectizeInput3)
    
        })
    }
    
    shinyApp(ui, server)
    

    为了更好的理解,你可以阅读this article或者试试this app

    【讨论】:

      最近更新 更多