Create reactive selectInput - flexdashboard with Shiny

北战南征 提交于 2019-12-10 12:11:45

问题


I'm trying to make a reactive selectInput using Shiny in a flexdashboard document.

  1. 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")
    
  2. 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)}
    })
    
  3. Then I try to use this reactive data frame to define my choices for the next 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.

Cheers,

Sam


回答1:


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

```

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



来源:https://stackoverflow.com/questions/45975959/create-reactive-selectinput-flexdashboard-with-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!