Skip to contents

Add a button to take a screenshot of a specified element and download a PDF file.

Usage

capture_pdf(
  selector,
  filename,
  ...,
  margins = 15,
  scale = NULL,
  options = NULL,
  loading = NULL,
  statusInputId = NULL,
  button_class = "btn btn-default"
)

Arguments

selector

A CSS selector, for example body to target the whole page or #<ID> to target a specific output.

filename

Name of the file (without extension) that will be created. If NULL no file is downloaded client side.

...

Arguments passed to HTML button.

margins

Margins to add to PDF.

scale

Scale factor applied to image's dimension. Can be used to get a higher resolution image.

options

Options (as a list) passed to html-to-image method, for example you can use backgroundColor to set background color.

loading

Add a loading indicator if taking screenshot take time, see loading() for usage.

statusInputId

Retrieve status information in an input value server-side with following structure: list(status = "started|finished|error", timestamp = <POSIX timestamp>).

button_class

Class to use for the HTML tag <button>

Value

an HTML tag that can be used in UI or rmarkdown HTML document.

Examples

library(shiny)
library(capture)

ui <- fluidPage(
  tags$h2("Capture PDF example"),
  capture_pdf(
    selector = "body",
    filename = "all-page",
    icon("camera"), "Take screenshot of all page",
    loading = loading()
  ),
  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_pdf(
        selector = "#result-block",
        filename = "results",
        icon("camera"), "Take screenshot of results"
      ),
      capture_pdf(
        selector = "#result-block",
        filename = "results",
        scale = 3,
        icon("camera"), "Take screenshot of results (bigger scale)"
      )
    )
  )
)

server <- function(input, output, session) {
  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)