r - How can you tell if a pipe operator is the last (or first) in a chain? -
i have been playing creating own pipes, using awesome pipe_with()
function in magittr
. looking track number of pipes in current chain (so pipe can behave differently depending on position in chain). thought had answer example magrittr
github page:
# create own pipe side-effects. in example # create pipe "logging" function traces # left-hand sides of chain. first, logger: lhs_trace <- local({ count <- 0 function(x) { count <<- count + 1 cl <- match.call() cat(sprintf("%d: lhs = %s\n", count, deparse(cl[[2]]))) } }) # attach new pipe `%l>%` <- pipe_with(lhs_trace) # try 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
the number on left hand side pipe number. however, when run same chain again, numbers don't restart @ 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
this presumably because local environment created first use of %l>%
not destroyed when last %l>%
in chain executed. in order tell position of pipe in current chain (and not since first pipe in session), there needs way set count variable 0 when chain ends (or reset local environment).
does have ideas on how this?
in current dev
branch, working new approach, due compound operator, %<>%
, last pipe has know last. anyway, implication pipe relatively has knowledge through local value toplevel
either true or false. don't know whether of use.
in particular because pipe_with
"on hold" due limited interest received in it. therefore not part of current dev
branch.
Comments
Post a Comment