Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The official example WebR REPL is definitely cool and useful to get the feel for WebR. But, it is far from an ideal way to deal with it interactively, even as just a REPL.
As y’all know, I’ve been conducing numerous experiments with WebR and various web technologies. I started doing this for numerous reasons, one was to get folks excited about WebR and try to show there are endless possibilities for it (and hopefully avoid lock-in to prescribed views on how you should work with it). Another was to brush up on rusty web skills and have something fun to do during the continuing long aftermath of my spike protein invasion.
I started poking under the WebR covers this past weekend, and until there’s a more pyodide-like JS bridge on the R side of WebR, I decided to forego said spelunking. Instead, I began a dive into {plot2}, a really neat {ggplot2}-esque enhancement to base R plotting. While I could use any R-compatible IDE (there are many, btw), I wanted to do all the experiments in WebR-proper, since base plots work out of the box and the {ggplot2} ecosystem takes a bit of time to install. The tinkering began just fine, but it became a bit tedious doing browser refreshes (they’re automatic with Vite in dev mode) for small tweaks. There was no way I was using the official REPL given the lack of real interactivity in the console. And, I wanted to avoid keeping re-rendering Quarto documents, since that would have been as tedious as the Vite refreshes.
So, I decided to make an “IDE REPL” for WebR, so I could work with it like I would R in Sublime Text, VS Code, or RStudio. I mean, wouldn’t everyone?
You can check it out here, and the source is on GitHub.
I’m not going to take up much more time here, since it comes with some explanations out of the box, but I will reproduce the GH README for it at the end. I will present the structure of the project, here, to make it easier to build upon it (clone/fork away!).
I’m using Monaco, the editor that powers VS Code and the online GitHub editor. It has so many batteries included that it’s hard not to want to use it, even considering how much I despise Microsoft as a company. It is dead simple to use.
The entire project is in vanilla javascript, and there is no builder this time, since I wanted to make this as accessible to as many folks as possible.
This is the project structure:
├── boilerplate.js # text that appears in the source on first load or hard refresh ├── completions.js # a decent number of R completions (I'll add more) ├── index.css # core CSS ├── index.html # HTML shell ├── main.js # Main "app" ├── resizers.js # We need to keep the panes sized properly ├── r.js # Some WebR bits └── rlang.js # Language stuff for Microsoft's Monaco editor
Rather than adorn the interface with silly buttons and baubles, I am putting functionality into the Monaco command palette.
Here’s what you’ve got with v0.1.0:
- Auto-saves current source pane contents to local storage
- R syntax highlighting
- An oddly decent # of auto-completes
- cmd-shift-p to bring up command palette
- WebR: Clear Local Storage — nuke local storage and replace with the default document
- WebR: Save Canvas as PNG — captain obvious
- WebR: Save Source Pane — captain obvious
- WebR: View WebR Environment Summary — captain obvious
- WebR: View WebR History — captain obvious
- cmd-shift-i inserts
|>
- option-shift-minus inserts
<-
- watches for
?…
and will open up a new tab for web help on whatev u searched for (XSS protected) - watches for
broweURL(…)
and will open up the URL in a new tab (XSS protected) - baked-in
install.runiverse(pkg)
which will try to install a pkg from R Universe. It is ON YOU to load the deps and ensure all deps and thepkg
itself will work in WebR. You can use this tool I made to help you out.
FIN
Apart from making the current functionality more robust/pretty, one big forthcoming advancement will be the ability to save/load the WebR workspace to local browser storage. What that will mean for you, is that you can go to an instance of the app, all source changes will automagicallly be saved/restored to the session between visits. Plus — if you’ve saved the workspace image — it will be auto-restored on the visit, leaving you to just have to re-install/load any necessary packages. This means you can get right back to “work”.
I’ll be adding the ability to load files from your local system and use {svglite} for graphics (Monaco has an amazing SVG viewer), and to actually work in the R Console area (either with some janky input box or janky xterm.js).
Kick the tyres, file bugs, feature enhancements, and PRs, and start playing more with WebR!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.