Share data across ShinyApp code blocks within a session

I have an RMarkdown document with several ShinyApp code blocks. I would like one of the apps to set variables which can then be used in the other apps further down the page. I'm running it on Shiny Server Open Source. Is this possible?

Here is a minimal version, where a user makes a dataset (session$userData$data) in app1 and use it in app2. Information in session$userData seems not to carry over from one block/app. It's NULL in app2. I've also (blindly) tried the parent trick described here but no luck. Also, setting in the global environment using <<- makes user sessions interfere with each other, so that's not an option either.

Maybe I need to use a different architecture to do this? There's going to be a lot of RMarkdown stuff between the code blocks, so putting all that markdown into intermittent shiny div, p, etc. between ui elements is not an option.

# FIRST SHINYAPP
Please select your dataset:

```{r, echo=FALSE}
shinyApp(
  # A selector and a plot placeholder
  ui = fluidPage(
    selectInput('dataset', label = 'Choose:', choices = list('A'='A', 'B'='B')),
    plotOutput('data_plot')
  ),
  
  # Set session variables and plot them.
  server = function(input, output, session) {
    output$data_plot = renderPlot({
      session$userData$dataset = input$dataset
      session$userData$data = rnorm(1000)  # Would normally depend on dataset, but I'm keeping it simple
      hist(session$userData$data)
    })
  }
)
```

#SECOND SHINYAPP
Bla bla bla bla. Now add a curve to the data you selected above:

```{r, echo=FALSE}
shinyApp(
  # A slider and a plot placeholder
  ui = fluidPage(
    sliderInput("mu", label = 'Mean', min = 0, max = 3, value = 1),
    plotOutput('curve_plot')
  ),
  
  # Use the data stored in the session to plot a curve.
  server = function(input, output, session) {
    output$curve_plot = renderPlot({
      hist(session$userData$data)  # NULL
      curve(dnorm(x, mean=input$mu), add=T)
    })
  }
)
```

Have you considered just making a Shiny RMarkdown doc (rather than embedding shinyApps inside an doc)? See https://bookdown.org/yihui/rmarkdown/shiny-documents.html for more details.

Thanks, @hadley, you put me on the right track. Dropping the shinyApp, I can use session$userData to hold data across code blocks within a session.

The updated code is:

Please select your dataset:

```{r, echo=FALSE}
selectInput('dataset', label = 'Choose:', choices = list('A'='A', 'B'='B'))
plotOutput('data_plot')
  
renderPlot({
  session$userData$dataset = input$dataset
  session$userData$data = rnorm(1000, 0)  # Would normally depend on dataset, but I'm keeping it simple
  hist(session$userData$data, freq=F)
})
```


Bla bla bla bla. Now add a curve to the data you selected above:

```{r, echo=FALSE}
sliderInput("mu", label = 'Mean', min = 0, max = 3, value = 1)
renderPlot({
  hist(session$userData$data, freq=F)
  curve(dnorm(x, mean=input$mu), add=T)
})
```

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.