Create reactive selectInput - flexdashboard with Shiny

6.8k views Asked by At

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

There are 1 answers

1
Mal_a On

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