capture | R Documentation |
Add a button to take a screenshot of a specified element and download a PNG file.
capture(
selector,
filename,
...,
format = c("png", "jpeg"),
scale = NULL,
inputId = NULL,
options = NULL,
loading = NULL,
statusInputId = NULL,
button_class = "btn btn-default"
)
selector |
A CSS selector, for example |
filename |
Name of the file (without extension) that will be created. If |
... |
Arguments passed to HTML button. |
format |
Format of output between: |
scale |
Scale factor applied to image's dimension. Can be used to get a higher resolution image. |
inputId |
An |
options |
Options (as a list) passed to html-to-image
method, for example you can use |
loading |
Add a loading indicator if taking screenshot take time, see |
statusInputId |
Retrieve status information in an |
button_class |
Class to use for the HTML tag |
an HTML tag that can be used in UI or rmarkdown HTML document.
It's only possible to take screenshot of elements that are actually visible on screen. It doesn't work in Internet Explorer.
library(shiny)
library(capture)
ui <- fluidPage(
tags$h2("Capture example"),
capture(
selector = "body",
filename = "all-page",
icon("camera"), "Take screenshot of all page",
format = "png"
),
tags$br(),
fluidRow(
column(
width = 4,
wellPanel(
tags$b("Parameters :"),
selectInput(
inputId = "loi",
label = "Law:",
choices = c("normal", "uniform", "exponential")
)
)
),
column(
width = 8,
tags$div(
id = "result-block",
tags$b("Results :"),
plotOutput(outputId = "plot"),
uiOutput(outputId = "mean"),
verbatimTextOutput(outputId = "raw")
),
capture(
selector = "#result-block",
filename = "results-screenshot",
icon("camera"), "Take screenshot of results",
options = list(backgroundColor = "#FFF")
),
capture(
selector = "#result-block",
filename = "results-screenshot",
icon("camera"), "Take screenshot of results (bigger scale)",
scale = 3,
options = list(backgroundColor = "#FFF")
),
capture(
selector = "#result-block",
filename = NULL, # no download client side
icon("camera"), "Take screenshot of results (retrieve server side)",
inputId = "screenshot",
options = list(backgroundColor = "#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,
"normal" = rnorm(1000),
"uniform" = runif(1000),
"exponential" = rexp(1000)
)
})
output$plot <- renderPlot({
hist(distrib_r())
})
output$mean <- renderUI({
tags$p(tags$b("The mean is :"), round(mean(distrib_r()), 2))
})
output$raw <- renderPrint({
summary(distrib_r())
})
}
if (interactive())
shinyApp(ui, server)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.