Data Conversion

Automatic handling of data conversion to and from WebAssembly

When OJS variables are used as input or exported variables in quarto-live interactive code blocks, automatic data conversion to or from JavaScript types occurs.

Converting data types from OJS

Data conversion is handled by the WebAssembly engine transparently, and most data conversion should be reasonable1. JavaScript objects are handled by recursively converting inner properties.

foo = [123, 456];
bar = ({ x: [1, 2, 3], y: ["a", "b"], z: true });
baz = [{ a: 1 }, { b: ["x", "y", "z"], c: null }];

R data.frame conversion

When using webR powered R blocks, D3 style JavaScript objects are specially handled and converted into data.frames. For example, OJS ships with a built in dataset alphabet:

alphabet

Notice how expanding the Array output shows that this data is D3 formatted, in a row-based format.

However, when used as an input variable for a webr interactive code block, the resulting object is of class data.frame and can be used as such in the R environment. It is column based, and can be used like any other data.frame.

Source

```{webr}
#| input:
#|   - alphabet
class(alphabet)
plot(alphabet)
```

Output

Converting data types to OJS

Similar data conversion occurs when exporting variables from the WebAssembly environment to OJS. This can be used to combine R and Python processing of data with OJS or D3 data visualisation methods.

abc
def
mtcars
Plot.plot({
  x: {label: "Economy (mpg)"},
  y: {label: "Power (hp)"},
  marks: [ Plot.dot(mtcars, { x: "mpg", y: "hp"}) ]
})

Footnotes

  1. See Pyodide’s Type Conversions and webR’s Creating New R Objects documentation for the low-level details about how this works.↩︎