0

Dplyr 的mutate函数可以评估“链式”表达式,例如

library(dplyr)

data.frame(a = 1) %>%
   mutate(b = a + 1, c = b * 2)
##   a b c
## 1 1 2 4 

如何实施?快速浏览 dplyr 的源代码会发现候选代码的基本结构:

library(lazyeval)
library(rlang)

compat_as_lazy <- function(quo) {
  structure(class = "lazy", list(
    expr = f_rhs(quo),
    env = f_env(quo)
  ))
}

compat_as_lazy_dots <- function(...) {
  structure(class = "lazy_dots", lapply(quos(...), compat_as_lazy))
}

my_mutate <- function(.data, ...) {
  lazy_eval(compat_as_lazy_dots(...), data = .data)
}

data.frame(a = 1) %>%
  my_mutate(b = a + 1, c = b * 2)
## Error in eval(x$expr, data, x$env) : object 'b' not found

...但是这种“幼稚”的实现不起作用,并且背后的 C++ 代码mutate_impl非常复杂。我知道它不起作用,因为lazy_evalon "lazy_dots"uses lapply,即每个表达式都是相互独立地评估的,而我宁愿需要链式评估并将结果返回到共享环境。如何让它发挥作用?

4

2 回答 2

2

我不完全确定这是您想要的,但这里有 3 个基础 R 中的变异克隆,可与您的示例一起使用:

mutate_transform <- function(df,...){
  lhs <- names(match.call())[-1:-2]
  rhs <- as.character(substitute(list(...)))[-1]
  args = paste(lhs,"=",rhs)
  for(arg in args){
    df <- eval(parse(text=paste("transform(df,",arg,")")))
  }
df
}

mutate_within <- function(df,...){
  lhs <- names(match.call())[-1:-2]
  rhs <- as.character(substitute(list(...)))[-1]
  args = paste(lhs,"=",rhs)
  df <- eval(parse(text=paste("within(df,{",paste(args,collapse=";"),"})")))
  df
}

mutate_attach <- function(df,...){
  lhs <- names(match.call())[-1:-2]
  rhs <- as.character(substitute(list(...)))[-1]
  new_env <- new.env()
  with(data = new_env,attach(df,warn.conflicts = FALSE))
  for(i in 1:length(lhs)){
    assign(lhs[i],eval(parse(text=rhs[i]),envir=new_env),envir=new_env)
  }
  add_vars <- setdiff(lhs,names(df))
  with(data = new_env,detach(df))
  for(var in add_vars){
    df[[var]] <- new_env[[var]]
  }
  df
}  

data.frame(a = 1) %>%  mutate_transform(b = a + 1, c = b * 2)
#   a b c
# 1 1 2 4
data.frame(a = 1) %>%  mutate_within(b = a + 1, c = b * 2)
#   a c b   <--- order is different here 
# 1 1 4 2
data.frame(a = 1) %>%  mutate_attach(b = a + 1, c = b * 2)
#   a b c
# 1 1 2 4
于 2017-07-14T23:46:46.597 回答
0

在阅读了 Moody_Mudskipper的回答后,我提出了自己的解决方案,该解决方案重新实现了lazyeval::lazy_eval“记住”过去评估的表达式列表的函数:

my_eval <- function(expr, .data = NULL) {
  idx <- structure(seq_along(expr),
                   names = names(expr))
  lapply(idx, function(i) {
    evl <- lazy_eval(expr[[i]], data = .data)
    .data[names(expr)[i]] <<- evl
    evl
  })
}

接下来,需要用 in 替换以lazy_eval使所有内容都能按预期工作。my_mutatemy_eval

于 2017-07-17T19:55:08.023 回答