webR
as an alternative or an addition to shiny
to serve R code on a webpage, without having to have R installed server-side.
Here I am just running the following ultra simple function and bind it to a button so that it can be re-run by the user:
f = function() paste(sample(1:100,10),collapse=", ")
webR is starting, please wait...
Here is the code I used:
import('https://webr.r-wasm.org/latest/webr.mjs').then(async ({WebR}) => {
let grid = document.getElementById("rspace");
const webr = new WebR();
await webr.init();
//here is the R code, naturally it can also be read from a file
let func = "f=function() paste(sample(1:100,10),collapse=', ')";
await webr.evalR(func);
let str = await webr.evalR('f()')
grid.innerHTML = (await str.toJs()).values;
async function randomize() {
await webr.objs.globalEnv.bind('str', grid.innerHTML)
let str = await webr.evalR('f()')
grid.innerHTML = (await str.toJs()).values;
}
globalThis.randomize = () => {
randomize()
}
});
And the html code:
<button class="webr" onClick="globalThis.randomize();">Randomize!</button>
<div id="rspace">webR is starting, please wait...</div>