I have been trying to find a solution to this problem for quite some time. I tried appshot, but my tables and graphs show as blank.
Any help or recommendations would be very appreciated.
I have been trying to find a solution to this problem for quite some time. I tried appshot, but my tables and graphs show as blank.
Any help or recommendations would be very appreciated.
You could use html2canvas
JavaScript library to take screenshots.
For creating the prompt for download, I referenced this post: How to save img to user's local computer using HTML2canvas
And, to run arbitrary JavaScript, you need to load shinyjs
library in R.
Please see the example app below:
library(shiny)
library(shinyjs)
library(DT)
library(ggplot2)
ui <- fluidPage(
tags$head(
# include html2canvas library
tags$script(src = "http://html2canvas.hertzen.com/dist/html2canvas.min.js"),
# script for creating the prompt for download
tags$script(
"
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
"
)
),
useShinyjs(),
actionButton("screenshot","Take Screenshot"),
dataTableOutput("table"),
plotOutput("plot")
)
server <- function(input, output, session) {
observeEvent(input$screenshot,{
shinyjs::runjs(
'html2canvas(document.querySelector("body")).then(canvas => {
saveAs(canvas.toDataURL(), "shinyapp.png");
});'
)
})
output$table <- renderDataTable(iris)
output$plot <- renderPlot(ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width)))
}
shinyApp(ui, server)
You can now use a dedicated package for this called shinyscreenshot. It uses the html2canvas library under the hood, but makes it very easy to use - simply call shinyscreenshot::screenshot()
whenever you want to take a picture, and it'll download a PNG.