我一直在尝试%>%从magrittr包中越来越受欢迎的操作员.
我已经用得足够了,我设置了一个键盘快捷键来保存我输入:
shift+ command+ .代替space,shift+ 5,shift+ .,shift+ 5, space.

这是很大SublimeTetxt2但Rstudio不允许的服务,如果我Rstudio项目中的工作这是行不通的.
这与作为水獭氧气的赋值算子的alt+ -绑定完全同义.<-
如果有人能指出我在github仓库中编写快捷方式的位置,那么我可以"破解"我自己的,我想这将是一个有用的开始.
是否有一个命令可以添加到 tidyverse 管道中,该命令不会中断流程,但会产生一些副作用,例如打印出来的东西。我想到的用例是这样的。如果是管道
data %>%
mutate(new_var = <some time consuming operation>) %>%
mutate(new_var2 = <some other time consuming operation>) %>%
...
Run Code Online (Sandbox Code Playgroud)
我想向管道添加一些不会修改最终结果的命令,但会打印出一些进度或事情的状态。也许是这样的:
data %>%
mutate(new_var = <some time consuming operation>) %>%
command_x(print("first operation done")) %>%
mutate(new_var2 = <some other time consuming operation>) %>%
...
Run Code Online (Sandbox Code Playgroud)
command_x已经存在这样的吗?
编码时,我经常想检查我正在处理的管道的中间结果。如果我正在处理长管道的早期部分,则需要多次点击/鼠标才能有选择地运行并保存结果。有没有一种巧妙的方法可以做类似以下的事情?
library(dplyr)
result = mtcars |>
# Testing this step
filter(cyl == 4) |>
return_early() |>
# I don't want to run the rest of the pipeline
group_by(gear) |>
summarise()
Run Code Online (Sandbox Code Playgroud)
这样执行后,result将保持结果return_early()而不执行管道的其余部分?
当我想估计 R 代码的运行时间时,我使用函数system.time().
library(dplyr)
system.time({
Titanic %>%
as.data.frame() %>%
mutate(Dataset = 1) %>%
bind_rows(as.data.frame(Titanic)) %>%
mutate_all(funs(replace_na(., NA))) %>%
filter(Dataset != 1)
})
# utilisateur système écoulé
# 0.02 0.00 0.02
Run Code Online (Sandbox Code Playgroud)
问题:
有没有办法知道每个操作的运行时间,每个管道之间的操作(the mutate, then the bind_rows, then thefilter等),而不用一个一个地运行一个或者不写几个system.time()?
在这个例子中它没有用,但有时我收到一个很长的脚本,运行时间很长,我想确定哪些操作是最低的。
我做了一些研究,但没有找到有用的东西。