The Vapour Guide
Vapour is a typed language that transpiles to R.
You write .vp files, Vapour checks them, and then turns them into
plain R code that runs anywhere R runs.
This guide teaches the language from the ground up. Read the chapters in order: each one builds on the previous one, and every example can be copied into a file and run.
This guide explains how to use Vapour, with plenty of examples. The specification states the same rules briefly and precisely, for when you need a definitive answer.
Why Vapour?
Code is read far more often than it is written. When you add up the time spent on a project, very little of it goes into typing the code. Most of it goes into reading code, looking for bugs, and changing code without breaking it.
Vapour makes you write a little more code so you can read it faster, find bugs sooner, refactor without fear, and get fewer errors at run time.
In practice this means three things:
- Everything is declared. Variables are declared with
letorconstand have a type. Functions say what they accept and what they return. - Vapour checks your code before it runs. It catches values of
the wrong type, misspelled variables, missing arguments,
unexpected
NULLs, and more. - The output is just R. If you ever stop using Vapour, the R code it generated is still there and readable. Nothing locks you in.
How it works
Vapour adds a build step between the code you write and the code that runs:
script.vp ──▶ vapour checks it ──▶ script.R ──▶ R runs it
The check step is where Vapour is useful. It reports problems as diagnostics (errors, warnings, and hints), and refuses to produce R code when it finds an error.
With an editor plugin you see the same diagnostics while you type, before you run anything.
What you need
- R, Vapour calls R to know about base R functions.
- The
vapourcommand line tool, see install. - Optionally, an editor plugin for syntax highlighting and live diagnostics.
This guide assumes you know some R. Each example shows the Vapour code next to the R code it produces, so you can always see what Vapour does.
What's in this guide
The basics
- Your first program: write, check, run, and transpile a script.
- Variables:
let,const, assignment, and values. - Types: the built-in types and how Vapour checks them.
- Control flow: vectors, operators, conditions, and loops.
- Functions: declaring, calling, and passing functions.
Going further
- Custom types: objects, structs, data frames, and more.
- Methods: S3 methods, generics, and classes.
- Packages: writing R packages in Vapour and sharing types.
- Diagnostics: every check Vapour runs.
Examples
Complete programs that put it all together: a basic script, callbacks, S3 methods, and a Shiny app.