This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sealr1.Rmd
99 lines (72 loc) · 3.14 KB
/
sealr1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: "1. Sealing the R Objects Test and Assert Conditions"
author: "Shinya Uryu"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
number_sections: true
vignette: >
%\VignetteIndexEntry{1. Sealing the R Objects Test and Assert Conditions}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## Motivation
Data is not always what you think. Someone may change a single value or the data type may be different depending on the specification the API. We need to be aware of these data changes as soon as possible (*It is hard to review from the final result!*).
You can use tests and asserts to check data behavior. Although the testthat package is originally used for unit test of R package, this framework is wide and applicable to any *R* object. On the other hand, it is time-consuming task to enter the state of existing objects, and there is a possibility of mistakes as well.
The goal of **sealr** is to reduce the burden of writing unit tests and assertion that record the state of objects. Applying a function of sealr to the target object outputs the test code that record the current state.
## How to use
1. (As usual) Create an *R* object.
2. Execute the function of sealr (`design_*()` or `transcribe()`) against a object whose state is to be record.
- `design_*()`... The preamble is fixed and consists of the function name bearing test items (eg. `design_class()`, `design_length()`, etc.).
- `transcribe()`... Sets of `design_*()`.It is a generic function and returns combinations according to the class of the object.
```{r, eval = TRUE, echo = TRUE}
library(sealr)
library(testthat)
```
```{r}
x <- seq(1, 9, by = 2)
design_class(x, seal = TRUE)
design_range(x, seal = TRUE)
```
You can copy the output, but if you activate the *clip* argument, the output result will be in a copied state, pasting that value is too easy. This feature depends on the [clipr](https://github.com/mdlincoln/clipr) package.
```{r, eval = FALSE, echo = TRUE}
design_class(x, seal = TRUE, clip = TRUE)
expect_is(
x,
"numeric"
)
```
`transcribe()` is a generic function that produces output according to the class of the object. Currently it suports to 8 classes, but we plan to add various classes in future upgrades.
```{r, warning = FALSE}
transcribe(iris)
my_data <- list(A = letters, B = trees)
transcribe(my_data, load_testthat = FALSE, ts = FALSE)
```
## Details
### Options
Both `design_*()` and `transcribe()` have the following common arguments (Ref. `?seal`).
- load_testthat: include `library(testthat)` when *TRUE*
- clip: If *TRUE* will overwrite the system clipboard. When clipr is not available, The clip arguments is forcibly *FALSE*.
- ts: include comments that timestamp?
- mask_seal: Whether to comment out after executing the function. Default FALSE. This option is effective only when using RStudio.
### APIs
#### design_* function
- `design_class()`
- `design_length()`
#### transcribe function
Suports to 8 classes. Default test of the class.
- numeric
- character
- factor
- Date
- data.frame
- list
- matrix
- table