I'm having a bizarre problem in which a tidyeval function I wrote works fine the first time I run it with a particular data frame, but might or might not work on subsequent attempts. I've provided two reprexes below, just to show a couple of different failure modes. Does anyone know what could be causing this and how to fix it?
library(tidyverse)
fnc = function(data, value.vars, group.vars=NULL) {
data %>%
group_by(across({{group.vars}})) %>%
summarise(n=n(), across({{value.vars}},
list(mean=~mean(.x, na.rm=TRUE),
n.miss=~sum(is.na(.x))),
.names="{.fn}_{.col}"))
}
mtcars %>% fnc(mpg)
#> # A tibble: 1 x 3
#> n mean_mpg n.miss_mpg
#> <int> <dbl> <int>
#> 1 32 20.1 0
iris %>% fnc(c(Petal.Width, Sepal.Width), Species)
#> # A tibble: 3 x 6
#> Species n mean_Petal.Width n.miss_Petal.Wi… mean_Sepal.Width
#> * <fct> <int> <dbl> <int> <dbl>
#> 1 setosa 50 0.246 0 3.43
#> 2 versic… 50 1.33 0 2.77
#> 3 virgin… 50 2.03 0 2.97
#> # … with 1 more variable: n.miss_Sepal.Width <int>
diamonds %>% fnc(c(x,y), c(cut, color))
#> `summarise()` has grouped output by 'cut'. You can override using the `.groups` argument.
#> # A tibble: 35 x 7
#> # Groups: cut [5]
#> cut color n mean_x n.miss_x mean_y n.miss_y
#> <ord> <ord> <int> <dbl> <int> <dbl> <int>
#> 1 Fair D 163 6.02 0 5.96 0
#> 2 Fair E 224 5.91 0 5.86 0
#> 3 Fair F 312 5.99 0 5.93 0
#> 4 Fair G 314 6.17 0 6.11 0
#> 5 Fair H 303 6.58 0 6.50 0
#> 6 Fair I 175 6.56 0 6.49 0
#> 7 Fair J 119 6.75 0 6.68 0
#> 8 Good D 662 5.62 0 5.63 0
#> 9 Good E 933 5.62 0 5.63 0
#> 10 Good F 909 5.69 0 5.71 0
#> # … with 25 more rows
iris %>% fnc(c(Petal.Width, Sepal.Width), Species)
#> Error: Can't subset elements that don't exist.
#> x Location 35 doesn't exist.
#> ℹ There are only 3 elements.
diamonds %>% fnc(c(x,y))
#> Error: Problem with `summarise()` input `..2`.
#> x subscript out of bounds
#> ℹ Input `..2` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Created on 2021-02-18 by the reprex package (v1.0.0)
library(tidyverse)
fnc = function(data, value.vars, group.vars=NULL) {
data %>%
group_by(across({{group.vars}})) %>%
summarise(n=n(), across({{value.vars}},
list(mean=~mean(.x, na.rm=TRUE),
n.miss=~sum(is.na(.x))),
.names="{.fn}_{.col}"))
}
diamonds %>% fnc(c(x,y))
#> # A tibble: 1 x 5
#> n mean_x n.miss_x mean_y n.miss_y
#> <int> <dbl> <int> <dbl> <int>
#> 1 53940 5.73 0 5.73 0
mtcars %>% fnc(mpg)
#> # A tibble: 1 x 3
#> n mean_mpg n.miss_mpg
#> <int> <dbl> <int>
#> 1 32 20.1 0
iris %>% fnc(c(Petal.Width, Sepal.Width), Species)
#> # A tibble: 3 x 6
#> Species n mean_Petal.Width n.miss_Petal.Wi… mean_Sepal.Width
#> * <fct> <int> <dbl> <int> <dbl>
#> 1 setosa 50 0.246 0 3.43
#> 2 versic… 50 1.33 0 2.77
#> 3 virgin… 50 2.03 0 2.97
#> # … with 1 more variable: n.miss_Sepal.Width <int>
diamonds %>% fnc(c(x,y), c(cut, color))
#> `summarise()` has grouped output by 'cut'. You can override using the `.groups` argument.
#> # A tibble: 35 x 7
#> # Groups: cut [5]
#> cut color n mean_x n.miss_x mean_y n.miss_y
#> <ord> <ord> <int> <dbl> <int> <dbl> <int>
#> 1 Fair D 163 6.02 0 5.96 0
#> 2 Fair E 224 5.91 0 5.86 0
#> 3 Fair F 312 5.99 0 5.93 0
#> 4 Fair G 314 6.17 0 6.11 0
#> 5 Fair H 303 6.58 0 6.50 0
#> 6 Fair I 175 6.56 0 6.49 0
#> 7 Fair J 119 6.75 0 6.68 0
#> 8 Good D 662 5.62 0 5.63 0
#> 9 Good E 933 5.62 0 5.63 0
#> 10 Good F 909 5.69 0 5.71 0
#> # … with 25 more rows
iris %>% fnc(c(Petal.Width, Sepal.Width), Species)
#> Error: Can't subset elements that don't exist.
#> x Location 35 doesn't exist.
#> ℹ There are only 3 elements.
mtcars %>% fnc(mpg, cyl)
#> Error: Can't subset elements that don't exist.
#> x Location 35 doesn't exist.
#> ℹ There are only 3 elements.
diamonds %>% fnc(c(x,y), color)
#> Error: Can't subset elements that don't exist.
#> x Location 35 doesn't exist.
#> ℹ There are only 7 elements.
Created on 2021-02-18 by the reprex package (v1.0.0)