Skip to content

Instantly share code, notes, and snippets.

@wjhopper
Created May 9, 2016 13:32
Show Gist options
  • Save wjhopper/13c68c00a1de9548a868e405973894dc to your computer and use it in GitHub Desktop.
Save wjhopper/13c68c00a1de9548a868e405973894dc to your computer and use it in GitHub Desktop.
Metaprogramming in R
# Create a function programmatically by creating its constituents:
# an argument list, a function body of expressions, and an enclosing environment
args <- alist(x=,y=)
exps <- expression(z <- x^2 + y^2, z <- sqrt(z), return(z))
body <- as.call(c(as.name("{"), exps))
f <- as.function(x = c(args,body), envir = parent.frame())
f(x=1,y=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment