How to implement mutate-like chain evaluation?

165 views Asked by At

Dplyr's mutate function can evaluate "chained" expressions, e.g.

library(dplyr)

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

How can this be implemented? Quick glance at dplyr's source code reveals the basic structure of the candidate code:

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

...but such "naive" implementation does not work and the C++ code behind mutate_impl is pretty complicated. I understand that it doesn't work because lazy_eval on "lazy_dots" uses lapply, i.e. each of the expressions is evaluated independently of each other, while I would rather need chained evaluation with returning the result back to the shared environment. How to make it work?

2

There are 2 answers

0
moodymudskipper On

I'm not completely sure it's what you want but here are 3 mutate clones in base R that work with your example:

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
0
Tim On

After reading Moody_Mudskipper's answer I came out with my own solution that re-implements the lazyeval::lazy_eval function for a list of expressions that "remembers" the past evaluations:

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
  })
}

Next, lazy_eval in my_mutate needs to be substituted with my_eval for everything to work as expected.