Forked from agberg/ printing_flexdashboard_with_webshot
Created
April 6, 2018 13:40
-
-
Save vfulco/3ce273de378476aea627529a85a33dca to your computer and use it in GitHub Desktop.
Using Webshot to "Print" Flexdashboards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This gist holds the files necessary to reproduce a minimal example of "printing" a flexdashboard using a headless browser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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) | |
}) | |
``` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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