Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop over a tidy eval function using purrr?

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?

like image 982
Ramiro Bentes Avatar asked Dec 18 '17 14:12

Ramiro Bentes


1 Answers

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]]

like image 141
hadley Avatar answered Sep 30 '22 20:09

hadley