library(knitr) opts_knit$set(base.dir='./', fig.path='', out.format='md') opts_chunk$set(prompt=FALSE, comment='', results='markup') rm(list = objects())
rtype is a strong type system for R which supports symbol declaration and assignment with type checking.
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")
Declare symbols Before using them.
library(rtype) declare(x,y=numeric(),z=logical()) ls.str()
# 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) # 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)
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)
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])
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
General checking
declare(x) check(x, class="integer", length=10) <- 1:10 check(x, class="numeric", length=10) <- 1:10
help(package = rtype)
This package is under MIT License.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.