tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

Inconsistent assign() behavior in simple piping with Magrittr #252

Closed millermc38 closed 2 years ago

millermc38 commented 2 years ago

By simply changing the argument order in the join step, I can get the code below to run. I just installed the most recent version of Tidyverse as of this post (1.3.1), and I'm using R version 4.1.1 (2021-08-10), "Kick Things". If you run the the pipe without join statment, the assignment works fine (odd). When I tested on a different machine, I had an old version of the tidyverse (which I foolishly did not record), and the code would run. Now it does not with the latest version of tidyverse. R version on the other machine is 3.6.3 (2020-02-29).

Update: Sorry to cross post, but as soon as I learned it was a version issue, I moved this questions from Stack Overflow to here. However, someone on Stack Overflow pointed out that if you make the code below without pipes, the result makes perfect sense. However, I think this is not what the user expects. They expect the operations to be processed in the order of the piping. So really, this is not a bug but a feature request.

library(dplyr)

#Doesn't run
if(exists("test")) rm("test")
iris%>%
  assign(x = "test",value = .,envir = .GlobalEnv)%>%
  left_join(x = test,y =. ,by="Species")

#Runs
if(exists("test")) rm("test")
iris%>%
  assign(x = "test",value = .,envir = .GlobalEnv)%>%
  left_join(x = .,y =test ,by="Species")
lionel- commented 2 years ago

This behaviour is consistent with the non-magrittr form and is due to lazy evaluation.

Use %!>% to evaluate eagerly. See documentation and examples in https://magrittr.tidyverse.org/reference/pipe-eager.html for more about this behaviour.