Как правильно использовать глаголы dplyr внутри определения функции в r?

Я хочу использовать filter и summarise из dplyr внутри своей функции. Без функции это работает следующим образом:

library(dplyr)
> Orange %>% 
+     filter(Tree==1) %>% 
+     summarise(age_max = max(age))
  age_max
1    1582  

Я хочу сделать то же самое внутри функции, но следующее не удается:

## Function definition:

df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    filter(plant==1) %>% 
    summarise(age_max = max(Age))

  return(dfo)
}

## Use:
> df.maker(Orange, Tree, age)

 Rerun with Debug
 Error in as.lazy_dots(list(...)) : object 'Tree' not found

Я знаю, что подобные вопросы уже задавались. Я также просмотрел некоторые соответствующие ссылки, такие как page1 и страница2. Но я не могу полностью понять концепции NSE и SE. Я пробовал следующее:

df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    filter_(plant==1) %>% 
    summarise_(age_max = ~max(Age))

  return(dfo)
} 

Но получить ту же ошибку. Пожалуйста, помогите мне понять, что происходит. И как мне правильно создать свою функцию? Спасибо!

РЕДАКТИРОВАТЬ:
Я также пробовал следующее:

df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    #filter_(plant==1) %>% 
    summarise_(age_max = lazyeval::interp(~max(x),
                                          x = as.name(Age)))

  return(dfo)
}  

> df.maker(Orange, Tree, age)
 Error in as.name(Age) : object 'age' not found 

person umair durrani    schedule 23.01.2017    source источник
comment
это отвечает на ваш вопрос?   -  person Axeman    schedule 23.01.2017
comment
@Axeman, я попробовал это, как показано в моем редактировании. Но это все еще не работает. Я думаю, что это как-то связано с окружающей средой.   -  person umair durrani    schedule 23.01.2017


Ответы (1)


Либо укажите символьные аргументы, либо используйте as.name:

df.maker1 <- function(d, plant, Age){
  require(dplyr)
  dfo <- d %>% 
    filter_(lazyeval::interp(~x == 1, x = as.name(plant))) %>% 
    summarise_(age_max = lazyeval::interp(~max(x), x = as.name(Age)))
  return(dfo)
}  
df.maker1(Orange, 'Tree', 'age')
  age_max
1    1582

Или зафиксируйте аргументы с помощью substitute:

df.maker2 <- function(d, plant, Age){
  require(dplyr)
  plant <- substitute(plant)
  Age <- substitute(Age)

  dfo <- d %>% 
    filter_(lazyeval::interp(~x == 1, x = plant)) %>% 
    summarise_(age_max = lazyeval::interp(~max(x), x = Age))
  return(dfo)
}  
df.maker2(Orange, Tree, age)
  age_max
1    1582
person Axeman    schedule 23.01.2017
comment
Большое спасибо! Я не знал о substitute(). - person umair durrani; 23.01.2017