穿越具有中间结果的流水线

18

有没有一种方法可以在不手动运行的情况下输出每个步骤管道的结果?(例如,无需选择并仅运行所选块)

我经常逐行运行管道以记住它正在做什么或者当我在开发一些分析时。

例如:

library(dplyr)

mtcars %>% 
  group_by(cyl) %>% 
  sample_frac(0.1) %>% 
  summarise(res = mean(mpg))
# Source: local data frame [3 x 2]
# 
# cyl  res
# 1   4 33.9
# 2   6 18.1
# 3   8 18.7

我需要选择并运行:

mtcars %>% group_by(cyl)

然后...

mtcars %>% group_by(cyl) %>% sample_frac(0.1)

等等之类的...

RStudio 中选择并按下 CMD/CTRL+ENTER 留下了更有效的方法。

这能在代码中完成吗?

是否有一个函数可以接受管道并逐行运行/消化它,在控制台上显示每个步骤的输出,然后通过按 Enter 继续,就像包指南中的 demos(...)examples(...) 一样?


看看R的debug()函数,它接近你想要的。你可以与print()语句一起使用它。这篇关于交叉验证的帖子更多地讨论了它。 - Richard Erickson
你可以简单地使用 %>% print() %>% - 参见此回答:https://dev59.com/BVYO5IYBdhLWcg3wPvRv#54075410 - Emy
5个回答

10

您可以使用tee运算符 (%T>%) 和print() 来选择要打印的结果。 tee运算符仅用于像打印这样的副作用。

# i.e.
mtcars %>%
  group_by(cyl) %T>% print() %>%
  sample_frac(0.1) %T>% print() %>%
  summarise(res = mean(mpg))

2
当输出为数据框时,我发现使用 %T>% View() %>% 很有用,可以查看中间结果。 - see24

3
使用magrittr函数链非常容易。例如,可以通过以下方式定义一个名为my_chain的函数:
foo <- function(x) x + 1
bar <- function(x) x + 1
baz <- function(x) x + 1
my_chain <- . %>% foo %>% bar %>% baz

并获取链的最终结果:

     > my_chain(0)
    [1] 3

您可以通过functions(my_chain)获取函数列表,并定义一个“stepper”函数,如下所示:
stepper <- function(fun_chain, x, FUN = print) {
  f_list <- functions(fun_chain)
  for(i in seq_along(f_list)) {
    x <- f_list[[i]](x)
    FUN(x)
  }
  invisible(x)
}

并使用插入print函数的方式运行链:

stepper(my_chain, 0, print)

# [1] 1
# [1] 2
# [1] 3

或者等待用户输入:

stepper(my_chain, 0, function(x) {print(x); readline()})

2
我写了一个名为pipes的包,可以做一些有用的事情:
  • 使用%P>%将输出打印出来。
  • 使用%ae>%在输入和输出上使用all.equal
  • 使用%V>%在输出上使用View,它会为每个相关步骤打开一个查看器。

如果您想查看一些聚合信息,可以尝试%summary>%%glimpse>%%skim>%,它们将使用summarytibble::glimpseskimr::skim,或者您可以定义自己的管道以显示特定的更改,使用new_pipe

# devtools::install_github("moodymudskipper/pipes")
library(dplyr)
library(pipes)

res <- mtcars %P>% 
  group_by(cyl) %P>% 
  sample_frac(0.1) %P>% 
  summarise(res = mean(mpg))
#> group_by(., cyl)
#> # A tibble: 32 x 11
#> # Groups:   cyl [3]
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>  * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ... with 22 more rows
#> sample_frac(., 0.1)
#> # A tibble: 3 x 11
#> # Groups:   cyl [3]
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  26       4  120.    91  4.43  2.14  16.7     0     1     5     2
#> 2  17.8     6  168.   123  3.92  3.44  18.9     1     0     4     4
#> 3  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#> summarise(., res = mean(mpg))
#> # A tibble: 3 x 2
#>     cyl   res
#>   <dbl> <dbl>
#> 1     4  26  
#> 2     6  17.8
#> 3     8  18.7

res <- mtcars %ae>% 
  group_by(cyl) %ae>% 
  sample_frac(0.1) %ae>% 
  summarise(res = mean(mpg))
#> group_by(., cyl)
#> [1] "Attributes: < Names: 1 string mismatch >"                                              
#> [2] "Attributes: < Length mismatch: comparison on first 2 components >"                     
#> [3] "Attributes: < Component \"class\": Lengths (1, 4) differ (string compare on first 1) >"
#> [4] "Attributes: < Component \"class\": 1 string mismatch >"                                
#> [5] "Attributes: < Component 2: Modes: character, list >"                                   
#> [6] "Attributes: < Component 2: Lengths: 32, 2 >"                                           
#> [7] "Attributes: < Component 2: names for current but not for target >"                     
#> [8] "Attributes: < Component 2: Attributes: < target is NULL, current is list > >"          
#> [9] "Attributes: < Component 2: target is character, current is tbl_df >"
#> sample_frac(., 0.1)
#> [1] "Different number of rows"
#> summarise(., res = mean(mpg))
#> [1] "Cols in y but not x: `res`. "                                                                
#> [2] "Cols in x but not y: `qsec`, `wt`, `drat`, `hp`, `disp`, `mpg`, `carb`, `gear`, `am`, `vs`. "

res <- mtcars %V>% 
  group_by(cyl) %V>% 
  sample_frac(0.1) %V>% 
  summarise(res = mean(mpg))
# you'll have to test this one by yourself

2
添加打印功能:
mtcars %>% 
  group_by(cyl) %>% 
  print %>% 
  sample_frac(0.1) %>% 
  print %>% 
  summarise(res = mean(mpg))

我明白 print 返回其参数,所以这样做是可以的,但它并没有比手动选择和运行代码块更短/更快/更方便。 - andrew wong
@andrewwong请告诉我们更多,为什么您需要逐行运行它,更重要的是,为什么您想逐个查看打印输出? - zx8754
1
更新的问题。我想要在控制台中像交互式步进器一样,或者是一个自动生成所有中间文件的自动化Markdown文档。感谢您的想法! - andrew wong

2

在我看来,magrittr 在交互式使用时最为有用,也就是当我在探索数据或构建新的公式/模型时。

在这些情况下,将中间结果存储在不同的变量中会非常耗时且分散注意力,而管道使我可以专注于数据而不是打字:

x %>% foo
## reason on results and 
x %>% foo %>% bar
## reason on results and 
x %>% foo %>% bar %>% baz
## etc.

这里的问题在于我事先不知道最终的管道会是什么,就像@bergant中的情况一样。
输入方式与@zx8754类似。
x %>% print %>% foo %>% print %>% bar %>% print %>% baz

添加太多的开销,对我来说,这完全违背了magrittr的初衷。

本质上,magrittr缺少一个既打印管道传递结果的简单操作符。
好消息是,似乎很容易制作一个:

`%P>%`=function(lhs, rhs){ print(lhs); lhs %>% rhs }

现在您可以打印一个管道:
1:4 %P>% sqrt %P>% sum 
## [1] 1 2 3 4
## [1] 1.000000 1.414214 1.732051 2.000000
## [1] 6.146264

我发现,如果定义/使用%P>%%>%的键绑定,原型工作流程会变得非常简化(请参见Emacs ESSRStudio)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接