-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
70 lines (53 loc) · 2.08 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
## Type Check ##
[![Travis-CI Build Status](https://travis-ci.org/jimhester/typeCheck.svg?branch=master)](https://travis-ci.org/jimhester/typeCheck)
[![Coverage Status](https://img.shields.io/codecov/c/github/jimhester/typeCheck/master.svg)](https://codecov.io/github/jimhester/typeCheck?branch=master)
Type check allows use of [types](https://github.com/jimhester/types) to
automatically add checking code when types are annotated.
### Defining Types ###
`type_define()` is used to define a new type. The `check` argument specifies
a function used to verify the objects type. `type_check` adds the checks to a
specific function.
```{r error = TRUE}
type.numeric <- type_define(check = is.numeric)
f <- type_check(function(x = ? numeric) x)
f(1)
f("txt")
```
Types are defined as methods of the `type` generic. This means they follow the
same properties as normal S3 methods and can be exported and imported
to and from packages like all other functions.
The `error` argument is used to specify a custom error message for a type.
```{r error = TRUE}
type.numeric <- type_define(
check = is.numeric,
error = function(obj_name, obj_value, type) {
sprintf("%s: '%s' is not a number!", obj_name, obj_value)
})
f <- type_check(function(x = ? numeric) x)
f("txt")
```
### Packages ###
When writing a package adding a call to `typeCheck::type_check_package()`
anywhere outside a function will add type checks to all functions in the
package. Functions without type annotations are unaltered.
This means it is easy to add annotations in a stepwise process to existing
packages.
If you are using [roxygen2](https://github.com/klutometis/roxygen) You can use
the following `importFrom` statement (or use the equivalent `importFrom()` call directly
in the `NAMESPACE` file.)
```{r eval = FALSE}
f <- function(x = ? numeric) x
#' @importFrom typeCheck type type_define
typeCheck::type_check_package()
```