I want to embed an external shiny app in another vanilla shiny app and run this app including the nested / embedded app on my local machine.
In RMarkdown this is well documented and can be easily done by using shinyAppDir()
in an R chunk within a flexdashboard.
My question is: How can this be done in a pure vanilla shiny context (without relying on RMarkdown and flexdashboard)? I gave it a first try with putting shinyAppDir
in a renderUI
statement, but that is not working (which makes sense, since the app is not only made of UI, but also contains server logic).
Here is a working example of an embedded shiny app in a flexdashboard:
This is a simple shiny app I want to embed:
library(shiny)
library(dplyr)
shinyApp(ui = fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
textInput("min_e", "min eruptions", min(faithful$eruptions)),
textInput("max_e", "max eruptions", max(faithful$eruptions)),
actionButton(inputId = "OK", label = "OK")
),
mainPanel( plotOutput("distPlot1")
)
)
),
server = function(input, output) {
nbins = reactive({input$OK
isolate(input$bins)})
faithful_filtered = reactive({input$OK
faithful %>% filter(eruptions >= isolate(input$min_e),
eruptions <= isolate(input$max_e))
})
output$distPlot1 <- renderPlot({
x <- faithful_filtered()[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = nbins() + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
)
And it can be embedded in a flexdashboard like this (if you run this code make sure to first save the app above (as app.R) and enter the correct file path in shinyAppDir()
).
---
title: "embedded shiny"
output:
flexdashboard::flex_dashboard:
orientation: column
runtime: shiny
---
```{r, include=FALSE, message=FALSE, context="setup"}
library(flexdashboard)
library(shiny)
```
Input {.sidebar data-width=300}
-------------------------------------
```{r, echo=FALSE, context="render"}
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
actionButton(inputId = "OK", label = "OK")
```
```{r, context="server"}
```
Row
-----------------------------------------------------------------------
### Some plot here
```{r, context="server"}
```
```{r, echo=FALSE}
```
### Another plot here
```{r, context="server"}
```
```{r, echo=FALSE}
```
### Embeded app here
```{r, echo=FALSE}
shinyAppDir(
file.path("file_path_goes_here"), # enter valid file path here
options=list(
width="100%", height=700
)
)
```