Skip to content

Instantly share code, notes, and snippets.

@karthik
Forked from dgrtwo/separate_steps.R
Created September 29, 2016 09:58
Show Gist options
  • Save karthik/ff99fb64cea10ca1f4c5d3fda01110a5 to your computer and use it in GitHub Desktop.
Save karthik/ff99fb64cea10ca1f4c5d3fda01110a5 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