webr icon indicating copy to clipboard operation
webr copied to clipboard

Deno runtime support

Open arkraieski opened this issue 1 year ago • 1 comments

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!

arkraieski avatar Jun 14 '23 03:06 arkraieski

Hey,

This might have been fixed, the following code works:

  • index.ts
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/");
});
  • deno run --allow-net --allow-read --allow-env index.ts

  • curl http://localhost:3000

Hello from R!
  • deno --version
deno 1.44.4 (release, aarch64-apple-darwin)
v8 12.6.228.9
typescript 5.4.5

ColinFay avatar Jul 10 '24 14:07 ColinFay