Last active
September 29, 2016 09:58
-
-
Save dgrtwo/c2a22442c4799718e69b08a8936bb9e2 to your computer and use it in GitHub Desktop.
separate_steps.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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