You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/typewriter.Rmd
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ knitr::opts_chunk$set(
18
18
library(typewriter)
19
19
```
20
20
21
-
The goal of typewriter is to add type safety to your R code and it is inspired by [Pydantic](https://docs.pydantic.dev/). Under the hood, it mainly uses base R functions to keep its dependencies as low as possible. It lets you
21
+
The goal of typewriter is to add type safety to your R code. Under the hood, it mainly uses base R functions to keep its dependencies as low as possible. It lets you
22
22
23
23
* add types checks to your function arguments with `check_args()`
24
24
* create typed structures with `typed_struct()` and `base_model()`
@@ -87,17 +87,26 @@ Person <- typed_struct(
87
87
name = "character",
88
88
age = "integer"
89
89
)
90
+
91
+
# R, alternate syntax
92
+
Person <- typed_struct(
93
+
name = character(),
94
+
age = integer()
95
+
)
90
96
```
91
97
92
98
A typed structure could be used as input for a function:
try(say_hello_to(list(name = "Peter", age = 20L)))
101
110
```
102
111
103
112
You can also use it to validate a data frame:
@@ -114,25 +123,31 @@ df$id <- 1:2
114
123
try(Person(df))
115
124
```
116
125
117
-
A type can also be described as a function that takes the value to be validated as its only arguments and returns `TRUE` in case of success:
126
+
A type can also be defined as a function that takes the value to be validated as its only argument and returns `TRUE` in case of success:
118
127
119
128
```{r}
120
129
my_type <- typed_struct(
121
130
a_number = is.integer,
122
-
some_text = is.character
131
+
some_text = is.character,
132
+
# Add a range check to y
133
+
y = function(y) {
134
+
is.integer(y) & y > 1 & y < 10
135
+
}
123
136
)
124
137
125
138
obj <- my_type(
126
139
a_number = 1:2,
127
-
some_text = "typewriter"
140
+
some_text = "typewriter",
141
+
y = 5L
128
142
)
129
143
130
144
obj
131
145
132
146
try(
133
147
my_type(
134
-
a_number = NULL,
135
-
some_text = "typewriter"
148
+
a_number = 1L,
149
+
some_text = "typewriter",
150
+
y = 1L
136
151
)
137
152
)
138
153
```
@@ -174,7 +189,7 @@ As you can see in the example above you can also check the length of the value b
174
189
175
190
### The `base_model()` function
176
191
177
-
Under the hood `typed_struct()` and `check_args()`uses`base_model()` that got its name from [Pydantic's](https://docs.pydantic.dev/)`BaseModel` class. It gives you further options like validating your inputs before the assignment:
192
+
Under the hood `typed_struct()` and `check_args()`use`base_model()` that got its name from [Pydantic's](https://docs.pydantic.dev/)`BaseModel` class. It gives you further options like validating your inputs before the assignment:
0 commit comments