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)
})
}
)
```