# The R runner's messages

The R runner (`rrun/runner.html`, `runner.js`, `helpers.R`) is a separate program, free software under the GNU GPL
version 3 or later. It runs R (webR, GPL) and talks to the page that opened it in a frame only through
`window.postMessage` on the same origin, with plain data. This file is the whole of that conversation.

## Requests (page → runner)

`{ rrun: 1, id, op, args }` – answered once with `{ rrun: 1, id, ok: true, result }` or `{ rrun: 1, id, ok: false,
error }`. Requests are done one at a time, in the order sent; `interrupt` is done at once.

| op | args | result |
|---|---|---|
| `run` | `code` (R code, text), `width`, `height` (plot size, pixels) | `{ out: [{ stream: 'stdout' \| 'stderr' \| 'message' \| 'warning' \| 'error', text }], plots: [PNG bytes], error: text \| null, views: [titles], help: [{ topic, package } \| { package } \| { search }] }` – views: the data `View()` was asked to show (then read with `table`, the title as the name); help: what `?topic`, `help()`, `help(package =)` and `??words` asked to show (then read with `help`, `helpIndex`, `helpSearch`) |
| `check` | `code` | `'ok'`, `'incomplete'` (an unfinished expression) or `'error'` – nothing is run |
| `interrupt` | – | `true` (the running code stops: `run` answers with what it had, and "Stopped") |
| `env` | – | `[{ name, class, kind, dims, size, preview }]` – the objects in R's global environment |
| `table` | `name`, `from`, `to` (rows, from 1), `sort` (a column), `desc` | `{ total, columns: [{ name, type }], rowNames, rows: [[values]] }` |
| `str` | `name` | text (R's `str()` of the object) |
| `files` | – | `[{ name, size }]` – the files in R's working directory |
| `packages` | – | `[{ name, version, attached }]` |
| `install` | `pkgs` (names) | `true` (from webR's repository) |
| `available` | – | `[[name, version]]` – the packages webR's repository has |
| `help` | `topic`, `package` (optional) | `{ package, file, topic, html }` – the help page as R's Rd2HTML makes it; its links read `../../package/help/topic` |
| `helpIndex` | `package` | `{ package, title, version, topics: [{ name, title, topic }] }` |
| `helpSearch` | `q` (words) | `[{ topic, package, title }]` – topics named so, starting with them, containing them; titles containing them |
| `complete` | `line` (text before the cursor), `pos` | `{ token, items: [{ label, kind: 'function' \| 'data' \| 'value' \| 'arg' \| 'field' \| 'other' }] }` – R's own completion |
| `signature` | `name` (a function) | text: its usage, e.g. `mean(x, ...)` – `''` if it isn't a function |
| `saveWorkspace` | – | `{ bytes (an .RData file), size, packages }` – the objects, and the packages from webR's repository they need |
| `loadWorkspace` | `bytes`, `packages` | the number of objects loaded (the packages installed first) |
| `writeFile` | `path` (in the working directory), `bytes` | `true` |
| `readFile` | `path` | bytes |
| `deleteFile` | `path` | `true` |
| `reset` | – | `true` (a new R session) |

## Events (runner → page)

`{ rrun: 1, type: 'ready', isolated }` when R has started · `{ rrun: 1, type: 'failed', error }` if it couldn't ·
`{ rrun: 1, type: 'busy', busy }` while code runs (`run`, `install`, `loadWorkspace`, `reset` – the requests that take
long; `interrupt` stops `run`).

Nothing else crosses: no functions, no shared memory, no objects of R's own – text, numbers, JSON and bytes only.
