debugging 提前返回dplyr管道结果[重复]

5gfr0r5j  于 2023-03-03  发布在  其他
关注(0)|答案(3)|浏览(111)
    • 此问题在此处已有答案**:

Stepping through a pipeline with intermediate results(5个答案)
Printing intermediate results without breaking pipeline in tidyverse(3个答案)
4小时前关门了。
在编码时,我经常想检查我正在处理的管道的中间结果。如果我正在处理一个长管道的早期部分,它需要相当多的点击/鼠标来选择性地运行并保存结果。有没有一种简洁的方法可以像下面这样做?

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()

以便在执行之后,result将结果保存在return_early(),而不执行流水线的其余部分?

d8tt03nd

d8tt03nd1#

我的习惯是注解掉(#)指向下一个命令的管道,然后运行代码(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()

这仍然需要点击几下来删除注解字符,希望看到其他的方法。

4si2a6ki

4si2a6ki2#

您可以将中间结果赋给一个临时对象temp,尽管整个管道仍然被求值并返回result作为输出。

library(dplyr)

result <- mtcars |>
  filter(cyl == 4) |>
  assign(x = 'temp') |>
  group_by(gear) |>
  summarise()

result
#    gear
# 1     3
# 2     4
# 3     5

temp
#                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
# Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
# Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
# Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
# ...

更新

受@Maël使用stop()的启发,我创建了一个包含切换的函数,以便您可以控制是否对剩余的管道求值。

return_early <- function(x, name = 'temp', eval_latter = FALSE){
  assign(name, x, pos = 1)
  if(!eval_latter) {
    stop("Intermediate object '", name, "' is created. ",
         "The remaining pipeline is omitted!", call. = FALSE)
  }
  return(x)
}
    • 测试1:**仅创建temp
result <- mtcars |>
  filter(cyl == 4) |>
  return_early() |>
  group_by(gear) |>
  summarise()

***错误:**创建了中间对象"temp"。省略了其余管道! *

    • 测试2:**同时创建resulttemp
result <- mtcars |>
  filter(cyl == 4) |>
  return_early(eval_latter = TRUE) |>
  group_by(gear) |>
  summarise()
c8ib6hqw

c8ib6hqw3#

您可以先执行assign,然后执行stop。它会抛出一个错误,但会停止计算:

result <- 
  mtcars |>
  filter(cyl == 4) |>
  assign(x = "early") |>
  stop() |>
  group_by(gear) |>
  summarise()

#Error in group_by(stop(assign(filter(mtcars, cyl == 4), x = "early")),  : 
#  c(22.8, 24.4, 22.8, 32.4, 30.4, 33.9, 21.5, 27.3, 26, 30.4, 21.4)c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4)c(108, 146.7, 140.8, 78.7, 75.7, 71.1, 120.1, 79, 120.3, 95.1, 121)c(93, 62, 95, 66, 52, 65, 97, 66, 91, 113, 109)c(3.85, 3.69, 3.92, 4.08, 4.93, 4.22, 3.7, 4.08, 4.43, 3.77, 4.11)c(2.32, 3.19, 3.15, 2.2, 1.615, 1.835, 2.465, 1.935, 2.14, 1.513, 2.78)c(18.61, 20, 22.9, 19.47, 18.52, 19.9, 20.01, 18.9, 16.7, 16.9, 18.6)c(1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)c(1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1)c(4, 4, 4, 4, 4, 4, 3, 4, 5, 5, 4)c(1, 2, 2, 1, 2, 1, 1, 1, 2, 2, 2)

#> result
#Error: object 'result' not found

#> early
#                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

相关问题