knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(capture)

{capure} allow to take screenshots in R Markdown documents in HTML format.

All page

Capture a screenshot of the entire page with:

capture(
  selector = "body", 
  filename = "my-document", 
  "Take screenshot", 
  button_class = "btn btn-primary btn-block"
)

If you want a PDF as output, you can use capture_pdf().

leaflet map

For example, save the map as PNG:

capture("#map-and-code", "map", "Save map and code as PNG")
capture("#only-map", "map", "Save only map as PNG")

::: {#map-and-code}

library(leaflet)

leaflet(width = "100%", elementId = "only-map") %>%
  addTiles() %>%
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")

:::

dygraph chart

Or with a {dygraph} chart:

capture("#my-chart", "chart", "Save chart as PNG")
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths, width = "100%", elementId = "my-chart")


dreamRs/capture documentation built on Oct. 29, 2024, 9:43 p.m.