MyNixOS website logo
Description

A strong type system for R.

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

rtype

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.

Metadata

Version

0.1-1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows