Skip to main content
Version: 0.0.6

Syntax

Vectors​

In Vapour vectors are declared with parenthesis, without the need for c prefix.

let x: int = (1, 2, 3)

Range​

The : is used to define types in Vapour, so we use .. for ranges.

let x: int = 1..10

For loop​

The iterator needs to be declared with let.

for(let i: int in 1..10) {
print(i)
}

Conditions​

if, else if, and else work as they do in R.

let x: int = 2

if(x == 1) {
print("one")
} else if(x == 2) {
print("two")
} else {
print("other")
}

While loop​

let i: int = 0

while(i < 3) {
i += 1
}

Strings​

Strings can use double or single quotes.

let x: char = "hello"
let y: char = 'world'

Comments​

Comments start with #, roxygen2 comments (#') are kept as-is in the transpiled code so you can document packages.

#' Add one
#' @param x An integer
#' @export
func add_one(x: int): int {
# this is a comment
stopifnot(!missing(x))
return x + 1
}

Operators​

Operators are the same as in R.

  • Arithmetic: +, -, *, /, ^, %%
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !
  • Custom infix operators, e.g.: %in%, %>%
let x: int = 10
let ok: bool = x > 1 && x <= 20
let z: num = x %% 3 + x ^ 2

Indexing​

Use [, [[, and $ as in R.

let x: int = (10, 20, 30)
let first: int = x[1]

x[2] = 25

Pipe​

The native pipe |> is supported.

let x: num = (1, 2, 3) |>
sum()

Namespaces​

Call functions from other packages with ::, or ::: for unexported functions, Vapour checks that the package is installed and that it exports the function (see features).

let x: char = tools::toTitleCase("hello")

Return​

In Vapour return is a keyword, not a function.

func addOne(x: int): int {
return x + 1
}

Defer​

Vapour adds the defer keyword which translates to on.exit, defer expects an anonymous function.

func addOne(x: int): int {
defer (): null => {
print("second!")
}

print("first")
return x + 1
}

Declaration & assignment​

In Vapour assignment is always done with the = sign, <- is used for something else (see next section).

You also have to declare mutable variables with let, this avoids shooting yourself in the foot overriding variables that you should not.

let x: char = "hello"

Assign parent​

In Vapour <- is used to assign to a parent variable, where one would use <<- in R.

let x: char = "hello"

if(x == "world") {
x <- "it's world!"
}

Constants​

We have constants!

info

This is only checked in Vapour, we do not lock the binding in the transpiled code.

const x: char = "a constant!"

Booleans​

Booleans are denoted with true and false.

const x: bool = true

Assignments​

Vapour supports addition (+=) and substitution assignments (-=).

let x: int = 40
x += 2
x -= 1

Missing values​

R's missing and special values are available as they are in R: NA, NA_integer_, NA_real_, NA_character_, NA_complex_, NaN, and Inf.

let x: int = NA_integer_
let y: num = Inf
info

NA is a value, not a type, it is accepted by any vector type. Vapour does not check for NAs, see types for NULL.