tidyverse / magrittr

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

Add function not behaving correctly when being fed an expression #265

Open victorfeagins opened 1 year ago

victorfeagins commented 1 year ago

I like to use magrittr aliases for arithmetic operations to get the Order of operations correct without playing around with parathesis when working with complicated formulas. Though today I noticed the results are sensitive to how results are feed into the function and it behaves unexpectedly wrong.

library(magrittr)
test <- 1

test_nopipe <- test^2/2 + test^2/2

test_expressions <- test^2/2 %>% 
  add(test^2/2)

step <- test^2/2

test_asvariable <- step %>% 
  add(step)

test^2
#> [1] 1
test_expressions #Wrong
#> [1] 0.4
test_nopipe
#> [1] 1
test_asvariable
#> [1] 1