You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
across() uses .fns = as the argument to supply an anonymous function.
This is different than the scoped mutate functions, which used .funs =, with an extra "u".
This causes some problems when rewriting code from scoped syntax mutate_at(.funs = ~) to the new mutate(across(.fns = ~)), because supplying the wrong argument in across() (namely, .funs instead of .fns) fails silently, as shown below.
library(tidyverse)
mtcars<- as_tibble(mtcars) %>% select(mpg)
# Using the named argument ".fns" applies the function, as expectedmtcars %>% mutate(across(mpg, .fns=~.*1000))
#> # A tibble: 32 × 1#> mpg#> <dbl>#> 1 21000#> 2 21000#> 3 22800#> 4 21400#> 5 18700#> 6 18100#> 7 14300#> 8 24400#> 9 22800#> 10 19200#> # … with 22 more rows# Using the named argument ".funs" fails, silentlymtcars %>% mutate(across(mpg, .funs=~.*1000))
#> # A tibble: 32 × 1#> mpg#> <dbl>#> 1 21 #> 2 21 #> 3 22.8#> 4 21.4#> 5 18.7#> 6 18.1#> 7 14.3#> 8 24.4#> 9 22.8#> 10 19.2#> # … with 22 more rows
As the argument is ignored, the result is no different than failing to supply any function at all.
(Supplying the function without naming the argument works as expected.)
Perhaps across should throw a warning when it does not detect an anonymous function being supplied?
The text was updated successfully, but these errors were encountered:
library(tidyverse)
x<-mtcars %>% mutate(across(mpg, .funs=~.*1000))
#> Warning: There was 1 warning in `mutate()`.#> ℹ In argument: `across(mpg, .funs = ~. * 1000)`.#> Caused by warning:#> ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.#> Supply arguments directly to `.fns` through an anonymous function instead.#> #> # Previously#> across(a:b, mean, na.rm = TRUE)#> #> # Now#> across(a:b, \(x) mean(x, na.rm = TRUE))
Thank you! Indeed, the warning is quite indirect (especially for such a subtle one-letter difference, for a spelling that is accepted elsewhere), but at least it prompts further inspection.
What would be a use-case for not supplying any .fns in across()?
across() uses
.fns =
as the argument to supply an anonymous function.This is different than the scoped mutate functions, which used
.funs =
, with an extra "u".This causes some problems when rewriting code from scoped syntax
mutate_at(.funs = ~)
to the newmutate(across(.fns = ~))
, because supplying the wrong argument inacross()
(namely,.funs
instead of.fns
) fails silently, as shown below.Created on 2023-01-12 by the reprex package (v2.0.1)
As the argument is ignored, the result is no different than failing to supply any function at all.
(Supplying the function without naming the argument works as expected.)
Perhaps
across
should throw a warning when it does not detect an anonymous function being supplied?The text was updated successfully, but these errors were encountered: