generated from hrbrmstr/slightly-more-than-minimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r.js
36 lines (32 loc) · 991 Bytes
/
r.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { WebR } from './webr.mjs';
import {getenumci} from './getenumci.js';
// this can be accessed everywhere as "webR"
globalThis.webR = new WebR();
await globalThis.webR.init();
globalThis.webRCodeShelter = await new globalThis.webR.Shelter();
console.log("Installing dplyr and readr");
await globalThis.webR.installPackages(['dplyr', 'readr'])
console.log("Done installing packages");
// load getenumCI function
webR.evalR(getenumci);
/**
* This is a [Tag Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)
*
* @param {strings[]} strings
* @param {...any} values
* @returns a WebR toJs() object
* @example
* await R`sample(100, 5)`
*/
globalThis.R = async function R(strings, ...values) {
let result = "";
for (let i = 0; i < strings.length; i++) {
result += strings[ i ];
if (i < values.length) {
result += values[ i ];
}
}
return Promise.resolve(
await (await webR.evalR(result)).toJs()
)
}