A strong type system for R

A strong type system for R which supports symbol declaration and assignment with type checking and condition checking.


Build Status

rtype is a strong type system for R which supports symbol declaration and assignment with type checking.

Release notes

Installation

You can install the latest released version from CRAN with

install.packages("rtype")

or the latest development version from GitHub with

devtools::install_github("rtype","renkun-ken")

Examples

Declaring symbols

Declare symbols Before using them.

library(rtype)
declare(x,y=numeric(),z=logical())
ls.str()
x :  NULL
y :  num(0) 
z :  logi(0) 

Assignment with type checking

# NULL symbol can be assigned any value
numeric(x) <- c(1,2,3.5)
 
# numeric symbol can be assigned only numeric value
# is.numeric(y) = TRUE, and is.numeric(c(1,2,3)) = TRUE
numeric(y) <- c(1,2,3)
 
# the symbol does not take value that violates its type checking
# is.integer(y) = FALSE (violation)
integer(y) <- c(1L,2L)
Error: symbol fails type checking with .Primitive("is.integer")
# the assignment fails if the value does not pass type checking
# is.logical(z) = TRUE, but is.logical(c(1,2,3)) = FALSE (violation)
logical(z) <- c(1,2,3)
Error: value fails type checking with .Primitive("is.logical")

Assignment with condition checking

Checking single condition

# assign value checking condition length = 3
numeric(x, length = 3) <- c(1,2,3)
 
# stop if condition is violated
numeric(x, length = 3) <- c(1,2,3,4)
Error: value [length = 4L] violates condition [length = 3]

Checking multiple conditions

# assign value checking multiple conditions
declare(df)
data.frame(df, ncol=2, nrow=10) <- data.frame(x=1:10,y=letters[1:10])
 
# or equivalently
data.frame(df, dim=c(10,2)) <- data.frame(x=1:10,y=letters[1:10])
 
# stop if any condition is violated
data.frame(df, ncol=3, nrow=10) <- data.frame(x=1:10,y=letters[1:10])
Error: value [ncol = 2L] violates condition [ncol = 3]

Checking function conditions

# checking function condition
declare(x)
cond1 <- function(x) mean(x) <= 5
numeric(x, length = 10, cond1) <- 0:9
numeric(x, length = 10, cond1) <- 1:10
Error: value violates condition [function (x) mean(x) <= 5]

General checking

declare(x)
check(x, class="integer", length=10) <- 1:10
check(x, class="numeric", length=10) <- 1:10
Error: value [class = "integer"] violates condition [class = "numeric"]

Help overview

help(package = rtype)

License

This package is under MIT License.

News

Version 0.1-1 (2014-08-15)

Maintenance update

Version 0.1 (2014-08-02)

Add declare function Add assignment functions with type checking and condition checking

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rtype")

0.1-1 by Kun Ren, 5 years ago


http://renkun.me/rtype, https://github.com/renkun-ken/rtype


Report a bug at https://github.com/renkun-ken/rtype/issues


Browse source code at https://github.com/cran/rtype


Authors: Kun Ren <[email protected]>


Documentation:   PDF Manual  


MIT + file LICENSE license


Suggests testthat, knitr


See at CRAN