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. Once drawing has finished, a JavaScript ImageBitmap object is transmitted to the main webR thread for display.

Usage

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

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.

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, { event: ‘canvasImage’, image: ImageBitmap }.

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.

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 R code for webR

Description

This function evaluates the provided R code 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(
  code,
  conditions = TRUE,
  streams = FALSE,
  autoprint = FALSE,
  handlers = TRUE,
  env = parent.frame()
)

Arguments

code

The R code 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, code automatically prints as if it were written at an R console.

handlers

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

env

The environment in which to evaluate.

Prompt user to install missing packages from a webR binary repo

Description

When enabled, packageNotFoundError errors cause a menu prompt to be shown to the user asking if they would like to attempt to download the missing package.

If the user replies in the negative, the package error is thrown as it would normally. If the user replies in the affirmative, the package is downloaded using install().

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, lib = NULL, quiet = FALSE)

Arguments

packages

Character vector containing the names of packages to install.

repos

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

lib

The library directory where the packages will be installed.

quiet

Logical. If TRUE, do not output downloading messages.

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()

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.