-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc11eb4
commit db55c1f
Showing
10 changed files
with
378 additions
and
55 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script> | ||
<script type="module" id="webr-monaco-editor-init"> | ||
|
||
// Configure the Monaco Editor's loader | ||
require.config({ | ||
paths: { | ||
'vs': 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs' | ||
} | ||
}); | ||
</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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/editor/editor.main.css" /> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/editor/editor.main.css" /> | ||
|
||
<style> | ||
.monaco-editor pre { | ||
background-color: unset !important; | ||
|
@@ -7,19 +8,40 @@ | |
.btn-webr { | ||
background-color: #EEEEEE; | ||
border-bottom-left-radius: 0; | ||
border-bottom-right-radius: 0; | ||
border-bottom-right-radius: 0; /* Extra styling for consistency */ | ||
display: inline-block; | ||
font-weight: 400; | ||
line-height: 1.5; | ||
color: #000; | ||
text-align: center; | ||
text-decoration: none; | ||
-webkit-text-decoration: none; | ||
-moz-text-decoration: none; | ||
-ms-text-decoration: none; | ||
-o-text-decoration: none; | ||
vertical-align: middle; | ||
-webkit-user-select: none; | ||
border-color: #dee2e6; | ||
border: 1px solid rgba(0,0,0,0); | ||
padding: 0.375rem 0.75rem; | ||
font-size: 1rem; | ||
border-radius: 0.25rem; | ||
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out; | ||
} | ||
</style> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script> | ||
<script type="module"> | ||
|
||
// Configure the Monaco Editor's loader | ||
require.config({ | ||
paths: { | ||
'vs': 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs' | ||
} | ||
}); | ||
.btn-webr:hover { | ||
color: #000; | ||
background-color: #e3e6ea; | ||
border-color: #e1e5e9; | ||
} | ||
|
||
.btn-webr:disabled,.btn-webr.disabled,fieldset:disabled .btn-webr { | ||
pointer-events: none; | ||
opacity: .65 | ||
} | ||
</style> | ||
|
||
<script type="module"> | ||
|
||
// Start a timer | ||
const initializeWebRTimerStart = performance.now(); | ||
|
@@ -74,19 +96,34 @@ | |
quartoTitleMeta.appendChild(firstInnerDiv); | ||
|
||
// Add new element as last child in header element | ||
var header = document.getElementsByTagName("header")[0]; | ||
header.appendChild(quartoTitleMeta); | ||
var header = document.getElementById("title-block-header"); | ||
|
||
// Check if the header option is present or missing | ||
if(header) { | ||
// If present, directly append the child element. | ||
header.appendChild(quartoTitleMeta); | ||
} else { | ||
// Attempt to place the new element inside a header _after_ the Monaco initialization | ||
var monacoScript = document.getElementById("webr-monaco-editor-init"); | ||
var header = document.createElement("header"); | ||
header.setAttribute("id", "title-block-header"); | ||
// Now attempt to add the webR area to the title | ||
header.appendChild(quartoTitleMeta); | ||
// Include the header after the script tag | ||
monacoScript.after(header); | ||
} | ||
} | ||
|
||
// Retrieve the webr.mjs | ||
import { WebR } from "https://webr.r-wasm.org/v0.1.1/webr.mjs"; | ||
import { WebR, ChannelType } from "https://webr.r-wasm.org/v0.2.1/webr.mjs"; | ||
|
||
// Populate WebR options with defaults or new values based on | ||
// webr meta | ||
globalThis.webR = new WebR({ | ||
"baseURL": "{{BASEURL}}", | ||
"serviceWorkerUrl": "{{SERVICEWORKERURL}}", | ||
"homedir": "{{HOMEDIR}}" | ||
"homedir": "{{HOMEDIR}}", | ||
"channelType": {{CHANNELTYPE}} | ||
}); | ||
|
||
// Initialization WebR | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
importScripts('https://webr.r-wasm.org/v0.1.1/webr-serviceworker.js'); | ||
importScripts('https://webr.r-wasm.org/v0.2.1/webr-serviceworker.js'); |
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 |
---|---|---|
@@ -1 +1 @@ | ||
importScripts('https://webr.r-wasm.org/v0.1.1/webr-worker.js'); | ||
importScripts('https://webr.r-wasm.org/v0.2.1/webr-worker.js'); |
Oops, something went wrong.