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

Description Usage Arguments Details Value Examples

View source: R/phantom.r

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

1
2
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 <div> 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.

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 <img src='file:///...'/> tag will be returned and if inline is specified, a base64 encoded <img> tag will be returned (just like you'd see in a self-contained HTML file from knitr).

Value

See Details

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## 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)

vegalite documentation built on May 2, 2019, 10:46 a.m.