One toolchain
Checks, formats and navigates the R you have today.
R/pricing.R
fee_rate <- function(region) {
if (region == "north") 0.18 else 0.22
}
quote <- list(region = "north", amount = 1240.5)
fee <- quote$amount * fee_rate(quote$amount)
message("fee: " + fee)
$ ry check
type-mismatch
x expected a numeric value (`integer` or `double`), found `character`
--[R/pricing.R:7:9]
6 | fee <- quote$amount * fee_rate(quote$amount)
7 | message("fee: " + fee)
| ^^^^^^^
1 problem in 1 file
Plain R with no annotations: types are inferred, and the bug is found anyway.Checking guide
R/pricing.R
#: fn(region: character) -> double
fee_rate <- function(region) {
if (region == "north") 0.18 else 0.22
}
quote <- list(region = "north", amount = 1240.5)
fee <- quote$amount * fee_rate(quote$amount)
message("fee: ", fee)
$ ry check
type-mismatch
x expected `character`, found `double`
--[R/pricing.R:7:32]
6 | quote <- list(region = "north", amount = 1240.5)
7 | fee <- quote$amount * fee_rate(quote$amount)
| ^^^^^^^^^^^^
8 | message("fee: ", fee)
1 problem in 1 file
With that bug fixed, one #: comment finds the next. R reads it as a comment, so the file still runs on GNU R.Annotations
R/summarise.R
summarise <- function(sales, min_units = 1) {
big<-sales[sales$units>=min_units,]
if(nrow(big)==0){
warning("no rows")
return(NULL)}
for(region in unique(big$region)) report(big,region)
invisible(big)
}
summarise <- function(sales, min_units = 1) {
big <- sales[sales$units >= min_units, ]
if (nrow(big) == 0) {
warning("no rows")
return(NULL)
}
for (region in unique(big$region)) {
report(big, region)
}
invisible(big)
}
sales.Rreport.R
# one sale, and what is left of it after fees
sale: list{region: character, amount: double, closed: logical} <- list(region = "north", amount = 1240.5, closed = TRUE)
margin: <T: numeric, U: numeric> fn(gross: T, rate: U) -> double <- function(gross, rate) { gross * (1 - rate) }
net: double <- margin(sale$amount, 0.18)
label: character <- paste0(sale$region, ": ", round(net, 2))
message(label)
3 references
R/sales.R4:1margin <- function(gross, rate) { gross * (1 - rate) }
R/sales.R6:8net <- margin(sale$amount, 0.18)
R/report.R2:28paste0(sale$region, " ", margin(sale$amount, rate))
A backwards-compatible R runtime
Runs your R unchanged, and adds the types R is missing.
R/shapes.R
type Shape =
| :Circle(radius: double)
| :Square(side: double)
area <- function(shape: Shape) -> double {
match (shape) {
:Circle(radius = r) => {
pi * r^2
}
}
}
$ ry check
non-exhaustive-match
x this `match` does not handle `:Square(side = _)`: add an arm for it, or a catch-all `_ => ...` arm
--[R/shapes.R:6:3]
5 | area <- function(shape: Shape) -> double {
6 | match (shape) {
| ^^^^^^^^^^^^^
7 | :Circle(radius = r) => {
R/units.R
type Meters = Record{x: double, y: double}
type Pixels = Record{x: double, y: double}
distance <- function(p: Meters) -> double {
sqrt(p$x^2 + p$y^2)
}
cursor <- @Pixels(x = 640, y = 480)
distance(cursor)
$ ry check
type-mismatch
x expected `Meters`, found `Pixels`
--[R/units.R:9:10]
8 | cursor <- @Pixels(x = 640, y = 480)
9 | distance(cursor)
| ^^^^^^
people.R
ada <- (name = "ada", age = 36L)
ada$name
ada$email
$ ry run people.R
[1] "ada"
Error in ada$email :
the record has no field `email`; its fields are `age`, `name`
sales.R
sales <- DataFrame(
region = ["eu", "us", "eu", "us"],
units = [3L, 5L, 2L, 1L],
price = [1.5, 2, 4, 3]
)
sales |>
DataFrame.filter(Expr.col("units") > 1L) |>
DataFrame.group_by("region") |>
GroupBy.agg(revenue = (Expr.col("units") * Expr.col("price")) |> Expr.sum())
$ ry run sales.R
region revenue
<char> <num>
1: eu 12.5
2: us 10.0
4 times faster than data.table across 19 benchmark queries on 10 million rows, 4 cores.
words.R
words <- Str.split_on("ry checks R", " ")
loud <- List.map(words, Str.with_ascii_uppercased)
Str.join_with(loud, " ")
$ ry run words.R
[1] "RY CHECKS R"
benchmarks
| suite | ry vs GNU R | ry | typed |
|---|---|---|---|
| Public R JIT benchmarks | 3.76x | 20x | |
| Package test suites | 2.75x | not typed | |
| Numeric kernels | 6.06x | 26x | |
| Application workloads | 3.77x | 72x |
Geometric-mean speed-up over GNU R 4.6.2, bars on a logarithmic scale. Full table, programs and method
Take your R code into the future
Use ry today on the code you have, and move it to typed code one step at a time.
Works with the code you have todayAdjust your code, still backwards compatibleUse what ry adds, no longer runs on GNU RStrict mode (R--): only this circle
R/pricing.R
net_price <- function(price, rate) {
price * (1 - ratee)
}
net_price(100, 0.2)
$ ry check
unresolved
! I could not resolve `ratee` in this package, its imports, or builtins. Did you mean `rate`?
--[R/pricing.R:2:16]
1 | net_price <- function(price, rate) {
2 | price * (1 - ratee)
| ^^^^^
3 | }
R/pricing.R
#: fn(price: double, rate: double) -> double
net_price <- function(price, rate) {
price * (1 - rate)
}
net_price(100, "0.2")
$ ry check
type-mismatch
x expected `double`, found `character`
--[R/pricing.R:6:16]
5 |
6 | net_price(100, "0.2")
| ^^^^^
R/pricing.R
net_price <- function(
price: double,
rate: double
) {
price * (1 - rate)
}
net_price(100, 0.2)
net_price(100, "0.2")
$ ry run R/pricing.R
[1] 80
Error in net_price(100, "0.2") :
argument `rate` must be `double`, not `character`
R/pricing.R
# typing: strict
show_price <- function(price: double, label) {
message(label, ": ", price)
}
$ ry check
strict
x strict mode: parameter `label` has no type. How the function uses it does not determine one, so any value is accepted unchecked. Annotate it, as in `label: TYPE`
--[R/pricing.R:2:39]
1 | # typing: strict
2 | show_price <- function(price: double, label) {
| ^^^^^
3 | message(label, ": ", price)
Install
cargo install --git https://github.com/felix-andreas/ry ry-lang
ry check .