-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving Hidden Evaluation Options (#36)
* Wordsmithing. * Fix option typo. * Rename output templates to match with context cell option * Add a template that shows output-only. * Incorporate the new output template into the filter. * Avoid the plot.new() request by removing the grid. * Add details on the `context` set of options and
- Loading branch information
Showing
8 changed files
with
257 additions
and
36 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<div id="webr-code-output-{{WEBRCOUNTER}}" aria-live="assertive"> | ||
<pre style="visibility: hidden"></pre> | ||
</div> | ||
<script type="module"> | ||
// Retrieve webR code cell information | ||
const outputDiv = document.getElementById("webr-code-output-{{WEBRCOUNTER}}"); | ||
|
||
// Function to execute the code (accepts code as an argument) | ||
async function executeOutputOnlyCode(codeToRun) { | ||
// Create a canvas variable for graphics | ||
let canvas = undefined; | ||
|
||
// Initialize webR | ||
await globalThis.webR.init(); | ||
|
||
// Setup a webR canvas by making a namespace call into the {webr} package | ||
await webR.evalRVoid("webr::canvas(width={{WIDTH}}, height={{HEIGHT}})"); | ||
|
||
// Capture output data from evaluating the code | ||
const result = await webRCodeShelter.captureR(codeToRun, { | ||
withAutoprint: true, | ||
captureStreams: true, | ||
captureConditions: false//, | ||
// env: webR.objs.emptyEnv, // maintain a global environment for webR v0.2.0 | ||
}); | ||
|
||
// Start attempting to parse the result data | ||
try { | ||
|
||
// Stop creating images | ||
await webR.evalRVoid("dev.off()"); | ||
|
||
// Merge output streams of STDOUT and STDErr (messages and errors are combined.) | ||
const out = result.output.filter( | ||
evt => evt.type == "stdout" || evt.type == "stderr" | ||
).map((evt) => evt.data).join("\n"); | ||
|
||
// Clean the state | ||
const msgs = await webR.flush(); | ||
|
||
// Output each image stored | ||
msgs.forEach(msg => { | ||
// Determine if old canvas can be used or a new canvas is required. | ||
if (msg.type === 'canvas'){ | ||
// Add image to the current canvas | ||
if (msg.data.event === 'canvasImage') { | ||
canvas.getContext('2d').drawImage(msg.data.image, 0, 0); | ||
} else if (msg.data.event === 'canvasNewPage') { | ||
// Generate a new canvas element | ||
canvas = document.createElement("canvas"); | ||
canvas.setAttribute("width", 2 * {{WIDTH}}); | ||
canvas.setAttribute("height", 2 * {{HEIGHT}}); | ||
canvas.style.width = "700px"; | ||
canvas.style.display = "block"; | ||
canvas.style.margin = "auto"; | ||
} | ||
} | ||
}); | ||
|
||
// Nullify the outputDiv of content | ||
outputDiv.innerHTML = ""; | ||
|
||
// Design an output object for messages | ||
const pre = document.createElement("pre"); | ||
if (/\S/.test(out)) { | ||
// Display results as text | ||
const code = document.createElement("code"); | ||
code.innerText = out; | ||
pre.appendChild(code); | ||
} else { | ||
// If nothing is present, hide the element. | ||
pre.style.visibility = "hidden"; | ||
} | ||
outputDiv.appendChild(pre); | ||
|
||
// Place the graphics on the canvas | ||
if (canvas) { | ||
const p = document.createElement("p"); | ||
p.appendChild(canvas); | ||
outputDiv.appendChild(p); | ||
} | ||
} finally { | ||
// Clean up the remaining code | ||
webRCodeShelter.purge(); | ||
} | ||
} | ||
|
||
// Run the code | ||
executeOutputOnlyCode(`{{WEBRCODE}}`) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script type="module"> | ||
// Initialization WebR | ||
await globalThis.webR.init(); | ||
|
||
// Run R code without focusing on storing data. | ||
await globalThis.webR.evalRVoid(` | ||
{{WEBRCODE}} | ||
`) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters