Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfulco/3ce273de378476aea627529a85a33dca to your computer and use it in GitHub Desktop.
Save vfulco/3ce273de378476aea627529a85a33dca to your computer and use it in GitHub Desktop.
Using Webshot to "Print" Flexdashboards
This gist holds the files necessary to reproduce a minimal example of "printing" a flexdashboard using a headless browser.
---
title: "Snapshot Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(flexdashboard)
options(digits = 1)
```
```{r libraries, message=FALSE, warning=FALSE}
# Load the required packages... you will need to ensure they are installed first.
library(dplyr); library(knitr); library(ggplot2); library(magrittr); library(shiny); library(data.table); library(DT); library(webshot)
# MAKE SURE PHANTOM JS IS INSTALLED
# webshot::install_phantomjs()
```
```{r import3, message=FALSE, warning=FALSE, include=FALSE}
data(mtcars)
```
Sidebar {.sidebar data-width=270}
=======================================================================
**Feature Selection**
```{r}
gears = mtcars$gear %>% as.factor() %>% levels()
selectizeInput("grs", "GEARS",
gears, selected = gears[1])
renderUI({
downloadButton("downloadFile", "Download")
})
```
Dashboard
=======================================================================
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### MPG Summary
```{r MPG}
mpgg = reactive({
mtcars %>% filter(gear %in% input$grs) %>% group_by(carb) %>% summarize(count = n(), `Avg HP` = mean(hp), `Avg mpg` = mean(mpg))
})
renderTable({
mpgg()
})
```
### MPG Graph
```{r Graph}
mpgp = reactive({
mtcars %>% filter(gear %in% input$grs) %>% ggplot(aes(x = hp, y = mpg)) + geom_point() + geom_smooth()
})
renderPlot({
mpgp()
})
```
```{r download}
output$downloadFile <- downloadHandler(filename = function() {
return(paste('Cars', '.png', sep=''))
},
content = function(file){
to_save <- list(
mpgg = mpgg(),
mpgp = mpgp()
)
saveRDS(to_save, "config_data.RDS")
rmarkdown::render("shadow_page.Rmd")
# file.remove("config_data.RDS")
webshot::webshot("shadow_page.html", file = file)
})
```
---
title: "Snapshot Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(flexdashboard)
options(digits = 1)
```
```{r libraries, message=FALSE, warning=FALSE}
# Load the required packages... you will need to ensure they are installed first.
library(dplyr); library(knitr); library(ggplot2); library(magrittr); library(shiny); library(data.table); library(DT); library(webshot)
# MAKE SURE PHANTOM JS IS INSTALLED
# webshot::install_phantomjs()
```
```{r import3, message=FALSE, warning=FALSE, include=FALSE}
data(mtcars)
```
Sidebar {.sidebar data-width=270}
=======================================================================
**Feature Selection**
```{r}
gears = mtcars$gear %>% as.factor() %>% levels()
selectizeInput("grs", "GEARS",
gears, selected = gears[1])
renderUI({
downloadButton("downloadFile", "Download")
})
```
Dashboard
=======================================================================
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### MPG Summary
```{r MPG}
mpgg = reactive({
mtcars %>% filter(gear %in% input$grs) %>% group_by(carb) %>% summarize(count = n(), `Avg HP` = mean(hp), `Avg mpg` = mean(mpg))
})
renderTable({
mpgg()
})
```
### MPG Graph
```{r Graph}
mpgp = reactive({
mtcars %>% filter(gear %in% input$grs) %>% ggplot(aes(x = hp, y = mpg)) + geom_point() + geom_smooth()
})
renderPlot({
mpgp()
})
```
```{r download}
output$downloadFile <- downloadHandler(filename = function() {
return(paste('Cars', '.png', sep=''))
},
content = function(file){
to_save <- list(
mpgg = mpgg(),
mpgp = mpgp()
)
saveRDS(to_save, "config_data.RDS")
rmarkdown::render("shadow_page.Rmd")
# file.remove("config_data.RDS")
webshot::webshot("shadow_page.html", file = file)
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment