Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
daattali committed Dec 15, 2018
1 parent ecdb905 commit 6ee61c7
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
32 changes: 32 additions & 0 deletions app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# https://html2canvas.hertzen.com
# Limitations: browser support see, some CSS properties (which I think is the reason why leaflet shows up as just gray), more info at https://html2canvas.hertzen.com/documentation
# Also, images that have a large size (I haven't tested, but it seems when the canvas blobk is over 1 or 2 GB for me) it fails due to memory issues.

library(shiny)

source("capture.R")

ui <- fluidPage(
textInput("text", "Selector to capture", "#plot"),
textInput("name", "name", "image.png"),
actionButton("screenshot", "Screenshot"),
plotOutput("plot"),
plotly::plotlyOutput("plotly"),
numericInput("num", "Number", 10)
)

server <- function(input, output, session) {
output$plot <- renderPlot({
plot(seq(input$num))
})

output$plotly <- plotly::renderPlotly({
plotly::plot_ly(iris, x = ~Sepal.Width, y = ~Sepal.Length)
})

observeEvent(input$screenshot, {
docapture(selector = input$text, filename = input$name)
})
}

shinyApp(ui, server)
39 changes: 39 additions & 0 deletions capture.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

docapture <- function(id = "", selector = "body", filename = "screenshot.png") {
setupcapture()

if (nzchar(id)) {
selector <- paste0("#", id)
}

session <- getSession()
session$sendCustomMessage("screenshot", list(selector = selector, filename = filename))
}

getSession <- function() {
session <- shiny::getDefaultReactiveDomain()

if (is.null(session)) {
stop("Could not find a Shiny session.")
}

session
}

setupcapture <- function() {
session <- getSession()

if (!is.null(attr(session, "shinyshot_attached"))) {
return()
}
attr(session, "shinyshot_attached") <- TRUE

shiny::addResourcePath("shinyscreenshot", "~/../R/shinyscreenshot/www/")

shiny::insertUI("head", "beforeEnd", {
shiny::singleton(shiny::tags$head(
shiny::tags$script(src = "shinyscreenshot/saveFile.js"),
shiny::tags$script(src = "shinyscreenshot/html2canvas.min.js")
))
}, immediate = TRUE)
}
16 changes: 16 additions & 0 deletions shinyscreenshot.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
6 changes: 6 additions & 0 deletions www/html2canvas.min.js

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions www/saveFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Shiny.addCustomMessageHandler('screenshot', function(params) {
html2canvas($(params.selector)[0]).then(function(canvas) {
img = canvas.toDataURL();
saveFile(img, params.filename);
});
});

// heavily borrowed from plotly.js
var saveFile = function(url, filename) {
var saveLink = document.createElement('a');
var canUseSaveLink = 'download' in saveLink;
var isSafari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent);
// IE <10 is explicitly unsupported
if (typeof navigator !== 'undefined' && /MSIE [1-9]\./.test(navigator.userAgent)) {
alert('Download not supported in IE < 10');
}

// First try a.download, then web filesystem, then object URLs
if (isSafari) {
// Safari doesn't allow downloading of blob urls
document.location.href = 'data:application/octet-stream' + url.slice(url.search(/[,;]/));
return;
}

if (canUseSaveLink) {
saveLink.href = url;
saveLink.download = filename;
document.body.appendChild(saveLink);
saveLink.click();
document.body.removeChild(saveLink);
return;
}

// IE 10+ (native saveAs)
if(typeof navigator !== 'undefined' && navigator.msSaveBlob) {
// At this point we are only dealing with a SVG encoded as
// a data URL (since IE only supports SVG)
var encoded = url.split(/^data:image\/svg\+xml,/)[1];
var svg = decodeURIComponent(encoded);
navigator.msSaveBlob(new Blob([svg]), filename);
return;
}

alert('Download error');
};

0 comments on commit 6ee61c7

Please sign in to comment.