brodie gaslam
2017-Jul-26 12:14 UTC
[R-pkgs] New Package vetr: Enforce Structural Requirements for S3 Objects
`vetr` implements a declarative template-based approach to verify that objects meet structural requirements, and auto-composes error messages when they do not. This package is intended to simplify a more formal use of S3 objects. The template concept is borrowed from `vapply`:>> vet(numeric(1L), 1:3) >? [1] "`1:3` should be length 1 (is 3)" >> vet(numeric(1L), "hello") >? [1] "`\"hello\"` should be type \"numeric\" (is \"character\")" >> vet(numeric(1L), 42) >? [1] TRUEThere is no limit on template complexity:>> vet(matrix(integer(), ncol=3), matrix(1:12, 3)) >? [1] "`matrix(1:12, 3)` should have 3 columns (has 4)" > >> iris.template <- abstract(iris) >> levels(iris$Species)[3] <- 'sibirica' >> vet(iris.template, iris[1:10,]) >? [1] "`levels((iris[1:10, ])$Species)[3]` should be \"virginica\" >? (is \"sibirica\")"`vetr` implements programmable non-standard evaluation via recursive substitution of language objects:>> a <- quote(integer() && . > 0) >> b <- quote(logical(1L) && !anyNA(.)) >> c <- quote(a || b) >> vet(c, -1) >? [1] "At least one of these should pass:" >? [2] "? - `-1 > 0` is not TRUE (FALSE)" >? [3] "? - `-1` should be type \"logical\" (is \"double\")"Performance is comparable to `stopifnot` for simple checks, and is faster for complex (template based) ones.? There is a mode that further streamlines parameter vetting in functions. The package is still under development, but the features should be mostly stable.? Feedback welcome <https://github.com/brodieG/vetr>. Best regards, Brodie [[alternative HTML version deleted]]