forked from daattali/shinyscreenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
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
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) |
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
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) | ||
} |
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
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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'); | ||
}; |