I have the following data set (sample):
train <- data.frame(ps_ind_06_bin = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE),
ps_ind_07_bin = c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE),
ps_ind_08_bin = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE),
ps_ind_09_log = c(1, 3, 4, 2, 3, 2))
I have the following function that shows a ggplot for a group_by()
operation:
get_charts1 <- function(mygroup){
quo_var <- enquo(mygroup)
train %>%
group_by(!!quo_var) %>%
count() %>%
ungroup() %>%
ggplot(aes_q(x = quo_var, y = quote(n), fill = quo_var)) +
geom_col() +
theme(legend.position = "none")
}
It works fine when I manually imput a column name, for example:
get_charts1(ps_ind_07_bin)
However, I want to use the function on several columns, which I put on a vector:
binarias <- train %>%
select(ends_with("bin")) %>%
colnames()
Using map and taking some suggestions, I tried to use:
listaplots <- map(quo(!!! syms(binarias)), get_charts1)
But this gives me the following error:
"Error: Can't splice at top-level"
Does anyone know what I need to do to get this to work?
I’m going to start by creating a reprex (you were very close, but forgot to load the needed packages), and re-style to a consistent format using styler:
library(tidyverse)
library(rlang)
train <- data.frame(
ps_ind_06_bin = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE),
ps_ind_07_bin = c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE),
ps_ind_08_bin = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE),
ps_ind_09_log = c(1, 3, 4, 2, 3, 2)
)
get_charts <- function(mygroup) {
quo_var <- enquo(mygroup)
train %>%
group_by(!! quo_var) %>%
count() %>%
ungroup() %>%
ggplot(aes_q(x = quo_var, y = quote(n), fill = quo_var)) +
geom_col() +
theme(legend.position = "none")
}
You want to automate the generation of code like this:
get_charts(ps_ind_06_bin)
get_charts(ps_ind_07_bin)
get_charts(ps_ind_08_bin)
That will require either a for loop or an apply/map function. A map()
works well here since we want to return the ggplot2 objects, and doing
that with a for loop requires some more infrastructure. It’s
straightforward once you remember that you need to use symbols here, not
raw strings
vars <- train %>% select(ends_with("bin")) %>% colnames()
vars %>%
syms() %>%
map(function(var) get_charts(!!var))
## [[1]]
##
## [[2]]
##
## [[3]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With