创建反应式 selectInput - 带有 Shiny 的 flexdashboard [英] Create reactive selectInput - flexdashboard with Shiny
问题描述
我正在尝试在 flexdashboard 文档中使用 Shiny 进行响应式 selectInput.
I'm trying to make a reactive selectInput using Shiny in a flexdashboard document.
我的第一个
selectInput
选择海洋公园的区域类型.
My first
selectInput
selects the type of Zone in a Marine Park.
selectInput("Zone", label = "Marine Park Zoning:",
choices = c("All", levels(EoTR$MarineParkZone)), selected = "All")
然后我使用此输入创建一个反应数据框,其中仅包含在步骤 1 中选择的区域.
Then I use this input to create a reactive data frame with only the Zones selected in step 1.
zone.choices = reactive({
if (input$Zone=="All"){
select(EoTR, ReefName, MarineParkZone, MarineParkMgmtSection)
}else{
select(EoTR, ReefName, MarineParkZone, MarineParkMgmtSection)%>%
filter(MarineParkZone==input$Zone)}
})
然后我尝试使用这个反应数据框来定义我对下一个 selectInput
reactive({
selectInput("Reef", label = "Priority Reef:",
choices = zone.choices()$ReefName, selected = "Arlington Reef (16-064)")
})
当我运行文档时,我的第二个输入显示了一堆代码而不是选择菜单,因此基于该选择器的所有进程都失败了.
When I run the document my second input displays a bunch of code instead of the select menu and therefore all the processes based from that selector fail.
下面是一些可以重现问题的代码
Below is some code that will reproduce the problem
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r}
library(flexdashboard)
library(dplyr)
EoTR = data.frame(ReefName=c("Reef1", "Reef2", "Reef3", "Reef4"),
MarineParkZone=c("Fished", "Fished", "Un-Fished", "Un-Fished"))
selectInput("Zone", label = "Marine Park Zoning:",
choices = c("All", levels(EoTR$MarineParkZone)), selected = "All")
zone.choices = reactive({
if (input$Zone=="All"){
select(EoTR, ReefName, MarineParkZone)
}else{
select(EoTR, ReefName, MarineParkZone)%>%
filter(MarineParkZone==input$Zone)}
})
reactive({
selectInput("Reef", label = "Priority Reef:",
choices = zone.choices()$ReefName, selected = "Reef1")
})
```
我知道我定义响应式输入的方式可能有些愚蠢,但我非常感谢您对此的任何帮助.
I know it's probably something silly with how I'm defining my reactive input but I'd really appreciate any help on this.
干杯,
山姆
推荐答案
这是适合您的解决方案:
Here is the solution for you:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r}
library(flexdashboard)
library(dplyr)
EoTR = data.frame(ReefName=c("Reef1", "Reef2", "Reef3", "Reef4"),
MarineParkZone=c("Fished", "Fished", "Un-Fished", "Un-Fished"))
selectInput("Zone", label = "Marine Park Zoning:",
choices = c("All", levels(EoTR$MarineParkZone)), selected = "All")
zone.choices = reactive({
if (input$Zone=="All"){
EoTR
}else{
EoTR %>%
filter(MarineParkZone==input$Zone)}
})
renderUI({selectInput("Reef", label = "Priority Reef:",
choices = zone.choices()$ReefName, selected = "Reef1")})
```
问题在于您的 selectInput("Reef"...)
,您已将其设置为响应式(这是不正确的)-->您应该宁愿将其渲染为 UI 对象 (renderUI
).
The problem was with your selectInput("Reef"...)
, you have set it to be reactive (which is inccorect) --> you should rather render it as UI object (renderUI
).
这篇关于创建反应式 selectInput - 带有 Shiny 的 flexdashboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!