r-wasm / webr

The statistical language R compiled to WebAssembly via Emscripten, for use in web browsers and Node.
https://docs.r-wasm.org/webr/latest/
Other
841 stars 65 forks source link

Deno runtime support #227

Open arkraieski opened 1 year ago

arkraieski commented 1 year ago

Hi, last night I was playing around with some scripts trying to get WebR to run in Deno, and unfortunately, it doesn't seem to work currently.

I'm not sure if anyone else is interested in this, but I figured it would be worthwhile to at least start the discussion (if it hasn't already been started) and discuss potential blockers. I'm interested in using Deno as novel way of deploying backend R and integrating with serverless web apps and apis, etc.

When I try importing webR from npm in Deno with the syntax import {WebR} from "npm:@r-wasm/webr" I got a nasty error trying to call new WebR():

error: Uncaught RangeError: Maximum call stack size exceeded
    at RegExp.[Symbol.replace] (<anonymous>)
    at String.replaceAll (<anonymous>)
    at encodeWhitespace (ext:deno_node/path/_util.ts:94:19)
    at toFileUrl (ext:deno_node/path/_posix.ts:431:20)
    at new _Worker (ext:deno_node/worker_threads.ts:29:36)

I'm not really sure what all that means, but I got the same error even when running the script with the -A flag which grants all security permissions.

Downloading WebR and importing from CDN as described in the WebR documentation results in a different error (seemingly from src/webR/compat.ts?) when I get to running webR.evalR():

error: Uncaught Error: Cannot determine runtime environment
  throw new Error("Cannot determine runtime environment");

Is Deno compatibility/support something that's been discussed? Are there any known blockers that need to be addressed?

Thanks!

ColinFay commented 2 months ago

Hey,

This might have been fixed, the following code works:

import express from "express";
import { WebR } from "webr";

const app = express();

const webR = new WebR();
await webR.init();

app.get("/", async (req, res) => {
  let result = await webR.evalRString('tools::toTitleCase("hello from r!")') as String;
  res.send(result);
});

app.listen(3000, () => {
  console.log("http://localhost:3000/");
});
Hello from R!
deno 1.44.4 (release, aarch64-apple-darwin)
v8 12.6.228.9
typescript 5.4.5