【发布时间】:2019-11-29 07:19:00
【问题描述】:
我正在尝试在 flexdashboard 中创建一个依赖于另一个用户输入的用户输入。示例数据集:alphabet_data
用户在 selectizeInput 中选择“字母表”,比如 ABD,基于我希望用户获取“数字”的 selectizeInput 选项,以便仅显示 5、6、7、8。
- 我尝试在“字母表”上观察事件,以创建新的依赖输入新
- 我使用 NULL 选项创建了依赖输入“数字”,并使用观察事件来更新selectizeInput。
- 我根据字母选择创建了一个新表,然后在响应式中使用该表来创建“数字”输入。
- 下面有重现问题的代码。
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