renkun-ken / pipeR

Multi-Paradigm Pipeline Implementation
Other
167 stars 39 forks source link

Leaked variable #71

Open kindlychung opened 7 years ago

kindlychung commented 7 years ago
f <- function(x, y, z = "nothing") {
  cat("x =", x, "\n")
  cat("y =", y, "\n")
  cat("z =", z, "\n")
}
1:10 %>>% {
  x = 3
  f(x, .)
  f(min(.), max(.))
}
x

It would be desirable not have intermediate variables leak into the global environment.

Thanks for the nice work!

timelyportfolio commented 7 years ago

I can't speak for @renkun-ken, but changing this would change expected R behavior. You could either wrap in a function for scoping or use local.

1:10 %>>% {local({x=10;print(.)})}
x

This is even uglier but does work.

1:10 %>>% (function(.){x=10;print(.)})()
x