R API

The webR R API is provided via a pre-installed webr support package.

Graphics device for drawing to a HTML canvas element

Description

A graphics device that generates HTML canvas API calls and executes them on the worker thread using a JavaScript OffscreenCanvas. Throughout plotting, and if capture is FALSE, JavaScript ImageBitmap objects are transmitted to the main webR thread for display.

Usage

canvas(
  width = 504,
  height = 504,
  pointsize = 12,
  bg = "transparent",
  capture = FALSE,
  ...
)

Arguments

width

The width of the device.

height

The height of the device.

pointsize

The default point size of plotted text.

bg

The initial background colour.

capture

If TRUE, store OffscreenCanvas elements in the cache.

Additional graphics device arguments (ignored).

Details

The resulting webR output messages are of type CanvasMessage, with the type property set as ‘canvas’ and the data property populated with further details about the event that triggered the message.

When the graphics device creates a new page an output message is emitted with the data property set to { event: ‘canvasNewPage’ }.

When bitmap image data is sent to the main thread for display, a message is emitted with the the bitmap additionally included as part of the data property, <U+2060>{ event: ‘canvasImage’, image: ImageBitmap }<U+2060>.

When capture is TRUE, the resulting OffscreenCanvas elements are stored in the canvas cache. The captured plots are then retrieved by eval_r().

A 2x scaling is used to improve the bitmap output visual quality. As such, the width and height of the HTML canvas element should be twice the width and height of the graphics device.

Based on the R canvas package by Jeffrey Horner, released under the GPL v2 Licence.

Value

A function with no arguments. When called returns an integer vector of canvas cache IDs for OffscreenCanvas elements captured by this device.

Interact with the OffscreenCanvas cache

Description

canvas_cache() returns an integer vector containing the current canvas cache IDs.

canvas_destroy() destroys the cached OffscreenCanvas elements with the given IDs.

canvas_purge() destroys all OffscreenCanvas elements in the cache.

Usage

canvas_cache()

canvas_destroy(ids)

canvas_purge()

Arguments

ids

Integer vector of canvas cache IDs.

Use the webR canvas graphics device

Description

Set R options so that the webR canvas graphics device is used as the default graphics device for new plots.

Usage

canvas_install(...)

Arguments

Arguments to be passed to the graphics device.

Evaluate JavaScript code

Description

This function evaluates the given character string as JavaScript code. The result is returned as an integer.

Usage

eval_js(code)

Arguments

code

The JavaScript code to evaluate.

Details

The JavaScript code is evaluated using emscripten_run_script_int from the Emscripten C API. In the event of a JavaScript exception an R error condition will be raised with the exception message.

This is an experimental function that may undergo a breaking change in the future so as to support different return types.

Value

Integer result of evaluating the code.

Evaluate the provided R code, call, or expression for webR

Description

This function evaluates the provided R code, call, or expression with various settings in place to configure behavior. The function is intended to be used by the webR evalR API, rather than invoked directly by the end user.

Usage

eval_r(
  expr,
  conditions = TRUE,
  streams = FALSE,
  autoprint = FALSE,
  handlers = TRUE,
  env = parent.frame()
)

Arguments

expr

The R code, call or expression to evaluate.

conditions

If TRUE, capture and return conditions raised during execution.

streams

If TRUE, capture and return the stdout and stderr streams.

autoprint

If TRUE, automatically print as if the expression was written at an R console.

handlers

If TRUE, execute using a tryCatch with handlers in place.

env

The environment in which to evaluate.

Install a global handler to automatically download missing packages

Description

When enabled, packageNotFoundError errors invoke a global handler to download missing R packages from the default webR binary repo.

If it is possible to do so, execution will continue without interrupting the current program.

Usage

global_prompt_install()

Install one or more packages from a webR binary package repo

Description

Install one or more packages from a webR binary package repo

Usage

install(
  packages,
  repos = NULL,
  info = NULL,
  lib = NULL,
  quiet = FALSE,
  mount = TRUE
)

Arguments

packages

Character vector containing the names of packages to install.

repos

Character vector containing the URIs of the webR repos to use.

info

A character matrix as from available.packages(). Can be used as an alternative to looking up available packages with the repos argument.

lib

The library directory where the packages will be installed.

quiet

Logical. If TRUE, do not output downloading messages.

mount

Logical. If TRUE, download and mount packages using Emscripten filesystem images.

Install missing packages from the default webR binary repo

Description

When loading R packages using library_shim() or require_shim(), if a package is missing an attempt will be made to download and install the missing package from the default webR binary repo.

Once downloaded, the original base::library() or base::require() command is invoked to load the package.

Usage

library_shim(pkg, ..., show_menu = getOption("webr.show_menu"))

require_shim(pkg, ..., show_menu = getOption("webr.show_menu"))

Arguments

show_menu

Show a menu asking the user if they would like to install the package if it is missing. Defaults to getOption(“webr.show_menu”).

Details

A menu will be shown to the user when the webr.show_menu global option is set to TRUE. Otherwise, the user will not be prompted.

When the menu is enabled, loading a missing package will cause a prompt to be shown to the user asking if they would like to download the missing package. If the user replies in the affirmative, the package is downloaded using install().

The webr.show_menu global option may be overridden by providing the show_menu argument. By default, if no global option is set and no argument is provided, the menu will not be shown.

Mount an Emscripten filesystem object

Description

Uses the Emscripten filesystem API to mount a filesystem object onto a given directory in the virtual filesystem. The mountpoint will be created if it does not already exist.

When mounting an Emscripten “workerfs” type filesystem the source should be the URL for a filesystem image with filename ending .data, as produced by Emscripten’s file_packager tool. The filesystem image and metadata will be downloaded and mounted onto the directory mountpoint.

When mounting an Emscripten “nodefs” type filesystem, the source should be the path to a physical directory on the host filesystem. The host directory will be mapped into the virtual filesystem and mounted onto the directory mountpoint. This filesystem type can only be used when webR is running under Node.

Usage

mount(mountpoint, source, type = "workerfs")

unmount(mountpoint)

Arguments

mountpoint

a character string giving the path to a directory to mount onto in the Emscripten virtual filesystem.

source

a character string giving the location of the data source to be mounted.

type

a character string giving the type of Emscripten filesystem to be mounted: “workerfs” or “nodefs”.

Generate an output message when file is displayed with the pager

Description

When enabled, the R pager function is replaced so that a request to display a file’s contents instead generates a webR output message. The request is forwarded to the main thread to be handled by the application loading webR.

This replaces the default R pager mechanism, which tries to start an external pager binary using the system() function.

Usage

pager_install()

Shim functions from base R

Description

Replace base R functions with implementations that work in the webR environment.

Usage

shim_install()

Details

The shimmed functions are:

Test an installed R package by running the package examples and tests

Description

This function runs R package examples and tests. The implementation is based on tools::testInstalledPackage(), with modifications for webR where the system() function cannot be used.

Usage

test_package(pkg)

Arguments

pkg

Name of the package to test.

Value

0 if the test was successful, otherwise 1.