Skip to content

Instantly share code, notes, and snippets.

@ddbs
Forked from wjhopper/metaprogramming.R
Created April 17, 2018 09:47
Show Gist options
  • Save ddbs/5f64b5c56adb1d532813ffa6db8882c8 to your computer and use it in GitHub Desktop.
Save ddbs/5f64b5c56adb1d532813ffa6db8882c8 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