[R-pkgs] New Package vetr: Enforce Structural Requirements for S3 Objects

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[R-pkgs] New Package vetr: Enforce Structural Requirements for S3 Objects

Heinzen, Ethan P. via R-packages
`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] TRUE

There 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]]

_______________________________________________
R-packages mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-packages
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.