Search code examples
rshinyflexdashboard

shiny flexdashboard: reference object in new tabset


I have a flexdashboard with one column and two tabsets. I want to create and plot dat2 in one tabset, and then show the data for dat2 in a second tabset. The real use case is more complex and I don't have it setup to run from global (maybe I need to figure out how to do that).

The following results in an error that dat2 cannot be located.

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    theme: bootstrap
runtime: shiny
---

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

```{r global, include=FALSE}
  set.seed(1)
  dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
                    y = runif(100))
```

Sidebar {.sidebar}
=====================================

```{r}
# age
  sliderInput("agerange", label = "Age", 
              min = 15, 
              max = 99, 
              value = c(15, 99),
              step=10)
```

Page 1
=====================================

Column {.tabset}
-----------------------------------------------------------------------

### Plot

```{r}
renderPlot({
  dat2 <- 
  dat %>%
    filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
    mutate(y2 = y*2)

  ggplot(dat2, aes(y2)) +
    geom_histogram()
})
```


Column {.tabset}
-----------------------------------------------------------------------

### Table

```{r}
  DT::renderDataTable({
    DT::datatable(dat2, options = list(bPaginate = FALSE))
  })
```

Solution

  • This seems to work (based on this SO answer). Simpler fixes out there, or is the basic approach to do munging in reactives, assign to an object inside the reactive, and then reassign that object in a render function (e.g., object <- object())?

    ---
    title: "test"
    output: 
      flexdashboard::flex_dashboard:
        theme: bootstrap
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(tidyverse)
    ```
    
    ```{r global, include=FALSE}
      set.seed(1)
      dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
                        y = runif(100))
    ```
    
    Sidebar {.sidebar}
    =====================================
    
    ```{r}
    # age
      sliderInput("agerange", label = "Age", 
                  min = 15, 
                  max = 99, 
                  value = c(15, 99),
                  step=10)
    ```
    
    Page 1
    =====================================
    
    Column {.tabset}
    -----------------------------------------------------------------------
    
    ### Plot
    
    ```{r}
      dat2 <- reactive(
        dat %>%
        filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
        mutate(y2 = y*2)
      )
    
    renderPlot({
      dat2 <- dat2()
    
      ggplot(dat2, aes(y2)) +
        geom_histogram()
    })
    ```
    
    
    Column {.tabset}
    -----------------------------------------------------------------------
    
    ### Table
    
    ```{r}
      DT::renderDataTable({
        dat2 <- dat2()
        DT::datatable(dat2, options = list(bPaginate = FALSE))
      })
    ```