forked from tidyverse/magrittr
/
README.Rmd
206 lines (150 loc) · 4.86 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/"
)
library(pipes)
```
# pipes
Install and attach package:
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("moodymudskipper/pipes")
library(pipes)
```
The *pipes* package expands the *magrittr* package by providing :
* More pipe operators to debug, print extra info, suppress warnings or
messages etc
* A convenient way to create custom pipes
* A couple of pipe friendly functions for printing (`pprint`) and testing (`pif`).
The package works just as *magrittr* except that:
* `alias` functions were not imported
* pipes have a class `pipe` and have a dedicated printing method
*magrittr* doesn't need to be attached, but attaching it before *pipes* will
make the alias functions available.
## New operators
* **`%D>%`** : Debug the pipe chain at the relevant step
* **`%V>%`** : Use `View()` on the output
* **`%L>%`** : Log the relevant call and execution time to the console
* **`%P>%`** : Use `print()` on the output
* **`%summary>%`** : Print the `summary()` off the output
* **`%glimpse>%`** : Use `tibble::glimpse()` on the output
* **`%skim>%`** : Use `skimr::skim()` on the output
* **`%ae>%`** : Use `all.equal` on the input and output
* **`%compare>%`** : Use `arsenal::compare()` and open the report of the
differences in the default browser window
* **`%gg>%`** : Apply the `rhs` to the data of a `gg` object and return the
modified `gg` object
* **`%nowarn>%`** : Silence warnings
* **`%nomsg>%`** : Silence messages
* **`%strict>%`** : Fail on warning
* **`%try>%`** : Try, and in case of failure prints error and returns input
* **`%quietly>%`** : Use `purrr::quietly()` to capture outputs of all kind and print them
Let's showcase a few of them.
debug the chain:
```{r, , eval = FALSE}
iris %>% head(2) %D>% `[`(4:5)
```
view steps of chain in the viewer:
```{r, eval = FALSE}
iris %V>% head(2) %V>% `[`(4:5)
```
Log steps in the console:
```{r}
iris %L>% {Sys.sleep(1);head(.,2)} %L>% {Sys.sleep(2);.[4:5]}
```
Use `print`, `summary` or `glimpse` on output:
```{r}
iris %P>% head(2) %P>% `[`(4:5)
iris %summary>% head(2) %summary>% `[`(4:5)
iris %glimpse>% head(2) %glimpse>% `[`(4:5)
```
Use `all.equal` on input and output, note that the method for tibbles gives
a different output.
```{r}
iris %>% head(2) %ae>%
transform(Species = as.character(Species), cst = 42)
iris %>% tibble::as_tibble() %>% head(2) %ae>%
transform(Species = as.character(Species), cst = 42)
```
Use `arsenal::compare` on input and output, and opens a markdown report written
into a temp file.
```{r, eval = FALSE}
iris %>% head(2) %compare>%
transform(Species = as.character(Species), cst = 42)
```
Use *tidyverse* syntax to mofidy a *gg* object's underlying data:
```{r}
library(ggplot2,quietly = TRUE, warn.conflicts = FALSE)
ggplot(iris, aes(Species, Sepal.Width, fill=Species)) +
geom_col() %gg>% dplyr::group_by(Species) %gg>% dplyr::summarize_at("Sepal.Width", mean) +
ggtitle("Using dplyr verbs")
```
Silence a warning or a message:
```{r}
-1 %>% sqrt
-1 %nowarn>% sqrt
iris[50:51,3:5] %>% dplyr::left_join(iris[50:51,c(1:2,5)])
iris[50:51,3:5] %nomsg>% dplyr::left_join(iris[50:51,c(1:2,5)])
```
Strictly fail on a warning
```{r}
try(-1 %strict>% sqrt())
```
Try, and in case of failure prints error and returns input
```{r}
"a" %try>% log()
```
Use `quietly` to capture outputs of all kind and print them.
```{r}
iris[50:51,3:5] %quietly>%
dplyr::left_join(iris[50:51,c(1:2,5)]) %quietly>%
dplyr::mutate(Petal.Length = - Petal.Length, Petal.Length = sqrt(Petal.Length))
```
## `new_pipe`
It's easier to understand how to build a new `pipe` by looking at examples.
```{r}
`%T>%`
```
If we want to rebuild this operator from scratch, all we have to do is :
```{r}
`%newT>%` <- new_pipe({
local(BODY)
.
})
```
`.` is the value of the input and `BODY` contains the code that would have been
executed by `%>%`, for example `iris %>% head(3)` `BODY` would be `head(.,3)`.
so what `%newT>%` is doing is executing the call in a protected environment through
`local(BODY)`, then returning the unaltered input `.`.
```{r}
iris %>% head(2) %newT>% print %>% head(1)
```
Take a look at the other functions to understand how to make your own :
```{r}
`%nowarn>%`
`%P>%`
`%summary>%`
`%strict>%`
```
## easy conditional steps with `pif`
Using functions, formulas or expressions
```{r}
iris %>% pif(is.data.frame, dim, nrow)
iris %>% pif(~is.numeric(Species), ~"numeric :)",~paste(class(Species)[1],":("))
iris %>% pif(nrow(iris) > 2, head(iris,2))
```
## print info on intermediate steps with `pprint`
```{r}
iris %>%
pprint(~"hello") %>%
head(2) %>%
transform(Species = NULL) %>%
pprint(rowSums,na.rm = TRUE) %>%
pprint(~setNames(.[1:2],toupper(names(.[1:2])))) %>%
pprint(dim)
```