Learn R Programming

vegalite (version 0.6.1)

capture_widget: Capture a static (png) version of a widget (e.g. for use in a PDF knitr document)

Description

Widgets are generally interactive beasts rendered in an HTML DOM with javascript. That makes them unusable in PDF documents. However, many widgets initial views would work well as static images. This function renders a widget to a file and make it usable in a number of contexts.

Usage

capture_widget(wdgt, output = c("path", "markdown", "html", "inline"), height, width, png_render_path = tempfile(fileext = ".png"))

Arguments

wdgt
htmlwidget to capture
output
how to return the results of the capture (see Details section)
height, width
it's important for many widget to be responsive in HTML documents. PDFs are static beasts and having a fixed image size works better for them. height & width will be passed into the rendering process, which means you should probably specify similar values in your widget creation process so the captured
size matches the size you specify here.
png_render_path
by default, this will be a temporary file location but a fully qualified filename (with extension) can be specified. It's up to the caller to free the storage when finished with the resource.

Value

See Details

Details

What is returned depends on the value of output. By default ("path"), the full disk path will be returned. If markdown is specified, a markdown string will be returned with a file:///... URL. If html is specified, an tag will be returned and if inline is specified, a base64 encoded tag will be returned (just like you'd see in a self-contained HTML file from knitr).

Examples

Run this code
## Not run: 
# library(webshot)
# library(vegalite)
# 
# dat <- jsonlite::fromJSON('[
#       {"a": "A","b": 28}, {"a": "B","b": 55}, {"a": "C","b": 43},
#       {"a": "D","b": 91}, {"a": "E","b": 81}, {"a": "F","b": 53},
#       {"a": "G","b": 19}, {"a": "H","b": 87}, {"a": "I","b": 52}
#     ]')
# 
# vegalite(viewport_width=350, viewport_height=250) %>%
#   add_data(dat) %>%
#   encode_x("a", "ordinal") %>%
#   encode_y("b", "quantitative") %>%
#   mark_bar() -> vl
# 
# capture_widget(vl, "inline", 250, 350)
# ## End(Not run)

Run the code above in your browser using DataLab