Search code examples
rshinyflexdashboard

r flexdashboard nested selectInput


  1. My first selectInput selects groups from dataframe mydf$grp
  2. Then I use this input to create reactive dataframe with a group value selected in step 1.
  3. Then I try to use this reactive data frame to define my choices for next selectInput

I would like to render gt by default with default values mentioned in select option (i.e. 'Group 1' and 'Val1')

I tried following the guidance mentioned here but getting errors. Create reactive selectInput - flexdashboard with Shiny

Following is my code using flexdashboard with runtime:shiny

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
   vertical_layout: fill 
   theme: 
      version: 4
      bootswatch: minty
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(gt)
```

Row {data-height=650}
-----------------------------------------------------------------------

### Chart A

```{r}

mydf = data.frame(val=c("Val1", "Val2", "Val3", "Val4", "Val5", "Val6"), 
                  grp=c("Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 1"),
                  text1=c("abc", "xyz", "pqr", "lmn", "zxcv", "fgf"),
                  text2=c("ABC", "XYZ", "PQR", "LMN", "ZXCV", "FGF"))

selectInput("Group", label = "Group", choices = mydf$grp, selected = mydf$grp[1])

g.choices = reactive({
  if (input$Group=="Group 1"){
     mydf %>% filter(grp=="Group 1")
  }else{
      mydf %>% 
          filter(grp==input$Group)}
})

renderUI({selectInput("Value", label = "Value",
        choices = g.choices()$val, selected = "Val1")})

```

Outputs {data-width=750}
-----------------------------------------------------------------------

```{r}

render_gt({
    mydf %>% 
      filter(val==input$Value)
      gt() 
      
})

```

Solution

  • There are several issues with your code. First, you haven't loaded dplyr. Second, in render_gt you missed to pipe the data into gt(). Besides that there are some minor issues, e.g. for the choices argument I would recommend to use unique() instead of passing a whole column.

    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
       vertical_layout: fill 
       theme: 
          version: 4
          bootswatch: minty
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(gt)
    library(dplyr)
    ```
    
    Row {data-height=650}
    -----------------------------------------------------------------------
    
    ### Chart A
    
    ```{r}
    mydf <- data.frame(
      val = c("Val1", "Val2", "Val3", "Val4", "Val5", "Val6"),
      grp = c("Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 1"),
      text1 = c("abc", "xyz", "pqr", "lmn", "zxcv", "fgf"),
      text2 = c("ABC", "XYZ", "PQR", "LMN", "ZXCV", "FGF")
    )
    
    selectInput("Group", label = "Group", choices = unique(mydf$grp), selected = mydf$grp[1])
    
    g.choices <- reactive({
      mydf %>%
        filter(grp %in% input$Group)
    })
    
    renderUI({
      selectInput("Value",
        label = "Value",
        choices = unique(g.choices()$val)
      )
    })
    ```
    
    Outputs {data-width=750}
    -----------------------------------------------------------------------
    
    ```{r}
    render_gt({
      mydf %>%
        filter(val %in% input$Value, grp %in% input$Group) %>%
        gt()
    })
    ```
    

    enter image description here