-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhello-webR.html
63 lines (54 loc) · 1.66 KB
/
hello-webR.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webR Hello World</title>
<script type="module">
import('https://webr.r-wasm.org/latest/webr.mjs').then(async ({ WebR }) => {
const webR = new WebR();
await webR.init();
const rCode = `
x = 5 + 7
print(paste("Hello, World! x=", x))
`;
const result = await webR.evalR(rCode);
const outputText = await result.toArray();
console.log(outputText.join(''));
document.getElementById("r-output").textContent = outputText.join('');
const canvas = document.getElementById("r-canvas");
const rCode2 = `
webr::install("ggplot2")
library(ggplot2)
webr::canvas()
x <- rnorm(100)
y <- rnorm(100)
df <- data.frame(x=x, y=y)
# Plot with axes, points, and labels all in one frame
p <- ggplot(df, aes(x=x, y=y)) + geom_point()
print(p)
dev.off()
`;
await webR.evalRVoid(rCode2);
for (;;) {
const output = await webR.read();
switch (output.type) {
case 'canvas':
if (output.data.event === 'canvasImage') {
canvas.getContext('2d').drawImage(output.data.image, 0, 0);
} else if (output.data.event === 'canvasNewPage') {
console.log("Not doing anything");
}
break;
default:
console.log(output);
}
}
});
</script>
</head>
<body>
<h1>Hello World with webR</h1>
<div id="r-output"></div>
<canvas id="r-canvas" width="1008" height="1008"></canvas>
</body>
</html>