Search code examples
rdplyrshinyreactiveflexdashboard

How can I create a reactive table in a flexdashboard or Shiny app, where the table output depends on the selected item in a drop-down list?


I am brand new to shiny apps/flexdashboard in R. I have a csv file that I would like to use to create a reactive dashboard. My aim is to have a drop down list on the left panel that, when an item is selected, it pulls and outputs the specific row in my dataframe that corresponds to that item.

Current RMarkdown file is as follows:

---
title: "Dashboard Test"
output:
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    vertical_layout: fill
    css: style.css
runtime: shiny
---

{r setup, include=FALSE}

library(flexdashboard)
library(shiny)
library(dplyr)



Inputs {.sidebar data-width=250}
-----------------------------------------------------------------------
<h3>Dashboard Test</h3>

Use the Search Bar to select your species of interest. Trait data (growth form, dormancy type, seed mass, germination treatment and seed length) will update to suit your selection.
&nbsp;  
&nbsp;  
<h3>Species</h3>

{r}

data <- read.csv("./species_traits_summary.csv")

      selectizeInput(
        inputId = "searchme", 
        label = "Search Bar",
        multiple = TRUE,
        choices = unique(data$Species),
        options = list(
          create = FALSE,
          placeholder = "Search Me",
          onDropdownOpen = I("function($dropdown) {if (!this.lastQuery.length) {this.close(); this.settings.openOnFocus = false;}}"),
          onType = I("function (str) {if (str === \"\") {this.close();}}"),
          onItemAdd = I("function() {this.close();}")
        )
      )

     


Column
-----------------------------------------------------------------------

{r}


I imagine something along these lines would work:

reactive(output <- df %>% filter(species %in% input$searchme)

The following outputs my fixed csv as the main panel:

renderTable(data)

But I would like the output to be dependent on what is selected in the side panel search bar.


Solution

  • Use output <- reactive(data %>% filter(Species %in% input$searchme) to create your filtered dataset, then create a table of the filtered dataset using renderTable(output()).

    Using a minimal reproducible example based on the iris dataset:

    ---
    title: "Dashboard Test"
    output:
      flexdashboard::flex_dashboard:
        orientation: columns
        social: menu
        vertical_layout: fill
        css: style.css
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)
    library(dplyr)
    ```
    
    Inputs {.sidebar data-width=250}
    -----------------------------------------------------------------------
    <h3>Dashboard Test</h3>
    
    Use the Search Bar to select your species of interest. Trait data (growth form, dormancy type, seed mass, germination treatment and seed length) will update to suit your selection.
    &nbsp;  
    &nbsp;  
    <h3>Species</h3>
    
    ```{r}
    data <- iris[seq(1, 150, 10),]
    
    selectizeInput(
      inputId = "searchme",
      label = "Search Bar",
      multiple = TRUE,
      choices = unique(data$Species),
      options = list(
        create = FALSE,
        placeholder = "Search Me",
        onDropdownOpen = I("function($dropdown) {if (!this.lastQuery.length) {this.close(); this.settings.openOnFocus = false;}}"),
        onType = I("function (str) {if (str === \"\") {this.close();}}"),
        onItemAdd = I("function() {this.close();}")
      )
    )
    ```
    
    ```{r}
    output <- reactive(data %>% filter(Species %in% input$searchme))
                       
    renderTable(output())
    ```
    

    enter image description here