tidyverse / magrittr

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

Add warning when piped object overwrites keyword arguments when not used as first argument #251

Closed HLasse closed 2 years ago

HLasse commented 2 years ago

When piping into a function that does not take the object as the first argument, %>% will force the object into the next available argument. This can be problematic in cases where only some attributes from the object are required. If this is indeed intended behaviour, perhaps it would be helpful to throw a warning message if the piped object replaces e.g. a default argument?

library(maggritr)
some_fun <- function(arg1,
                     arg2,
                     default_arg1 = "foo",
                     default_arg2 = "bar"){
  print("dummy function")
                     }

df <- tibble(var1 <- c(1,2,3,4),
             var2 <- c(5,6,7,8))

debug(some_fun)
df %>% 
  some_fun(arg1=.$var1, arg2=.$var2, default_arg2 = "baz")

In the above case, default_arg1 is replaced by .(the piped object)

lionel- commented 2 years ago

This is working as expected, so we won't add a warning for this.