【发布时间】:2014-08-16 18:13:03
【问题描述】:
我最近一直在玩创建自己的管道,使用 magittr 中很棒的 pipe_with() 函数。我正在寻找跟踪当前链中管道的数量(因此我的管道可以根据其在链中的位置而表现不同)。我想我已经从magrittr github 页面得到了这个例子的答案:
# Create your own pipe with side-effects. In this example
# we create a pipe with a "logging" function that traces
# the left-hand sides of a chain. First, the logger:
lhs_trace <- local({
count <- 0
function(x) {
count <<- count + 1
cl <- match.call()
cat(sprintf("%d: lhs = %s\n", count, deparse(cl[[2]])))
}
})
# Then attach it to a new pipe
`%L>%` <- pipe_with(lhs_trace)
# Try it out.
1:10 %L>% sin %L>% cos %L>% abs
1: lhs = 1:10
2: lhs = 1:10 %L>% sin
3: lhs = 1:10 %L>% sin %L>% cos
[1] 0.6663667 0.6143003 0.9900591 0.7270351 0.5744009 0.9612168 0.7918362 0.5492263 0.9162743 0.8556344
左边的数字是管道号。但是,当我再次运行相同的链时,数字不会在 1 处重新开始:
> 1:10 %L>% sin %L>% cos %L>% abs
4: lhs = 1:10
5: lhs = 1:10 %L>% sin
6: lhs = 1:10 %L>% sin %L>% cos
[1] 0.6663667 0.6143003 0.9900591 0.7270351 0.5744009 0.9612168 0.7918362 0.5492263 0.9162743 0.8556344
这大概是因为第一次使用%L>%创建的本地环境在执行链中最后一个%L>%时没有被破坏。因此,为了知道管道在当前链中的位置(不仅仅是从会话中的第一个管道开始),需要有一种方法在链结束时将 count 变量设置回 0(或重置本地环境)。
有人对如何做到这一点有任何想法吗?
【问题讨论】: