Description
Hi All,
The webr.evalR() function is not capturing the text output of R code into the result.obj property of the returned promise. While the R code executes (as confirmed by output appearing in the webr-main.ts logs), result.obj consistently returns undefined. This prevents the retrieval and display of R output within the browser.
Environment:
Browser: Chrome 133.0.6943.142
Operating System: Windows 11, Version 24H2
WebR Version: https://webr.r-wasm.org/latest/webr.mjs
HTML:
<title>WebR Test</title> <script type="module" src="script.js"></script> <textarea id="code" rows="10" placeholder="Enter R code here..."></textarea>Run
JavaScript:
import { WebR } from "https://webr.r-wasm.org/latest/webr.mjs";
let webr = null;
document.addEventListener('DOMContentLoaded', async () => {
const output = document.getElementById('output');
webr = new WebR();
await webr.init();
document.getElementById('runButton').addEventListener('click', async () => {
const code = document.getElementById('code').value;
try {
let result = await webr.evalR(code);
console.log("R result:", result);
console.log("Keys of result:", Object.keys(result));
console.log("result.obj:", result.obj);
if (result && result.obj) {
output.innerHTML = String(result.obj);
} else {
output.innerHTML = "No output";
}
} catch (err) {
console.error("R error:", err);
output.innerHTML = `Error: ${err.message}`;
}
});
});
3. Steps to Reproduce:
Create an HTML file with the provided structure.
Create a JavaScript file with the provided code.
Include the webr.mjs file from the specified CDN.
Open the HTML file in the specified browser.
Enter R code in the textarea (e.g., 1 + 1, print("test"), rnorm(10,5,1)).
Click the "Run" button.
Observe the developer console and the output area.
4. Expected Behavior:
The output of the R code should be captured in the result.obj property and displayed in the output div.
Actual Behavior:
The R code executes (as evidenced by output appearing in the webr-main.ts logs, e.g., webr-main.ts:318 [1] 2).
The result.obj property consistently returns undefined.
The output area displays "No output" or an error message if an R error occurs.
6. Additional Information:
I have tried using capture.output() and print() within the R code, but the issue persists.
I have tested with different R commands, including simple arithmetic, print() statements, and numerical data generation (rnorm()), with consistent results.
The issue appears to be related to how WebR captures the output of webr.evalR().
The toArray() function also does not return useable data.
I have tried several different versions of my javascript file, and html file.
I have tried using different browsers.
I have tested on multiple computers.
7. Console Log Output:
(Paste the console log output here, including the R result, Keys of result, and result.obj logs.)
webr-main.ts:321 WebR is using PostMessage communication channel, nested R REPLs are not available.
webr-main.ts:318 [1] 2
script.js:14 R result: Proxy(Object) {obj: {…}, payloadType: 'ptr'}
script.js:15 Keys of result: (2) ['obj', 'payloadType']
script.js:16 result.obj: undefined
script.js:18 No output
8. Request:
Please investigate this issue and provide guidance on how to correctly capture R output using webr.evalR(). If this is a bug, please provide a fix or workaround.
Thank you for your time and assistance.