尽早返回 dplyr 管道结果

Jon*_*løv 6 debugging pipeline r dplyr

编码时,我经常想检查我正在处理的管道的中间结果。如果我正在处理长管道的早期部分,则需要多次点击/鼠标才能有选择地运行并保存结果。有没有一种巧妙的方法可以做类似以下的事情?

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()而不执行管道的其余部分?

请注意,我询问的是一种保存中间输出并停止评估的便捷方法。如果您对打印感兴趣,请参阅此处此处

ben*_*n23 3

我的习惯是注释掉 ( #) 通向下一个命令的管道,然后运行代码(Macbook cmd+enter或 Windows ctrl+ enter)。

检查结果后,只需删除注释字符 ( #) 即可继续。

library(tidyverse)

result = mtcars |>
  filter(cyl == 4) #|> <- run the code here, the rest would be ignored
  group_by(gear) |>
  summarise()
Run Code Online (Sandbox Code Playgroud)

这仍然需要点击几下才能删除评论字符,很想看看其他人的方法。