Packages
Vapour fits into the usual R package workflow: you write .vp files,
Vapour writes the R files in R/, and you document, check, and install
the package with your usual tools.
1. Create an R package
Create an R package as you normally would.
usethis::create_package("mypkg")
2. Add a directory for Vapour code
Vapour code lives in its own directory, vp by convention.
Vapour also needs an inst directory, where it writes the types your
package exports.
- bash
- R
mkdir vp inst
dir.create("vp")
dir.create("inst")
Your package now looks like this:
mypkg/
├── DESCRIPTION
├── NAMESPACE
├── R/ # generated by Vapour
├── inst/ # inst/types.vp is generated by Vapour
└── vp/ # your Vapour code
3. Write some code
Write Vapour code in vp/. Roxygen2 comments (#') are copied to the
R code unchanged, so document your functions as usual.
#' Add one
#'
#' @param x An integer
#'
#' @export
func add_one(x: int): int {
stopifnot(!missing(x))
return x + 1
}
All files in vp/ are read as one program, recursively and in
alphabetical order of their path. Something must be declared in a file
that comes before the files that use it: don't use, in a.vp, a variable
declared in b.vp. Prefixing files with numbers, e.g.: 01-types.vp,
02-utils.vp, makes the order explicit.
4. Transpile
From the root of the package, transpile the vp directory.
vapour -indir=vp
This writes R/vapour.R (rename it with -outfile) and inst/types.vp.
Then document, check, and install as you normally would.
devtools::document()
devtools::check()
devtools::install()
The CLI can run these for you after a successful transpilation:
vapour -indir=vp -devtools=document,check,install
Calling other packages
Vapour does not read roxygen2's @import or @importFrom, so always
call functions from other packages with their namespace.
let title: char = tools::toTitleCase("my title")
Remember to list the packages you use in your DESCRIPTION's Imports.
Sharing types between packages
Exporting
You don't need to do anything to export types. When you transpile a
package, every type you declare is written to inst/types.vp.
That file is installed with your package, and Vapour uses it to find
the types of installed packages.
Use -types to write the file somewhere else.
Importing
There is no import statement. Use another package's types with its namespace, as you would its functions.
let x: aPkg::theType = aPkg::theType(x = 1)
type wrapper: object {
inner: aPkg::theType
}
Vapour checks these values just like your own types.
Recap
- Put Vapour code in
vp/and createinst/. vapour -indir=vpwritesR/vapour.Randinst/types.vp.- Files are read in alphabetical order, declare things before using them.
- Call other packages with
pkg::fn(), and use their types withpkg::type.
The precise rules are in the spec: source files and transpilation.