Skip to content

Instantly share code, notes, and snippets.

@moodymudskipper
Last active November 19, 2020 08:22
Show Gist options
  • Save moodymudskipper/33a6628c105e87bd0f602effeac37184 to your computer and use it in GitHub Desktop.
Save moodymudskipper/33a6628c105e87bd0f602effeac37184 to your computer and use it in GitHub Desktop.
decompose <- function(expr) {
expr <- substitute(expr)
funs <- setdiff(all.names(expr), all.vars(expr))
wrapped <- list()
for (fun in funs) {
fun_env <- environment(get(fun, envir = parent.frame()))
if(is.null(fun_env))
namespace <- "base"
else
namespace <- getNamespaceName(fun_env)
namespaced_fun <- paste0(namespace,"::`", fun, "`")
f <- as.function(c(alist(...=), bquote({
sc <- sys.call()
sc_bkp <- sc
sc[[1]] <- quote(.(str2lang(namespaced_fun)))
res <- eval.parent(sc)
message(deparse(sc_bkp))
print(res)
})))
wrapped[[fun]] <- f
}
eval(expr, envir = wrapped, enclos = parent.frame())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment