本文介绍了如何使用 Shiny 在 RMarkdown 中进行嵌套选择 selectInput()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面来自我的 RMarkdown \ flexdashboard 代码和 shiny 的代码片段中,我需要修改 choices 用于第二个 selectInput() 函数,基于在第一个 selectInput() 函数中所做的选择.

In a snippet of code below from my RMarkdown \ flexdashboard code with shiny , I need to modify the choices for the second selectInput() function, based on the selection made at the first selectInput() function.

selectInput('theme', 'Select theme:',
            choices = c(dtThemes$Theme %>% unique()))

selectInput('question', 'Select Question:',
            choices = c(dtQuestions$Question %>% unique())) # This works
            #choices = strQuestions)  # This does not work

strQuestions <- reactive({
    nQuestions <- dtThemes[Theme == input$theme, Q2018]
    dtQuestions[nQuestion %in% nQuestions, strQuestion]
})

我该怎么做?

renderUI()中封装代码没有帮助:

Encapsulating code inrenderUI() did not help:

  renderUI({
    selectInput('question', 'Select Question:',
                strQuestions(), width="100%")
  })

推荐答案

我进一步研究了这篇文章:创建反应式 selectInput - flexdashboard with Shiny 并且能够弄清楚如何让我的代码工作:

I've further studied this post: Create reactive selectInput - flexdashboard with Shiny and was able to figure out how to make my code work:

selectInput('theme', 'Select theme:',
            choices = c("All", dt$Theme %>% unique()), selected="All")

dt.subset<- reactive({
    if (input$theme=="All") {
      dt
    }else{
      dt[Theme == input$theme]
    }
  })
 renderUI({
    selectInput('question', 'Select Question:',
                choices = (dt.subset()$Question ) %>% unique(),
                selected =  ( (dt.subset()$Question ) %>% unique() )[1])
  })

}

诀窍是您需要有一个默认的 selected 值.否则,代码崩溃.

The trick is that you need to have a default selected value. Otherwise,the code crashes.

还要注意,我将 data.table 用于 dt.

Note also I'musing data.table for dt.

这篇关于如何使用 Shiny 在 RMarkdown 中进行嵌套选择 selectInput()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:32