-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
65 lines (49 loc) · 1.25 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# struct
{struct} provides ways to modify objects more strictly, guaranteeing that we
keep the type of the modified element, where type is defined by `vctrs::vec_ptype()`.
The `struct()` function gives a subclass "struct" to the objects if its internal
type is list (thus including data frames), and recursively to its list components.
When assigning new values, we give it the "struct" subclass if relevant then
use `vctrs::vec_cast()` to check the compatibility of the input with the output,
and apply coercion if necessary.
## Installation
Install with:
``` r
remotes::install_github("cynkra/struct")
```
## Example
```{r, error = TRUE}
library(struct)
library(tibble)
x1 <- list(
a = 1,
b = list(tibble(c = 3:4, d = 5:6)),
c = tibble(e = 7:8, f = 9:10),
y = list(z = c("a", "b"))
)
x2 <- deep_struct(x1)
x2
print_tree(x2)
# works
x2$a <- 11
x2$b <- list(tibble(c = 13:14, d = 15:16))
x2$c <- tibble(e = 17:18, f = 19:20)
# fails
x2$a <- c(11, 12)
x2$a <- "a"
x2$b <- list("a", "b")
x2$c <- 1
x2[["z"]]
```