【问题标题】:selectInput choices dependent on another selectInput for R Shiny FlexdashboardselectInput 选择依赖于 R Shiny Flexdashboard 的另一个 selectInput
【发布时间】:2021-05-17 22:27:26
【问题描述】:

我正在尝试使用 Flexdashboard 构建一个闪亮的应用程序,我遇到了一个有趣的问题。本质上,我有一个带有两个 selectInput 下拉菜单的侧边栏。我希望第二个 selectInput 中的选择取决于第一个中的选择。到目前为止,我的方法是在第二个输入的两个动态生成的选项之间切换。这个Dynamic UI shiny 示例本质上正是我想要做的,但我认为我在从服务器/ui 格式转换为 Rmarkdown 格式时遇到了麻烦。

此时我有以下简化代码,它产生第一个 selectInput,但不是第二个 selectInput,它只是绘制一个 html 代码块。

---
title: "Shiny Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---

Tab1
===============================================================================  

Inputs {.sidebar}
-------------------------------------------------------------------------------

```{r}
selectInput('type', label = 'Input 1',
            choices = c('choice1', 'choice2'))

reactive({
  switch(input$type,
         'choice1' = selectInput('select2', 
                                 label = 'Input 2',
                                 choices = c('choice3', 'choice4')),
         'choice2' = selectInput('select3', label = 'Input 3',
                                 choices = c('choice5', 'choice6')))
  })
```

Column {data-width=600}
-------------------------------------------------------------------------------

### Test Output

```{r}

```

Test picture

我感觉我遗漏了一些非常简单的东西,但我似乎找不到在 Flexdashboard 的上下文中处理这个问题的任何东西。任何建议将不胜感激!

【问题讨论】:

  • 一种方法是在服务器端处理它 (input$type) 在 renderUI 中,然后在 ui 中处理 uiOutput

标签: r shiny flexdashboard shiny-reactivity


【解决方案1】:

试试这个

ui <- basicPage(
  selectInput('type', label = 'Input 1',
              choices = c('choice1', 'choice2')),
  uiOutput("mychoices")
)

server <- function(input,output){

  output$mychoices <- renderUI({
    switch(input$type,
           'choice1' = selectInput('select2', 
                                   label = 'Input 2',
                                   choices = c('choice3', 'choice4')),
           'choice2' = selectInput('select3', label = 'Input 3',
                                   choices = c('choice5', 'choice6')))
  })
  
}

shinyApp(ui,server)

【讨论】:

  • 太棒了!这很好用(有一些调整以适应 flexdashboard)
  • @Plato_of_the_Wilds 如果答案对accept the answer 有帮助,请点击左侧的复选标记。每个帖子只能接受一个答案。
猜你喜欢
  • 2016-04-28
  • 2017-10-19
  • 2021-01-22
  • 1970-01-01
  • 2016-01-10
  • 2020-05-30
  • 1970-01-01
  • 2017-07-15
  • 2019-01-19
相关资源
最近更新 更多