Important
RS is in early development, and not quite ready for production use.
You may encounter bugs or missing features. Feel free to open an issue or PR if you do!
Contributions are definitely welcome! ✅
RS is an R package that implements fast, validated classes, with
the underlying implementation written in Rust.
It is centered around:
- Performance:
- ~700% faster than the next fastest R classes.
 
- Type validation:
- Gives you confidence that your classes contain the right data.
 
- Reference semantics:
- The underlying storage is a reference counted hashmap, allowing in-place modification of classes.
 
The name RS is a combination of:
- The R and S languages (S is R’s predecessor).
- .rs- the file extension used for Rust files, and the TLD of Rust websites.
Any of the following should work:
pak::pak("avhz/RS")
## or
renv::install("avhz/RS")
## or
devtools::install_github("avhz/RS")This installs the latest development version of RS from GitHub. RS
is not yet on CRAN, but hopefully will be soon.
library(RS)
Class(
    "Dog",
 
    # Fields
    name  := t_char,
    age   := t_int,
    breed := t_char,
 
    # Methods
    bark := function(.self) cat(.self@name, "goes woof!\n")
)
 
fluffy <- Dog(name = "Fluffy", age = 3L, breed = "Golden Retriever")Note that the fields are special objects like t_int, t_char, etc.
These objects allow for attribute type validation.
If you pass an incorrect type for one of the fields, you will get an error like:
fluffy <- Dog(name = 1, age = 3L, breed = "Golden Retriever")
"Invalid type <'double'> passed for field <'name'>."fluffy@name
#> [1] "Fluffy"
fluffy@age
#> [1] 3
fluffy@breed
#> [1] "Golden Retriever"
fluffy@bark()
#> Fluffy goes woof!I used the slot operator @ for accessing attributes in RS, because
the common $ operator is so pervasive in R code that I wanted an
operator that made it more obvious that the object is an RS class,
rather than a list, dataframe, environment, R6 class, etc.
Note: Inheritance is not currently supported and most likely won’t be unless there is overwhelming demand for it. Instead, composition is supported and preferred.
See the Benchmarks vignette for benchmark code.
The following shows the iterations per second for class instances for a number of R OOP libraries.
You can see that RS offers around a 7x performance gain over the
other offerings.
Note #1: the other libraries may provide more features and are currently more mature/complete offerings, in particular
R6.
Note #2: creating Python class instances via
reticulateinside the R interpreter is significantly slower than doing it directly in Python. Python classes are still much, much faster than myRSpackage (~8-10x faster thanRSwhen I last checked).
