it seems there was a change in dplyr 0.8.1 in the way vars
works for you. From the two examples, we see that vars(BM1)
will be put in .x
but as a its value and not as a name.
That means your fun1
does not need enquo
anymore. The issue is with fun1
and how summarise_at
handles it.
I don't know where the change comes from exactly and if it is intended as a breaking change or if it is a bug.
with 0.8.0.1
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
packageVersion("dplyr")
#> [1] '0.8.0.1'
fun1 <- function(df, x){
x <- enquo(x)
tmp <- mean(df %>% pull(!!x), na.rm = TRUE)
return(tmp)
}
df <- tibble(
BM1 = rnorm(40, mean = 3, sd = 1)
)
# this used to work, but it does not anymore
df %>%
summarise_at(
vars(BM1),
list(~fun1(df, .))
)
#> # A tibble: 1 x 1
#> BM1
#> <dbl>
#> 1 2.95
Created on 2019-05-24 by the reprex package (v0.3.0.9000)
with last version 0.8.1
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
packageVersion("dplyr")
#> [1] '0.8.1'
fun1 <- function(df, x){
x <- enquo(x)
tmp <- mean(df %>% pull(!!x), na.rm = TRUE)
return(tmp)
}
df <- tibble(
BM1 = rnorm(40, mean = 3, sd = 1)
)
# this used to work, but it does not anymore
df %>%
summarise_at(
vars(BM1),
list(~fun1(df, .))
)
#> `var` must evaluate to a single number or a column name, not a double
#> vector
Created on 2019-05-24 by the reprex package (v0.3.0.9000)
from the error message we understand that something change.
From the example in ?summarise_at
, we see that the current behavior is the expected one
library(dplyr)
starwars %>%
summarise_at(vars(height:mass), mean, na.rm = TRUE)
There is no need for a special fun that knows how to enquo. It just passes the variable from vars
as vector and not column name or symbol. Just provide a .funs
that know how to handle a column vector. I believe it was a bug in 0.8.0.1 and now works correctly in last version.