如何使用 purrr 循环一个整洁的 eval 函数? [英] How to loop over a tidy eval function using purrr?

查看:36
本文介绍了如何使用 purrr 循环一个整洁的 eval 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集(样本):

train <- data.frame(ps_ind_06_bin = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE),ps_ind_07_bin = c(假,真,真,假,真,真),ps_ind_08_bin = c(真,真,真,假,真,假),ps_ind_09_log = c(1, 3, 4, 2, 3, 2))

我有以下函数显示 group_by() 操作的 ggplot:

get_charts1 <- function(mygroup){quo_var <- enquo(mygroup)火车%>%group_by(!!quo_var)%>%计数()%>%取消分组()%>%ggplot(aes_q(x = quo_var, y = 引用(n), 填充 = quo_var)) +geom_col() +主题(legend.position =无")}

当我手动输入列名时它工作正常,例如:

get_charts1(ps_ind_07_bin)

但是,我想在几个列上使用该函数,我把它放在一个向量上:

binarias <- train %>%选择(ends_with(bin"))%>%列名()

使用地图并提出一些建议,我尝试使用:

listaplots <- map(quo(!!! syms(binarias)), get_charts1)

但这给了我以下错误:

<块引用>

错误:无法在顶层拼接"

有谁知道我需要做什么才能让它发挥作用?

解决方案

我将首先创建一个reprex(您非常接近,但是忘记加载所需的包),并将样式重新设置为一致的格式使用 styler:

图书馆(tidyverse)图书馆(rlang)训练 <- data.frame(ps_ind_06_bin = c(假,假,假,真,真,假),ps_ind_07_bin = c(假,真,真,假,真,真),ps_ind_08_bin = c(真,真,真,假,真,假),ps_ind_09_log = c(1, 3, 4, 2, 3, 2))get_charts <- 函数(我的组){quo_var <- enquo(mygroup)火车%>%group_by(!! quo_var) %>%计数()%>%取消分组()%>%ggplot(aes_q(x = quo_var, y = 引用(n), 填充 = quo_var)) +geom_col() +主题(legend.position =无")}

您想像这样自动生成代码:

get_charts(ps_ind_06_bin)获取图表(ps_ind_07_bin)获取图表(ps_ind_08_bin)

这将需要一个 for 循环或一个 apply/map 函数.一个 map()在这里工作得很好,因为我们想返回 ggplot2 对象,并做使用 for 循环需要更多的基础设施.它的一旦你记得你需要在这里使用符号,而不是原始字符串

vars <- train %>% select(ends_with("bin")) %>% colnames()变量%>%符号()%>%地图(函数(var)get_charts(!!var))## [[1]]

#### [[2]]

#### [[3]]

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

这篇关于如何使用 purrr 循环一个整洁的 eval 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆