使用enquo()在dplyr语法中编写可管理函数,而不返回预期输出

时间:2018-05-31 07:16:55

标签: r dplyr lazy-evaluation tidyr

library(tidyverse)
library(stringr)
library(janitor) 

word_count <- function(data, char_col) {
  char_col <- enquo(char_col)

  data %>% 
    select(!!char_col) %>% 
    mutate(char_col = str_remove_all(!!char_col, '[[:punct:]]')) %>% 
    mutate(char_col = str_split(!!char_col, ' ')) %>% 
    separate(char_col, into = paste0('col', 1:30), fill = 'right') %>% 
    select(-col1) %>% 
    gather(value = word) %>% 
    select(word) %>% 
    remove_empty(c('rows')) %>%
    filter(word != '') %>% 
    mutate(word = str_to_lower(word)) %>% 
    group_by(word) %>% 
    summarize(freq = n()) %>% 
   arrange(desc(freq))
}

iris %>% 
  as.tibble() %>% 
  mutate(Species = str_c(Species, ' species')) %>% 
  word_count(Species)

此代码在函数外部按预期工作,但是当我在函数内部使用它时,它将返回每个单词的频率以及每个单词的非分割频率。串。

我认为这是一个问题,我如何放置&#39; !!&#39;运营商,但我无法通过试用和错误放置来解决这个问题。它也可能是一个lazyeval问题,我不确定如何解决。

我希望函数的输出与下面代码的输出相匹配。

iris %>% 
  as.tibble() %>% 
  mutate(Species = str_c(Species, ' species')) %>% 
  select(Species) %>% 
  mutate(Species = str_remove_all(Species, '[[:punct:]]')) %>% 
  mutate(Species = str_split(Species, ' ')) %>% 
  separate(Species, into = paste0('col', 1:30), fill = 'right') %>% 
  select(-col1) %>% 
  gather(value = word) %>% 
  select(word) %>% 
  remove_empty(c('rows')) %>%
  filter(word != '') %>% 
  mutate(word = str_to_lower(word)) %>% 
  group_by(word) %>% 
  summarize(freq = n()) %>% 
  arrange(desc(freq)) 

1 个答案:

答案 0 :(得分:2)

我们无法将char_col对象作为列名分配给mutate。需要对其进行评估。 char_col是一个quosure对象,可以转换为characterquo_name(char_col))或symbol,当评估时(!!)将分配( :=)正确的列名称

 word_count <- function(data, char_col) {
  char_col <- enquo(char_col)
  char_colC <- quo_name(char_col)

  data %>% 
    select(!!char_col) %>% 
    mutate(!!char_colC := str_remove_all(!!char_col, '[[:punct:]]')) %>% 
    mutate(!!char_colC := str_split(!!char_col, ' ')) %>% 
    separate(char_colC, into = paste0('col', 1:30), fill = 'right') %>%
    select(-col1) %>% 
    gather(value = word) %>% 
    select(word) %>% 
    remove_empty(c('rows')) %>%
    filter(word != '') %>% 
    mutate(word = str_to_lower(word)) %>% 
    group_by(word) %>% 
    summarize(freq = n()) %>% 
    arrange(desc(freq))
}

out2 <- iris %>%
           as.tibble() %>%
           mutate(Species =str_c(Species, ' species')) %>%
           word_count(Species)

- 在不使用函数

的情况下检查输出
out1 <- iris %>% 
          as.tibble() %>% 
          mutate(Species = str_c(Species, ' species')) %>% 
          select(Species) %>% 
          mutate(Species = str_remove_all(Species, '[[:punct:]]')) %>% 
          mutate(Species = str_split(Species, ' ')) %>% 
          separate(Species, into = paste0('col', 1:30), fill = 'right') %>% 
          select(-col1) %>% 
          gather(value = word) %>% 
          select(word) %>% 
          remove_empty(c('rows')) %>%
          filter(word != '') %>% 
          mutate(word = str_to_lower(word)) %>% 
          group_by(word) %>% 
          summarize(freq = n()) %>% 
          arrange(desc(freq)) 


identical(out1, out2)
#[1] TRUE