Search code examples
rshinyr-markdownflexdashboard

How do I use to Shiny in Flexdashboard to "filter" a table?


I am working in with Flexdashboard and Shiny for the first time, and I am having a hard time figuring out how to render a table on the dashboard. I am trying to have an input select option on a sidebar that displays the individual "seasons" of a game. The goal is to display one season at a time, as defined in the "season" column in my data frame.

Abbreviated output of the data frame using dput().

data <- structure(
  list(Season = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
  player_name = c("Pecus", "Butter_Man\_", "arecue", "mattmmz123", "Jamesian", "Caenum\_", "Caenum\_", "coleam", "Karova", "Enzo\_", "LegoGuy162", "monkeykilling", "Toasty_boii", "arecue"), 
  death_time = structure(c(748, 1982, 4779, 4998, NA, NA, 3098, 3902, 4020, 4022, 4556, 4937, 5691, NA), class = c("hms", "difftime"), units = "secs"), 
  team_color = c("Green", "Purple", "Purple", "Green", "Blue", "Blue", "Green", "Blue", "Yellow", "Blue", "Green", "Red", "Red", "Yellow")),
  row.names = c(NA, \-14L), class = c("tbl_df", "tbl", "data.frame")
)

I have looked at a variety of sites and tutorials relating to Shiny, but I have thus been unable to piece together the necessary information.

Here is how my current attempt is structured in Markdown.

---
output:
  flexdashboard::flex_dashboard:
    orientation: columns
runtime: shiny
---

```{r,libraries,include=FALSE}
library(tidyverse)
library(ggplot2)
library(flexdashboard)
# Seasons

## Inputs {.sidebar data-width=250}
```{r}
seasonUI <- function(id){
  ns <- NS(id)
  fillCol(inputPanel(
    selectInput(ns("season"),"Season ", choices = data$Season),
    tableOutput(ns("seasonPlot"))
    ))
}

seasonSERVER <- function(input,output,session, ...) {
  output$seasonPlot <- renderTable({data[,input$season]})
}
seasonUI("seasons")
callModule(seasonSERVER,"seasons")
## Seasons

### Seasons

I'd greatly appreciate any and all help, thank you!


Solution

  • Besides some issues with the provided example data one issue with your code is your trial to filter your data, i.e. data[,input$season] makes no sense. Also, I'm not sure where you found the code for your shiny module. As I'm not familiar which this style I have rewritten you module using the new-style. Additionally I have split the UI server into two parts, one for the input, one for the output:

    ---
    output:
      flexdashboard::flex_dashboard:
        orientation: columns
    runtime: shiny
    ---
    
    ```{r}
    data <- structure(
      list(
        Season = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
        player_name = c("Pecus", "Butter_Man", "arecue", "mattmmz123", "Jamesian", "Caenum", "Caenum", "coleam", "Karova", "Enzo", "LegoGuy162", "monkeykilling", "Toasty_boii", "arecue"),
        death_time = structure(c(748, 1982, 4779, 4998, NA, NA, 3098, 3902, 4020, 4022, 4556, 4937, 5691, NA), class = c("hms", "difftime"), units = "secs"),
        team_color = c("Green", "Purple", "Purple", "Green", "Blue", "Blue", "Green", "Blue", "Yellow", "Blue", "Green", "Red", "Red", "Yellow")
      ),
      row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame")
    )
    ```
    
    ```{r,libraries,include=FALSE}
    library(tidyverse)
    library(shiny)
    library(flexdashboard)
    ```
    
    # Seasons
    
    ## Inputs {.sidebar data-width=250}
    
    ```{r}
    seasonUiInput <- function(id) {
      ns <- NS(id)
      fillCol(
        inputPanel(
          selectInput(ns("season"), "Season ", choices = data$Season),
        )
      )
    }
    
    seasonUiOutput <- function(id) {
      ns <- NS(id)
      fillCol(
        tableOutput(ns("seasonPlot"))
      )
    }
    
    seasonServer <- function(id) {
      moduleServer(
        id,
        function(input, output, session, ...) {
          output$seasonPlot <- renderTable({
            data[data$Season %in% input$season, ]
          })
        }
      )
    }
    ```
    
    ```{r}
    seasonUiInput("seasons")
    seasonServer("seasons")
    ```
    
    ## Seasons
    
    ### Seasons
    
    ```{r}
    seasonUiOutput("seasons")
    ```
    

    enter image description here