Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Last active September 29, 2016 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dgrtwo/c2a22442c4799718e69b08a8936bb9e2 to your computer and use it in GitHub Desktop.
Save dgrtwo/c2a22442c4799718e69b08a8936bb9e2 to your computer and use it in GitHub Desktop.
separate_steps.R
#' Convert a dplyr expression to a list of step objects
separate_steps <- function(expr, iscall=FALSE) {
if (iscall) {
call <- expr
} else {
call <- match.call()[["expr"]]
}
len <- length(call)
if (len == 1) {
# base case: original data
ret <- list(data = call, value = eval(call))
class(ret) <- "data"
return(list(ret))
} else if (len == 3) {
if (deparse(call[[1]]) != "%>%") {
stop(paste("Can process only expressions made up of %>% operations,",
"not", deparse(call[[1]])))
}
ret <- list(call = call[[3]], step = call[[3]][[1]],
argexpr = as.list(call[[3]][-1]), value = eval(call))
ret$argument <- paste(lapply(ret$argexpr, deparse), collapse = ", ")
class(ret) <- "step"
before <- separate_steps(call[[2]], TRUE)
return(c(before, list(ret)))
} else {
stop(paste("Cannot parse call of length", len))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment