gpt4 book ai didi

r - 使用 Shiny 创建响应式 selectInput - flexdashboard

转载 作者:行者123 更新时间:2023-12-03 09:07:26 31 4
gpt4 key购买 nike

我正在尝试在 Flexdashboard 文档中使用 Shiny 制作响应式(Reactive) selectInput。

  1. 我的第一个 selectInput 选择海洋公园中的区域类型。

    selectInput("Zone", label = "Marine Park Zoning:",
    choices = c("All", levels(EoTR$MarineParkZone)), selected = "All")
  2. 然后,我使用此输入创建一个响应式(Reactive)数据框,其中仅包含在步骤 1 中选择的区域。

    zone.choices = reactive({
    if (input$Zone=="All"){
    select(EoTR, ReefName, MarineParkZone, MarineParkMgmtSection)
    }else{
    select(EoTR, ReefName, MarineParkZone, MarineParkMgmtSection)%>%
    filter(MarineParkZone==input$Zone)}
    })
  3. 然后我尝试使用此响应式(Reactive)数据框来定义下一个 selectInput

    reactive({
    selectInput("Reef", label = "Priority Reef:",
    choices = zone.choices()$ReefName, selected = "Arlington Reef (16-064)")
    })

当我运行文档时,我的第二个输入显示一堆代码而不是选择菜单,因此基于该选择器的所有进程都会失败。

下面是一些可以重现该问题的代码

---
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")
})
```

我知道我定义响应式(Reactive)输入的方式可能有些愚蠢,但我真的很感谢对此的任何帮助。

干杯,

山姆

最佳答案

这是适合您的解决方案:

---
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)。

关于r - 使用 Shiny 创建响应式 selectInput - flexdashboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45975959/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com