Hi all,
I ran into an issue which do not understand. I can without issue pass a variable as string to a function and with rlang
pass the variable to dplyr
to execute a filter()
as shown below.
library(tidyverse)
## Injecting in a function context [THIS WORKS]
foo <- function(.x) {
filter(mtcars, .data[[.x]] > 300)
}
foo("hp")
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8
foo("disp")
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
#> Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
#> Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
#> Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
#> Dodge Challenger 15.5 8 318 150 2.76 3.520 16.87 0 0 3 2
#> AMC Javelin 15.2 8 304 150 3.15 3.435 17.30 0 0 3 2
#> Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
#> Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
#> Ford Pantera L 15.8 8 351 264 4.22 3.170 14.50 0 1 5 4
#> Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
However this code breaks when put into a purrr::map() context and I do not understand why... Please note that this has been simplified from a more complex example hence the need for group_by()
/map()
.
library(tidyverse)
## Injecting in a purrr:map context [ERROR]
df <- tibble(var = c("hp", "disp")) %>%
group_by(var) %>%
mutate(out = map(
.x = var,
.f = ~filter(mtcars, .data[[.x]] > 300) # If this code is replaced with message("Filtering data based on ", .x) the variable name is properly passed to .f
)
)
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?
All suggestions are welcome.
Created on 2022-08-03 by the reprex package (v2.0.1)