8
8
[ ![ CRAN
9
9
status] ( https://www.r-pkg.org/badges/version/magrittr )] ( https://cran.r-project.org/package=magrittr )
10
10
[ ![ Codecov test
11
- coverage] ( https://codecov.io/gh/tidyverse/magrittr/branch/master/graph/badge.svg )] ( https://app.codecov.io/gh/tidyverse/magrittr?branch=master )
12
- [ ![ R build
13
- status] ( https://github.com/tidyverse/magrittr/workflows/R-CMD-check/badge.svg )] ( https://github.com/tidyverse/magrittr/actions )
11
+ coverage] ( https://codecov.io/gh/tidyverse/magrittr/branch/main/graph/badge.svg )] ( https://app.codecov.io/gh/tidyverse/magrittr?branch=main )
12
+ [ ![ R-CMD-check] ( https://github.com/smbache/magrittr/actions/workflows/R-CMD-check.yaml/badge.svg )] ( https://github.com/smbache/magrittr/actions/workflows/R-CMD-check.yaml )
14
13
<!-- badges: end -->
15
14
16
15
## Overview
17
16
18
17
The magrittr package offers a set of operators which make your code more
19
18
readable by:
20
19
21
- - structuring sequences of data operations left-to-right (as opposed
20
+ - structuring sequences of data operations left-to-right (as opposed
22
21
to from the inside and out),
23
- - avoiding nested function calls,
24
- - minimizing the need for local variables and function definitions,
22
+ - avoiding nested function calls,
23
+ - minimizing the need for local variables and function definitions,
25
24
and
26
- - making it easy to add steps anywhere in the sequence of operations.
25
+ - making it easy to add steps anywhere in the sequence of operations.
27
26
28
27
The operators pipe their left-hand side values forward into expressions
29
- that appear on the right-hand side, i.e. one can replace ` f(x) ` with `x
30
- %>% f()` , where ` %>%` is the (main) pipe-operator. When coupling several
31
- function calls with the pipe-operator, the benefit will become more
32
- apparent. Consider this pseudo example:
28
+ that appear on the right-hand side, i.e. one can replace ` f(x) ` with
29
+ ` x %>% f()` , where ` %>% ` is the (main) pipe-operator. When coupling
30
+ several function calls with the pipe-operator, the benefit will become
31
+ more apparent. Consider this pseudo example:
33
32
34
33
``` r
35
34
the_data <-
@@ -59,16 +58,16 @@ install.packages("magrittr")
59
58
60
59
# Or the development version from GitHub:
61
60
# install.packages("devtools")
62
- devtools :: install_github (" tidyverse/magrittr" )
61
+ pak :: pak (" tidyverse/magrittr" )
63
62
```
64
63
65
64
## Usage
66
65
67
66
### Basic piping
68
67
69
- - ` x %>% f ` is equivalent to ` f(x) `
70
- - ` x %>% f(y) ` is equivalent to ` f(x, y) `
71
- - ` x %>% f %>% g %>% h ` is equivalent to ` h(g(f(x))) `
68
+ - ` x %>% f ` is equivalent to ` f(x) `
69
+ - ` x %>% f(y) ` is equivalent to ` f(x, y) `
70
+ - ` x %>% f %>% g %>% h ` is equivalent to ` h(g(f(x))) `
72
71
73
72
Here, “equivalent” is not technically exact: evaluation is non-standard,
74
73
and the left-hand side is evaluated before passed on to the right-hand
@@ -77,8 +76,8 @@ implication.
77
76
78
77
### The argument placeholder
79
78
80
- - ` x %>% f(y, .) ` is equivalent to ` f(y, x) `
81
- - ` x %>% f(y, z = .) ` is equivalent to ` f(y, z = x) `
79
+ - ` x %>% f(y, .) ` is equivalent to ` f(y, x) `
80
+ - ` x %>% f(y, z = .) ` is equivalent to ` f(y, z = x) `
82
81
83
82
### Re-using the placeholder for attributes
84
83
@@ -87,14 +86,14 @@ right-hand side expression. However, when the placeholder only appears
87
86
in a nested expressions magrittr will still apply the first-argument
88
87
rule. The reason is that in most cases this results more clean code.
89
88
90
- ` x %>% f(y = nrow(.), z = ncol(.)) ` is equivalent to `f(x, y = nrow(x),
91
- z = ncol(x))`
89
+ ` x %>% f(y = nrow(.), z = ncol(.)) ` is equivalent to
90
+ ` f(x, y = nrow(x), z = ncol(x))`
92
91
93
92
The behavior can be overruled by enclosing the right-hand side in
94
93
braces:
95
94
96
- ` x %>% {f(y = nrow(.), z = ncol(.))} ` is equivalent to `f(y = nrow(x), z
97
- = ncol(x))`
95
+ ` x %>% {f(y = nrow(.), z = ncol(.))} ` is equivalent to
96
+ ` f(y = nrow(x), z = ncol(x))`
98
97
99
98
### Building (unary) functions
100
99
0 commit comments