webR uses specific type mappings to bridge R objects and JavaScript. When working with data passed between the R environment and JavaScript, you will encounter several key data structures:
R Type Mappings
The RTypeMap defines the integer identifiers for various R types. This is useful when inspecting raw R object types.
WebRData vs WebRDataJs
WebRData: A broad union type representing data that can be converted into an R object or is the result of converting an R object to JavaScript. It includes scalars, arrays, maps, and complex objects.WebRDataJs: A structured tree format used specifically when serializing R objects into a JavaScript representation. This format is used to preserve the structure of nested R objects (like lists or environments) in a way that JavaScript can traverse.
WebRDataJs Structure
WebRDataJs objects use a type field to identify their structure:
null: { type: 'null' }string: { type: 'string', value: string }symbol: { type: 'symbol', printname: string | null, symvalue: RPtr | null, internal: RPtr | null }list | pairlist | environment: { type: '...', names: (string | null)[] | null, values: [...] }logical | integer | double | complex | character | raw: { type: '...', names: (string | null)[] | null, values: [...] }
Utility Functions
isWebRDataJs(value): Returns true if the object follows the WebRDataJs serialization format.isComplex(value): Returns true if the object is a Complex type (containing re and im properties).
// Example of checking for WebRDataJs structure
if (isWebRDataJs(myData)) {
console.log(myData.type);
console.log(myData.values);
}
// Example of checking for a complex number
const c = { re: 1, im: 2 };
if (isComplex(c)) {
console.log(c.re, c.im);
}