r - 通过管道传递时函数内控制台的消息顺序

标签 r pipe

为了跟踪某些函数的处理情况,我想在函数启动和完成时在控制台中打印消息。

我是 tidyverse 和管道运算符 %>% 的忠实粉丝。但我刚刚发现函数开头的消息以错误的顺序打印,而末尾的消息则以正确的顺序打印。

有人知道为什么或如何解决这个问题吗?

这是一些代码:

library(tidyverse)
testdata <- data.frame(a=c(3,5,7,1,2),b=c(2,5,6,3,9))

foo_1 <- function(DF){
  message("Hello 1")
  Obj <- DF %>% 
    mutate(Test_1=a*b)
  message("Bye 1")
  return(Obj)
} 

foo_2 <- function(DF){
  message("Hello 2")
  
  Obj <- DF %>% 
    mutate(Test_2=Test_1*2)
  message("Bye 2")
  return(Obj)
}

foo_3 <- function(DF){
  message("Hello 3")
  Obj <- DF %>% 
    mutate(Test_3=Test_2/a)
  
  message("Bye 3")
  return(Obj)
}

testdata %>% 
  foo_1() %>% 
  foo_2() %>% 
  foo_3()

控制台打印的结果为:

Hello 3
Hello 2
Hello 1
Bye 1
Bye 2
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

我期望的是以下消息顺序:

Hello 1
Bye 1
Hello 2
Bye 2
Hello 3
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

最佳答案

1)flush.console 使用flush.console强制显示当时排队的消息。

testdata %>% 
  foo_1() %>% 
  { flush.console(); foo_2(.) } %>% 
  { flush.console(); foo_3(.) }

给出这个输出:

Hello 1
Bye 1
Hello 2
Bye 2
Hello 3
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

2) Bizarro 管道 另一种方法是使用 Bizarro pipe ,这只是聪明的基本语法,看起来像管道。

testdata ->.;
  foo_1(.) ->.;
  foo_2(.) ->.;
  foo_3(.)

给予:

Hello 1
Bye 1
Hello 2
Bye 2
Hello 3
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

3)force 强制对三个函数中的参数求值。

foo_1 <- function(DF){
  force(DF) #######
  message("Hello 1")
  Obj <- DF %>% 
    mutate(Test_1=a*b)
  message("Bye 1")
  return(Obj)
} 

foo_2 <- function(DF){
  force(DF) #######
  message("Hello 2")
  Obj <- DF %>% 
    mutate(Test_2=Test_1*2)
  message("Bye 2")
  return(Obj)
}

foo_3 <- function(DF){
  force(DF) #######
  message("Hello 3")
  Obj <- DF %>% 
    mutate(Test_3=Test_2/a)
  
  message("Bye 3")
  return(Obj)
}

testdata %>% 
  foo_1 %>%
  foo_2 %>%
  foo_3

给予:

Hello 1
Bye 1
Hello 2
Bye 2
Hello 3
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

4)pipe_eager_lexical magrittr 开发人员 Lionel 向我指出 magrittr 有一个 eager 管道,但没有将其分配给 %..% 运算符;但是,我们可以轻松做到这一点。

library(magrittr)

`%>e%` <- pipe_eager_lexical  # eager pipe

testdata %>e% 
  foo_1() %>e% 
  foo_2() %>e% 
  foo_3()

给予:

Hello 1
Bye 1
Hello 2
Bye 2
Hello 3
Bye 3
  a b Test_1 Test_2 Test_3
1 3 2      6     12      4
2 5 5     25     50     10
3 7 6     42     84     12
4 1 3      3      6      6
5 2 9     18     36     18

关于r - 通过管道传递时函数内控制台的消息顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68131615/

相关文章:

java - 用于图像处理应用程序的 C++ 和 Java 通信

c - 在子进程和父进程之间发送数据时如何使用两个管道?

r - 管道会影响 sqrt 函数的操作吗?

r - 是否可以在合并中使用列索引?

r - 如何在 RStudio 中安装.packages corrplot?

r - 在 R 中使用 Azure 计算机视觉创建循环/分析多个图像的方法

r - 为什么从 data.table 中选择列会产生副本?

bash - 管道和重定向

Angular2 Dart 英雄之旅 : uppercase

r - 获取下一年的周数的连续值