Closed DanChaltiel closed 3 years ago
This is by design. You need to force()
arguments to force sequential evaluation, see https://www.tidyverse.org/blog/2020/11/magrittr-2-0-is-here/#sequential-evaluation. In your case this would fix it:
import_sum <- function(df){
force(df)
if(is.null(my_env$sum)) {
warning("env variable is null")
}
df$sum <- my_env$sum
df
}
@lionel- Thanks a lot, this works!
Sorry I missed the information, I was mostly looking at NEWS.md, which I think is a bit less clear than the blog post you just provided. You might want to at least put "Sequential evaluation" in the title.
If you want some well deserved SO points, I will gladly accept your answer there as well.
Can you provide the answer on SO please? Thanks!
@lionel- Done, thank you very much
When you modify an environment variable from inside a pipeline, it seems that the modification only occurs at the end of the pipeline.
For instance, if I set two functions, one for setting the variable and the other for getting it, and I call them on the same pipeline, the getter will only consider the environment as it was before the pipeline was called.
Here is an overcomplicated reprex:
Created on 2021-01-30 by the reprex package (v0.3.0)
As you can see, the first pipeline failed to get the environment variable and returned
NULL
, so thatmutate
didn't add a column. The second pipeline added the wrong column as it was based on the previous state of the environment, so it added the sum ofmpg
instead of the one ofcyl
.Separating the two pipelines solves the problem:
Created on 2021-01-30 by the reprex package (v0.3.0)
However, this worked perfectly with
magrittr
v1.5:Created on 2021-01-30 by the reprex package (v0.3.0)
Is there any chance that it is correctable?
Companion SO question: https://stackoverflow.com/questions/65924644/package-environments-are-not-working-as-expected-in-a-pipeline