An interactive, in-browser R tutorial for undergraduate watershed ecology students with no programming background. Code runs directly in the browser using WebR — no R installation required.
- 15 chapters covering variables, vectors, data frames, operators, equation translation, for-loops, while-loops, linear models, base R plotting, and ggplot2.
- Interactive code cells — students can read, run, and modify every example.
- Empty answer cells for each exercise.
- Assessment toggle on every non-plotting exercise. Students can choose "Just run" (execute without grading) or "Run & check" (execute and get pass/fail feedback per criterion). Plotting exercises (10.1, 11.1) skip the toggle since their output is visual.
- Hide/reveal solutions to encourage students to attempt exercises before peeking.
- All examples are watershed-ecology themed — discharge, DO, nitrate, EPT richness, stage-discharge curves, Manning's equation for open channel flow, and so on.
Because of the cross-origin isolation requirement, you can't just open index.html directly. Use any simple static server, for example:
# Python (any modern version)
python3 -m http.server 8000
# Or, with Node:
npx serve .Then open http://localhost:8000.
.
├── index.html # The tutorial page
├── coi-serviceworker.js # Enables COOP/COEP headers for WebR (do not modify)
├── .nojekyll # Tells GitHub Pages to skip Jekyll processing
├── css/
│ └── styles.css # All styling
├── img/
│ ├── R-Intro-logo.png # Favicon
│ └── rstudio-layout.png # Image of RStudio
├── js/
│ ├── assessments.js # Per-exercise grading specs (R validators + JS text checks)
│ ├── main.js # UI wiring (run buttons, toggles, TOC, progress bar)
│ ├── rmd-export.js # Website to R Markdown conversion
│ └── runner.js # WebR initialization + code execution
└── README.md
Each exercise's grading logic lives in js/assessments.js. To change how a check works, edit the R validator code under that exercise's key. The validator function .assess() returns a list of list(ok, label, detail) items — one per criterion. Pass/fail is reported per item.
For example, to change the threshold for Exercise 6.1, edit 'ex-6-1' and adjust the abs(val - 1.5747) < 0.01 tolerance.
To add a JS-side textual check (used to verify that specific operators or function names appear in the student's code), add a textCheck block — see 'ex-5-1' for the pattern.
- WebR is the R interpreter compiled to WebAssembly. It downloads (~30 MB) on first visit and runs in a Web Worker.
coi-serviceworker.jspatches inCross-Origin-Opener-PolicyandCross-Origin-Embedder-Policyheaders, which GitHub Pages doesn't set on its own. Without these,SharedArrayBufferis unavailable and WebR can't run.- Each
<textarea class="code-editor">is a live editor; clicking Run sends the code to WebR and renders text + plot output. - Solutions are hidden inside
<details>elements until the student clicks Reveal solution.
Need to adapt this for a different course or audience?
- Edit
index.htmlto change the text, exercises, or starter code. - Edit
css/styles.cssto change the color palette (defined in:rootat the top). - Add packages: in
js/runner.js, theinstallPackagescall pre-loadsggplot2. Add others likedplyrto that array. Note: not every CRAN package is yet available as a WebAssembly binary — see the WebR package list.
Course material — adapt freely for non-commercial educational use.
The bundled coi-serviceworker.js is MIT-licensed by gzuidhof.
WebR is owned by R-Wasm and the Posit team.