With the magrittr pipe (%>%
), I'd occasionally pipe the result to multiple parameters, like
ds <-
datasets::airquality |>
head()
ds %>%
# ds |>
knitr::kable(
x = .,
col.names = tolower(colnames(.)),
format = "markdown"
)
result:
| ozone| solar.r| wind| temp| month| day|
|-----:|-------:|----:|----:|-----:|---:|
| 41| 190| 7.4| 67| 5| 1|
| 36| 118| 8.0| 72| 5| 2|
| 12| 149| 12.6| 74| 5| 3|
| 18| 313| 11.5| 62| 5| 4|
| NA| NA| 14.3| 56| 5| 5|
| 28| NA| 14.9| 66| 5| 6|
But R's new native pipe (|>
, introduced in 4.1.0) does not support this. Replacing %>%
with |>
throws this error:
Error in knitr::kable(head(datasets::airquality), x = ., col.names = tolower(colnames(.)), :
object '.' not found
The description in the release notes (my emphasis):
R now provides a simple native forward pipe syntax |>. The simple form of the forward pipe inserts the left-hand side as the first argument in the right-hand side call. The pipe implementation as a syntax transformation was motivated by suggestions from Jim Hester and Lionel Henry.
Other than defining a new (anonymous or explicit) function that wraps the rhs (right-hand side) function proposed below, is there another approach using |>
?