I would like to pass quoted variables in the group
argument of geom_col_wrap
to the split_group
function.
# I deleted the rest of the function for readability
geom_col_wrap = function(data, mapping, group, ...) {
data |>
split_group(group)
}
# This function was based on the `tidytable` package
split_group = function(data, ...) {
by_quote = as.list(substitute(...()))
by = sapply(by_quote, deparse)
split = vctrs::vec_split(data, data[c(by)])
out = split[["val"]]
names = do.call(paste, c(split[["key"]], sep = "_"))
names(out) = names
return(out)
}
split_group
use substitute
to quote variables, here is the problem. How can I make split_group
recognize quote variables from group
argument? I know it is easy to solve using rlang
, but I need a R base solution.
split_group(mtcars, vs, am)
$`0_1`
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
...
$`1_1`
mpg cyl disp hp drat wt qsec vs am gear carb
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
...
$`1_0`
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
...
$`0_0`
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
...
geom_col_wrap(
mtcars,
mapping = ggplot2::aes(x = cyl, y = hp, color = am),
group = c(vs, am)
)
Error in `[.data.frame`(data, c(by)) : undefined columns selected
This error comes from as.list(substitute(...()))
. It does not unquoted the group
argument. Why?
Note: I cannot use dots arg to solve the problem.