Skip to content

Commit f71cd7f

Browse files
authored
Switch webR from v0.2.2 to use v0.3.1 (#169)
* Switch webR from v0.2.2 to use v0.3.1 * Fully switch to using webR v0.3.x's new `.images`
1 parent 891f803 commit f71cd7f

6 files changed

+45
-43
lines changed

_extensions/webr/_extension.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: webr
22
title: Embedded webr code cells
33
author: James Joseph Balamuta
4-
version: 0.4.1-dev.1
4+
version: 0.4.1-dev.2
55
quarto-required: ">=1.2.198"
66
contributes:
77
filters:

_extensions/webr/qwebr-compute-engine.js

+39-38
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ globalThis.qwebrComputeEngine = async function(
6464
// 1. We setup a canvas device to write to by making a namespace call into the {webr} package
6565
// 2. We use values inside of the options array to set the figure size.
6666
// 3. We capture the output stream information (STDOUT and STERR)
67-
// 4. While parsing the results, we disable image creation.
68-
69-
// Create a canvas variable for graphics
70-
let canvas = undefined;
67+
// 4. We disable the current device's image creation.
68+
// 5. Piece-wise parse the results into the different output areas
7169

7270
// Create a pager variable for help/file contents
7371
let pager = [];
@@ -92,9 +90,14 @@ globalThis.qwebrComputeEngine = async function(
9290
await mainWebR.init();
9391

9492
// Setup a webR canvas by making a namespace call into the {webr} package
95-
await mainWebR.evalRVoid(`webr::canvas(width=${fig_width}, height=${fig_height})`);
96-
97-
const result = await mainWebRCodeShelter.captureR(codeToRun, {
93+
// Evaluate the R code
94+
// Remove the active canvas silently
95+
const result = await mainWebRCodeShelter.captureR(
96+
`webr::canvas(width=${fig_width}, height=${fig_height}, capture = TRUE)
97+
.webr_cvs_id <- dev.cur()
98+
${codeToRun}
99+
invisible(dev.off(.webr_cvs_id))
100+
`, {
98101
withAutoprint: true,
99102
captureStreams: true,
100103
captureConditions: false//,
@@ -105,9 +108,6 @@ globalThis.qwebrComputeEngine = async function(
105108

106109
// Start attempting to parse the result data
107110
processResultOutput:try {
108-
109-
// Stop creating images
110-
await mainWebR.evalRVoid("dev.off()");
111111

112112
// Avoid running through output processing
113113
if (options.results === "hide" || options.output === "false") {
@@ -130,34 +130,11 @@ globalThis.qwebrComputeEngine = async function(
130130

131131

132132
// Clean the state
133-
// We're now able to process both graphics and pager events.
133+
// We're now able to process pager events.
134134
// As a result, we cannot maintain a true 1-to-1 output order
135135
// without individually feeding each line
136136
const msgs = await mainWebR.flush();
137137

138-
// Output each image event stored
139-
msgs.forEach((msg) => {
140-
// Determine if old canvas can be used or a new canvas is required.
141-
if (msg.type === 'canvas'){
142-
// Add image to the current canvas
143-
if (msg.data.event === 'canvasImage') {
144-
canvas.getContext('2d').drawImage(msg.data.image, 0, 0);
145-
} else if (msg.data.event === 'canvasNewPage') {
146-
147-
// Generate a new canvas element
148-
canvas = document.createElement("canvas");
149-
canvas.setAttribute("width", 2 * fig_width);
150-
canvas.setAttribute("height", 2 * fig_height);
151-
canvas.style.width = options["out-width"] ? options["out-width"] : `${fig_width}px`;
152-
if (options["out-height"]) {
153-
canvas.style.height = options["out-height"];
154-
}
155-
canvas.style.display = "block";
156-
canvas.style.margin = "auto";
157-
}
158-
}
159-
});
160-
161138
// Use `map` to process the filtered "pager" events asynchronously
162139
const pager = await Promise.all(
163140
msgs.filter(msg => msg.type === 'pager').map(
@@ -185,13 +162,37 @@ globalThis.qwebrComputeEngine = async function(
185162

186163
elements.outputCodeDiv.appendChild(pre);
187164

188-
// Place the graphics on the canvas
189-
if (canvas) {
165+
// Determine if we have graphs to display
166+
if (result.images.length > 0) {
190167
// Create figure element
191168
const figureElement = document.createElement('figure');
192169

193-
// Append canvas to figure
194-
figureElement.appendChild(canvas);
170+
// Place each rendered graphic onto a canvas element
171+
result.images.forEach((img) => {
172+
// Construct canvas for object
173+
const canvas = document.createElement("canvas");
174+
175+
// Set canvas size to image
176+
canvas.width = img.width;
177+
canvas.height = img.height;
178+
179+
// Apply output truncations
180+
canvas.style.width = options["out-width"] ? options["out-width"] : `${fig_width}px`;
181+
if (options["out-height"]) {
182+
canvas.style.height = options["out-height"];
183+
}
184+
185+
// Apply styling
186+
canvas.style.display = "block";
187+
canvas.style.margin = "auto";
188+
189+
// Draw image onto Canvas
190+
const ctx = canvas.getContext("2d");
191+
ctx.drawImage(img, 0, 0, img.width, img.height);
192+
193+
// Append canvas to figure output area
194+
figureElement.appendChild(canvas);
195+
});
195196

196197
if (options['fig-cap']) {
197198
// Create figcaption element
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
importScripts('https://webr.r-wasm.org/v0.2.2/webr-serviceworker.js');
1+
importScripts('https://webr.r-wasm.org/v0.3.1/webr-serviceworker.js');

_extensions/webr/webr-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
importScripts('https://webr.r-wasm.org/v0.2.2/webr-worker.js');
1+
importScripts('https://webr.r-wasm.org/v0.3.1/webr-worker.js');

_extensions/webr/webr.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local hasDoneWebRSetup = false
1212
-- https://docs.r-wasm.org/webr/latest/api/js/interfaces/WebR.WebROptions.html
1313

1414
-- Define a base compatibile version
15-
local baseVersionWebR = "0.2.2"
15+
local baseVersionWebR = "0.3.1"
1616

1717
-- Define where WebR can be found
1818
local baseUrl = "https://webr.r-wasm.org/v".. baseVersionWebR .."/"

docs/qwebr-release-notes.qmd

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ format:
99
---
1010

1111

12-
# 0.4.x-dev.1: ???????????? (??-??-??) [DEV]
12+
# 0.4.1-dev.2: ???????????? (??-??-??) [DEV]
1313

1414
## Features
1515

16+
- Upgraded the embedded version of webR to v0.3.1. ([#165](https://github.com/coatless/quarto-webr/issues/165))
1617
- `read-only` is a new code cell option that prevents changes to code inside of an `interactive` context.
1718

1819
## Bug fixes

0 commit comments

Comments
 (0)