-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
62 lines (44 loc) · 2.5 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
[![Build Status](https://travis-ci.org/mb706/typer.svg?branch=master)](https://travis-ci.org/mb706/typer)
[![CRAN Status Badge](http://www.r-pkg.org/badges/version/typer)](https://CRAN.R-project.org/package=typer)
[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/typer)](https://cran.rstudio.com/web/packages/typer/index.html)
```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```
# TypeR: Type Annotations for R
## What is this?
When writing an R package, it may be important to check that the types of certain values are as expected. This is especially true for user-facing functions, since otherwise giving wrongly typed arguments can lead to cryptic error messages. `typer` makes these checks automatic, using short and legible type annotations in declared functions.
## Usage
An example function with type annotations is
```{r}
fun = function(a = NULL : numeric(1) [[ a > 0 ]] | NULL) { a }
```
This function accepts one argument that may either be `NULL` or a numeric length 1 vector with a positive value. To compile a single function so that it uses its type decorators, use `compileFunction()`.
```{r, errors = TRUE}
library("typer")
compfun = compileFunction(fun, name = "compfun")
compfun()
compfun(1)
compfun("a")
compfun(-1)
```
`compileFunction()` is mostly useful for interactive development; to use type decorators in packages, the `compileTypes()` function should be used: It compiles all functions declared in its current environment. It should be called *after* all other functions have been created, e.g. in a file called `zzz.R`. The code generated by `typer` uses the `checkmate` package, so a package using `typer` should declare `checkmate` in its `DESCRIPTION` file in the `Imports` section.
The full description of annotation possibilities can be read in the help page of `compileTypes()`:
```{r, eval = FALSE}
?compileTypes
```
## Installation
`typer` is currently only on GitHub, so using the `devtools` package, do
```{r, eval = FALSE}
devtools::install_github("mb706/typer")
```
## Future Changes
`typer` is currently only a small tool which may or may not undergo future changes. Possibilities are, in order of decreasing likelihood:
* Syntax refinement
* Decoration of return values
* Recognizing more special classes / modes
* Compatibility with the `types` package
* Using a different backend than `checkmate`
* Class structure definitions
* Type inference
## License
This project is licensed under the terms of the [GNU AGPL v3](https://www.gnu.org/licenses/agpl-3.0.html).