|
79 | 79 | // Initialize webR
|
80 | 80 | await globalThis.webR.init();
|
81 | 81 |
|
82 |
| - // Setup a webR canvas |
83 |
| - await webR.evalRVoid("canvas(width={{WIDTH}}, height={{HEIGHT}})"); |
| 82 | + // Setup a webR canvas by making a namespace call into the {webr} package |
| 83 | + await webR.evalRVoid("webr::canvas(width={{WIDTH}}, height={{HEIGHT}})"); |
84 | 84 |
|
85 | 85 | // Capture output data from evaluating the code
|
86 | 86 | const result = await webRCodeShelter.captureR(codeToRun, {
|
87 | 87 | withAutoprint: true,
|
88 | 88 | captureStreams: true,
|
89 |
| - captureConditions: false, |
90 |
| - env: webR.objs.emptyEnv, |
| 89 | + captureConditions: false//, |
| 90 | + // env: webR.objs.emptyEnv, // maintain a global environment for webR v0.2.0 |
91 | 91 | });
|
92 | 92 |
|
93 | 93 | // Start attempting to parse the result data
|
|
106 | 106 |
|
107 | 107 | // Output each image stored
|
108 | 108 | msgs.forEach(msg => {
|
109 |
| - if (msg.type === "canvasExec") { |
110 |
| - if (!canvas) { |
| 109 | + // Determine if old canvas can be used or a new canvas is required. |
| 110 | + if (msg.type === 'canvas'){ |
| 111 | + // Add image to the current canvas |
| 112 | + if (msg.data.event === 'canvasImage') { |
| 113 | + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); |
| 114 | + } else if (msg.data.event === 'canvasNewPage') { |
| 115 | + // Generate a new canvas element |
111 | 116 | canvas = document.createElement("canvas");
|
112 | 117 | canvas.setAttribute("width", 2 * {{WIDTH}});
|
113 | 118 | canvas.setAttribute("height", 2 * {{HEIGHT}});
|
114 | 119 | canvas.style.width = "700px";
|
115 | 120 | canvas.style.display = "block";
|
116 | 121 | canvas.style.margin = "auto";
|
117 | 122 | }
|
118 |
| - Function(`this.getContext("2d").${msg.data}`).bind(canvas)(); |
119 | 123 | }
|
120 | 124 | });
|
121 | 125 |
|
|
0 commit comments