Skip to content

Commit

Permalink
Update pipe (R and others) sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
aphalo committed Jul 26, 2023
1 parent e0bca33 commit 470ecdc
Show file tree
Hide file tree
Showing 26 changed files with 29,839 additions and 475,454 deletions.
140 changes: 65 additions & 75 deletions R.data.Rnw

Large diffs are not rendered by default.

34 changes: 27 additions & 7 deletions R.scripts.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -291,35 +291,55 @@ How can \emph{pipes} exist within a single \Rlang script? When chaining function

What do pipes achieve in \Rlang scripts? They relieve us from the responsibility of creating and deleting the temporary objects and of enforcing the sequential execution of the different steps. Pipes usually improve readability of scripts by allowing more concise code.

Since year 2021, starting from version 4.1.0, \Rlang includes a native pipe operator (\Roperator{\textbar >}) as part of the language. Subsequently, the placeholder (\code{\_}) was implemented in version 4.2.0. Another two implementations of pipes, that have been available as \Rlang extensions for some years in packages \pkgnameNI{magrittr} and \pkgnameNI{wrapr}, are described in chapter \ref{chap:R:data} on page \pageref{chap:R:data}.
Since year 2021, starting from version 4.1.0, \Rlang includes a native pipe operator (\Roperator{\textbar >}) as part of the language. Subsequently, the placeholder (\code{\_}) was implemented in version 4.2.0 and its functionality expanded in version 4.3.0. Another two implementations of pipes, that have been available as \Rlang extensions for some years in packages \pkgnameNI{magrittr} and \pkgnameNI{wrapr}, are described in chapter \ref{chap:R:data} on page \pageref{chap:R:data}.

We describe R's pipe syntax based on R 4.2.0. We start by showing the same operations coded using nested function calls, using explicit saving of intermediate values in temporary objects, and using the pipe operator.
We describe R's pipe syntax based on R 4.3.0. We start by showing the same operations coded using nested function calls, using explicit saving of intermediate values in temporary objects, and using the pipe operator.

Nested function calls are concise, but difficult to read when the depth of nesting increases.

<<pipes-r-02>>=
data.out <- sum(sqrt(1:10))
sum(sqrt(1:10))
@

Saving intermediate results explicitly results in clear but verbose code.

<<pipes-r-03>>=
data.in <- 1:10
data.tmp <- sqrt(data.in)
data.out <- sum(data.tmp)
sum(data.tmp)
rm(data.tmp) # clean up!
@

A pipe using operator \Roperator{\textbar >} makes the data flow clear and keeps the code concise.

<<pipes-r-04>>=
1:10 |> sqrt() |> sum() -> data.out
1:10 |> sqrt() |> sum()
@

The \Roperator{\textbar >} operator from base \Rlang takes two operands. The value returned by the \emph{lhs} (left-hand side) operand, which can be any \Rlang expression, is passed by default as the first argument to the \emph{rhs} operand, which must be a function accepting at least one argument. Consequently, in using this simple syntax that implicitly passes the argument by position, the function in the \emph{rhs} must have a suitable signature for the pipe to work. However, it is possible to pass the piped argument also explicitly by name to any parameter of a function, including the first one, using an underscore (\code{\_}) as placeholder.
We can assign the result of the computation to a variable, most elegantly using the \Roperator{->} operator on the \emph{rhs} of the pipe.

<<pipes-r-04a>>=
1:10 |> sqrt() |> sum() -> my_rhs.var
my_rhs.var
@

We can also use the \Roperator{<-} operator on the \emph{lhs} of the pipe, i.e., for assignments a pipe behaves as a compound statement.

<<pipes-r-04b>>=
my_lhs.var <- 1:10 |> sqrt() |> sum()
my_lhs.var
@

Formally, the \Roperator{\textbar >} operator from base \Rlang takes two operands, just like operator \code{+} does. The value returned by the \emph{lhs} (left-hand side) operand, which can be any \Rlang expression, is passed as argument to the function-call operand on \emph{rhs} (right-hand side). The called function must accept at least one argument. This default syntax that implicitly passes the argument by position to the first parameter of the function would limit which functions could be used in a pipe construct. However, it is also possible to pass the piped argument explicitly by name to any parameter of the function on the \emph{rhs} using an underscore (\code{\_}) as placeholder.

<<pipes-r-05>>=
1:10 |> sqrt(x = _) |> sum(x = _) -> data.out
1:10 |> sqrt(x = _) |> sum(x = _)
@

The placeholder can be also used with extraction operators.

<<pipes-r-05a>>=
1:10 |> sqrt(x = _) |> _[2:8] |> sum(x = _)
@

\begin{explainbox}
Expand Down
43 changes: 20 additions & 23 deletions appendixes.prj
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,55 @@
1
1
using-r-main-crc.Rnw
20
14
0
19
15
8

using-r-main-crc.Rnw
TeX:RNW:UTF-8
402653186 0 4 30 4 61 6074 -1 6458 208 1 1 849 92 1 906 254 255 -1 0 0 33 1 0 61 4 0 -1 0
134217730 0 216 56 216 60 6074 -1 6458 208 1 1 974 1050 1 906 254 255 -1 0 0 33 1 0 60 216 0 -1 0
preface.Rnw
TeX:RNW
1060859 0 -1 5790 -1 7346 266 266 1252 1324 1 1 321 621 -1 -1 0 0 18 -1 -1 18 1 0 7346 -1 0 -1 0
1060859 0 -1 5790 -1 7346 266 266 1252 1324 1 1 1674 600 -1 -1 0 0 18 -1 -1 18 1 0 7346 -1 0 -1 0
R.learning.Rnw
TeX:RNW
17838075 0 -1 22561 -1 22561 304 304 1290 1362 1 1 549 553 -1 -1 0 0 40 -1 -1 40 1 0 22561 -1 0 -1 0
17838075 0 -1 22561 -1 22561 304 304 1290 1362 1 1 638 550 -1 -1 0 0 40 -1 -1 40 1 0 22561 -1 0 -1 0
R.as.calculator.Rnw
TeX:RNW
17838075 0 -1 98905 -1 0 190 190 1324 1242 1 1 129 0 -1 -1 0 0 31 -1 -1 31 3 0 0 -1 1 70542 -1 2 11719 -1 0 -1 0
17838075 0 -1 98905 -1 0 190 190 1324 1242 1 1 148 0 -1 -1 0 0 31 -1 -1 31 3 0 0 -1 1 70542 -1 2 11719 -1 0 -1 0
R.data.containers.Rnw
TeX:RNW
17838075 0 -1 45280 -1 45292 190 190 1324 1242 1 1 705 621 -1 -1 0 0 31 -1 -1 31 3 0 45292 -1 1 192 -1 2 192 -1 0 -1 0
17838075 0 -1 45280 -1 45292 190 190 1324 1242 1 1 821 600 -1 -1 0 0 31 -1 -1 31 3 0 45292 -1 1 192 -1 2 192 -1 0 -1 0
R.functions.Rnw
TeX:RNW
17838075 0 -1 31095 -1 94 456 456 1442 1484 1 1 477 46 -1 -1 0 0 31 -1 -1 31 1 0 94 -1 0 -1 0
17838075 0 -1 31095 -1 94 456 456 1442 1484 1 1 555 25 -1 -1 0 0 31 -1 -1 31 1 0 94 -1 0 -1 0
R.scripts.Rnw
TeX:RNW
17838075 0 -1 32451 -1 32451 152 152 1138 1210 1 1 129 621 -1 -1 0 0 31 -1 -1 31 2 0 32451 -1 1 56852 -1 0 -1 0
17838075 0 -1 30341 -1 30388 152 152 1138 1210 1 1 512 675 -1 -1 0 0 31 -1 -1 31 2 0 30388 -1 1 57458 -1 0 -1 0
R.stats.rnw
TeX:RNW
17838075 0 -1 16353 -1 16094 418 418 1404 1476 1 1 1461 184 -1 -1 0 0 31 -1 -1 31 1 0 16094 -1 0 -1 0
17838075 0 -1 16353 -1 16094 418 418 1404 1476 1 1 1702 175 -1 -1 0 0 31 -1 -1 31 1 0 16094 -1 0 -1 0
R.data.Rnw
TeX:RNW
1060859 0 -1 45852 -1 45915 342 342 1328 1400 1 1 597 92 -1 -1 0 0 31 -1 -1 31 1 0 45915 -1 0 -1 0
269496315 0 648 31 -1 38807 342 342 1328 1400 1 1 414 1100 -1 -1 0 0 31 -1 -1 31 1 0 38807 -1 0 -1 0
using-r-main-crc.tex
TeX
1060859 7 -1 56719 -1 56701 130 130 919 707 1 1 148 1025 -1 -1 0 0 49 -1 -1 49 1 0 56701 -1 0 -1 0
R.data.io.Rnw
TeX:RNW
17838075 0 -1 224 -1 225 494 494 1480 1522 1 1 585 184 -1 -1 0 0 31 -1 -1 31 1 0 225 -1 0 -1 0
17838075 1 -1 13190 -1 13203 494 494 1480 1522 1 1 817 400 -1 -1 0 0 31 -1 -1 31 1 0 13203 -1 0 -1 0
R.plotting.Rnw
TeX:RNW
17838075 0 -1 225 -1 226 380 380 1366 1438 1 1 549 184 -1 -1 0 0 31 -1 -1 31 1 0 226 -1 0 -1 0
17838075 0 -1 225 -1 226 380 380 1366 1438 1 1 672 175 -1 -1 0 0 31 -1 -1 31 1 0 226 -1 0 -1 0
rbooks.bib
BibTeX
1049586 0 785 7 782 2 38 38 1429 959 1 1 141 621 -1 -1 0 0 21 0 0 21 1 0 2 782 0 -1 0
1049586 0 785 7 782 2 38 38 1429 959 1 1 162 600 -1 -1 0 0 21 0 0 21 1 0 2 782 0 -1 0
references.bib
BibTeX
1049586 2 436 21 436 25 76 76 1467 997 1 1 417 575 -1 -1 0 0 23 0 0 23 1 0 25 436 0 -1 0
1049586 2 436 21 436 25 76 76 1467 997 1 1 484 575 -1 -1 0 0 23 0 0 23 1 0 25 436 0 -1 0
usingr.sty
TeX:STY
1060850 1 59 13 59 23 190 190 1176 1248 1 0 376 414 -1 -1 0 0 25 0 0 25 1 0 23 59 0 0 0
using-r-main-crc.tex
TeX
269496315 0 -1 173 -1 73 130 130 919 707 1 1 129 69 -1 -1 0 0 49 -1 -1 49 1 0 73 -1 0 -1 0
:\Program Files\MiKTeX 2.9\tex\latex\biblatex\biblatex.sty
TeX:STY:UNIX
269594610 0 0 1 0 1 192 192 1677 639 1 0 112 0 -1 -1 0 0 42 0 0 42 1 0 1 0 0 0 0
1060850 1 59 13 59 23 190 190 1176 1248 1 0 437 -275 -1 -1 0 0 25 0 0 25 1 0 23 59 0 0 0
using-r-main-crc.idx
TeX:AUX:UNIX
269594610 1 0 1 0 62 266 266 1657 1157 1 0 844 0 -1 -1 0 0 51 0 0 51 1 0 62 0 0 0 0
Expand Down
43 changes: 20 additions & 23 deletions appendixes.prj.bak
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,55 @@
1
1
using-r-main-crc.Rnw
20
14
0
19
15
8

using-r-main-crc.Rnw
TeX:RNW:UTF-8
402653186 4 4 32 4 33 6074 -1 6458 208 1 1 513 92 1 906 254 255 -1 0 0 33 1 0 33 4 0 -1 0
134217730 0 216 56 216 60 6074 -1 6458 208 1 1 974 1050 1 906 254 255 -1 0 0 33 1 0 60 216 0 -1 0
preface.Rnw
TeX:RNW
1060859 0 -1 5790 -1 7346 266 266 1252 1324 1 1 321 621 -1 -1 0 0 18 -1 -1 18 1 0 7346 -1 0 -1 0
1060859 0 -1 5790 -1 7346 266 266 1252 1324 1 1 1674 600 -1 -1 0 0 18 -1 -1 18 1 0 7346 -1 0 -1 0
R.learning.Rnw
TeX:RNW
17838075 0 -1 22561 -1 22561 304 304 1290 1362 1 1 549 553 -1 -1 0 0 40 -1 -1 40 1 0 22561 -1 0 -1 0
17838075 0 -1 22561 -1 22561 304 304 1290 1362 1 1 638 550 -1 -1 0 0 40 -1 -1 40 1 0 22561 -1 0 -1 0
R.as.calculator.Rnw
TeX:RNW
17838075 0 -1 98905 -1 0 190 190 1324 1242 1 1 129 0 -1 -1 0 0 31 -1 -1 31 3 0 0 -1 1 70542 -1 2 11719 -1 0 -1 0
17838075 0 -1 98905 -1 0 190 190 1324 1242 1 1 148 0 -1 -1 0 0 31 -1 -1 31 3 0 0 -1 1 70542 -1 2 11719 -1 0 -1 0
R.data.containers.Rnw
TeX:RNW
17838075 0 -1 45280 -1 45292 190 190 1324 1242 1 1 705 621 -1 -1 0 0 31 -1 -1 31 3 0 45292 -1 1 192 -1 2 192 -1 0 -1 0
17838075 0 -1 45280 -1 45292 190 190 1324 1242 1 1 821 600 -1 -1 0 0 31 -1 -1 31 3 0 45292 -1 1 192 -1 2 192 -1 0 -1 0
R.functions.Rnw
TeX:RNW
17838075 0 -1 31095 -1 94 456 456 1442 1484 1 1 477 46 -1 -1 0 0 31 -1 -1 31 1 0 94 -1 0 -1 0
17838075 0 -1 31095 -1 94 456 456 1442 1484 1 1 555 25 -1 -1 0 0 31 -1 -1 31 1 0 94 -1 0 -1 0
R.scripts.Rnw
TeX:RNW
17838075 0 -1 32451 -1 32451 152 152 1138 1210 1 1 129 621 -1 -1 0 0 31 -1 -1 31 2 0 32451 -1 1 56852 -1 0 -1 0
17838075 0 -1 30341 -1 30388 152 152 1138 1210 1 1 512 675 -1 -1 0 0 31 -1 -1 31 2 0 30388 -1 1 57458 -1 0 -1 0
R.stats.rnw
TeX:RNW
17838075 0 -1 16353 -1 16094 418 418 1404 1476 1 1 1461 184 -1 -1 0 0 31 -1 -1 31 1 0 16094 -1 0 -1 0
17838075 0 -1 16353 -1 16094 418 418 1404 1476 1 1 1702 175 -1 -1 0 0 31 -1 -1 31 1 0 16094 -1 0 -1 0
R.data.Rnw
TeX:RNW
1060859 0 -1 45852 -1 45915 342 342 1328 1400 1 1 597 92 -1 -1 0 0 31 -1 -1 31 1 0 45915 -1 0 -1 0
269496315 0 648 31 -1 38807 342 342 1328 1400 1 1 414 1100 -1 -1 0 0 31 -1 -1 31 1 0 38807 -1 0 -1 0
using-r-main-crc.tex
TeX
1060859 7 -1 56719 -1 56701 130 130 919 707 1 1 148 1025 -1 -1 0 0 49 -1 -1 49 1 0 56701 -1 0 -1 0
R.data.io.Rnw
TeX:RNW
17838075 0 -1 224 -1 225 494 494 1480 1522 1 1 585 184 -1 -1 0 0 31 -1 -1 31 1 0 225 -1 0 -1 0
17838075 1 -1 13190 -1 13203 494 494 1480 1522 1 1 817 400 -1 -1 0 0 31 -1 -1 31 1 0 13203 -1 0 -1 0
R.plotting.Rnw
TeX:RNW
17838075 0 -1 225 -1 226 380 380 1366 1438 1 1 549 184 -1 -1 0 0 31 -1 -1 31 1 0 226 -1 0 -1 0
17838075 0 -1 225 -1 226 380 380 1366 1438 1 1 672 175 -1 -1 0 0 31 -1 -1 31 1 0 226 -1 0 -1 0
rbooks.bib
BibTeX
1049586 0 785 7 782 2 38 38 1429 959 1 1 141 621 -1 -1 0 0 21 0 0 21 1 0 2 782 0 -1 0
1049586 0 785 7 782 2 38 38 1429 959 1 1 162 600 -1 -1 0 0 21 0 0 21 1 0 2 782 0 -1 0
references.bib
BibTeX
1049586 2 436 21 436 25 76 76 1467 997 1 1 417 575 -1 -1 0 0 23 0 0 23 1 0 25 436 0 -1 0
1049586 2 436 21 436 25 76 76 1467 997 1 1 484 575 -1 -1 0 0 23 0 0 23 1 0 25 436 0 -1 0
usingr.sty
TeX:STY
1060850 1 59 13 59 23 190 190 1176 1248 1 0 376 414 -1 -1 0 0 25 0 0 25 1 0 23 59 0 0 0
using-r-main-crc.tex
TeX
269496315 0 -1 173 -1 73 130 130 919 707 1 1 129 69 -1 -1 0 0 49 -1 -1 49 1 0 73 -1 0 -1 0
:\Program Files\MiKTeX 2.9\tex\latex\biblatex\biblatex.sty
TeX:STY:UNIX
269594610 0 0 1 0 1 192 192 1677 639 1 0 112 0 -1 -1 0 0 42 0 0 42 1 0 1 0 0 0 0
1060850 1 59 13 59 23 190 190 1176 1248 1 0 437 -275 -1 -1 0 0 25 0 0 25 1 0 23 59 0 0 0
using-r-main-crc.idx
TeX:AUX:UNIX
269594610 1 0 1 0 62 266 266 1657 1157 1 0 844 0 -1 -1 0 0 51 0 0 51 1 0 62 0 0 0 0
Expand Down
Loading

0 comments on commit 470ecdc

Please sign in to comment.