管道结果到多个参数(使用 R 4.1+)
Pipe result to multiple parameters (with R 4.1+)
使用 magrittr pipe (%>%
),我偶尔会将结果传递给多个参数,例如
ds <-
datasets::airquality |>
head()
ds %>%
# ds |>
knitr::kable(
x = .,
col.names = tolower(colnames(.)),
format = "markdown"
)
结果:
| 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|
但是R's new native pipe (|>
, introduced in 4.1.0)不支持这个。将 %>%
替换为 |>
会引发此错误:
Error in knitr::kable(head(datasets::airquality), x = ., col.names = tolower(colnames(.)), :
object '.' not found
release notes中的描述(我的重点):
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.
除了定义一个新的(匿名的或明确的)函数来包装下面提出的 rhs(右侧)函数之外,是否还有另一种使用 |>
的方法?
Jumping Rivers 的博客描述了如何使用匿名函数和新的本机管道 (a) 将值传递给不是第一个参数的参数,以及 (b) 将值传递给多个参数。对于上面的问题:
ds |>
{\(x)
knitr::kable(
x = x,
col.names = tolower(colnames(x)),
format = "markdown"
)
}() # Don't forget the parentheses.
这利用了 R 4.1.0 release 功能。
R now provides a shorthand notation for creating functions, e.g. (x) x + 1 is parsed as function(x) x + 1.
它比像
这样显式定义函数要简洁一些
kable2 <- function (x) {
knitr::kable(
x = x,
col.names = tolower(colnames(x)),
format = "markdown"
)
}
ds |>
kable2()
注意:如果您收到以下错误,您可能忘记了()
(即,opening & closing括号) 在定义匿名函数之后。
Error: function '{' not supported in RHS call of a pipe
正如 Keith McNulty 在最近 Medium post 中解释的那样,
用于 ”。”在新的 R 本机管道中删除了引用。相反,您必须按照建议使用中间函数,但也要利用
本机 |>
管道进入第一个 未命名 参数。
所以,如果您没有使用 col.names
参数,
library(dplyr)
ds <- datasets::airquality |> head()
ds %>%
knitr::kable(
x = .,
format = "markdown"
)
可以替换为
ds |>
knitr::kable(
format = "markdown"
)
因为 x
是 kable
的第一个未命名参数
处理第二个“.”参考,您可以先使用一个函数(在这种情况下为dplyr::rename_with
)
ds |>
dplyr::rename_with(tolower) |>
knitr::kable(
format = "markdown"
)
| 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 |
由 reprex package (v2.0.0)
于 2021-06-29 创建
使用 magrittr pipe (%>%
),我偶尔会将结果传递给多个参数,例如
ds <-
datasets::airquality |>
head()
ds %>%
# ds |>
knitr::kable(
x = .,
col.names = tolower(colnames(.)),
format = "markdown"
)
结果:
| 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|
但是R's new native pipe (|>
, introduced in 4.1.0)不支持这个。将 %>%
替换为 |>
会引发此错误:
Error in knitr::kable(head(datasets::airquality), x = ., col.names = tolower(colnames(.)), :
object '.' not found
release notes中的描述(我的重点):
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.
除了定义一个新的(匿名的或明确的)函数来包装下面提出的 rhs(右侧)函数之外,是否还有另一种使用 |>
的方法?
Jumping Rivers 的博客描述了如何使用匿名函数和新的本机管道 (a) 将值传递给不是第一个参数的参数,以及 (b) 将值传递给多个参数。对于上面的问题:
ds |>
{\(x)
knitr::kable(
x = x,
col.names = tolower(colnames(x)),
format = "markdown"
)
}() # Don't forget the parentheses.
这利用了 R 4.1.0 release 功能。
R now provides a shorthand notation for creating functions, e.g. (x) x + 1 is parsed as function(x) x + 1.
它比像
这样显式定义函数要简洁一些kable2 <- function (x) {
knitr::kable(
x = x,
col.names = tolower(colnames(x)),
format = "markdown"
)
}
ds |>
kable2()
注意:如果您收到以下错误,您可能忘记了()
(即,opening & closing括号) 在定义匿名函数之后。
Error: function '{' not supported in RHS call of a pipe
正如 Keith McNulty 在最近 Medium post 中解释的那样,
用于 ”。”在新的 R 本机管道中删除了引用。相反,您必须按照建议使用中间函数,但也要利用
本机 |>
管道进入第一个 未命名 参数。
所以,如果您没有使用 col.names
参数,
library(dplyr)
ds <- datasets::airquality |> head()
ds %>%
knitr::kable(
x = .,
format = "markdown"
)
可以替换为
ds |>
knitr::kable(
format = "markdown"
)
因为 x
是 kable
处理第二个“.”参考,您可以先使用一个函数(在这种情况下为dplyr::rename_with
)
ds |>
dplyr::rename_with(tolower) |>
knitr::kable(
format = "markdown"
)
| 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 |
由 reprex package (v2.0.0)
于 2021-06-29 创建