Skip to content

Commit

Permalink
capture: added inputId argument to allow retrieving image as base64 s…
Browse files Browse the repository at this point in the history
…tring server-side
  • Loading branch information
pvictor committed Dec 21, 2021
1 parent 0d1a1b4 commit 4aab9e5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# capture 0.1.1

* `capture()` / `capture_pdf()`: added `scale` argument, to be applied to image dimensions.
* `capture()`: added `inputId` argument to allow retrieving image as base64 string server-side.


# capture 0.1.0

Expand Down
23 changes: 13 additions & 10 deletions R/capture.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#' @description Add a button to take a screenshot of a specified element and download a PNG file.
#'
#' @param selector A CSS selector, for example `body` to target the whole page or `#<ID>` to target a specific output.
#' @param filename Name of the file (without extension) that will be created.
#' @param filename Name of the file (without extension) that will be created. If `NULL` no file is downloaded client side.
#' @param ... Arguments passed to HTML button.
#' @param format Format of output between: `"png"` or `"jpeg"`.
#' @param scale Scale factor applied to image's dimension.
#' @param scale Scale factor applied to image's dimension. Can be used to get a higher resolution image.
#' @param inputId An `inputId` to retrieve image as base64 in an `input` slot server-side.
#' @param options Options (as a list) passed to [domtoimage](https://github.com/tsayen/dom-to-image)
#' method, for example you can use `bgcolor` to set background color.
#'
Expand All @@ -21,23 +22,25 @@
#' @importFrom jsonlite toJSON
#'
#' @example examples/default.R
capture <- function(selector, filename, ..., format = c("png", "jpeg"), scale = NULL, options = list()) {
capture <- function(selector,
filename,
...,
format = c("png", "jpeg"),
scale = NULL,
inputId = NULL,
options = NULL) {
format <- match.arg(format)
ext <- tools::file_ext(filename)
if (identical(ext, ""))
filename <- paste0(filename, ".", format)
if (length(options) < 1) {
options <- "{}"
} else {
options <- toJSON(x = options, auto_unbox = TRUE)
}
tagList(
tags$button(
class = "btn btn-default btn-capture btn-capture-screenshot",
`data-selector` = selector,
`data-filename` = filename,
`data-options` = options,
`data-scale` = if (!is.null(scale)) scale,
`data-options` = toJSON(x = options, auto_unbox = TRUE),
`data-scale` = scale,
`data-inputId` = inputId,
...
),
html_dependency_filesaver(),
Expand Down
25 changes: 24 additions & 1 deletion examples/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,35 @@ ui <- fluidPage(
icon("camera"), "Take screenshot of results (bigger scale)",
scale = 3,
options = list(bgcolor = "#FFF")
)
),
capture(
selector = "#result-block",
filename = NULL, # no download client side
icon("camera"), "Take screenshot of results (retrieve server side)",
inputId = "screenshot",
options = list(bgcolor = "#FFF")
),
uiOutput("out")
)
)
)

server <- function(input, output, session) {

output$out <- renderUI({
# # Here we display image back in interface,
# # but you can also write image on disk
# write_png <- function(x, filename) {
# x <- sub(".*,", "", x)
# x <- base64enc::base64decode(x)
# png::writePNG(png::readPNG(x), filename)
# }
# write_png(input$screenshot, "myimage.png")

tags$img(src = input$screenshot)
})


distrib_r <- reactive({
switch(
input$loi,
Expand Down
13 changes: 12 additions & 1 deletion inst/assets/js/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
var toCapture = el.getAttribute("data-selector");
var node = document.querySelector(toCapture);
var fileName = el.getAttribute("data-filename");
var inputId = el.getAttribute("data-inputId");
var scale = parseInt(el.getAttribute("data-scale"));
var options = el.getAttribute("data-options");
options = JSON.parse(options);
Expand All @@ -70,7 +71,17 @@
domtoimage
.toBlob(node, options)
.then(function(blob) {
window.saveAs(blob, fileName);
if (fileName !== null) {
window.saveAs(blob, fileName);
}
if (inputId !== null) {
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
Shiny.setInputValue(inputId, base64data);
}
}
el.classList.remove("disabled");
})
.catch(function(error) {
Expand Down
34 changes: 30 additions & 4 deletions man/capture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/capture_pdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4aab9e5

Please sign in to comment.