Skip to content

Instantly share code, notes, and snippets.

@gweissman
Created April 14, 2012 17:04
Show Gist options
  • Save gweissman/2385949 to your computer and use it in GitHub Desktop.
Save gweissman/2385949 to your computer and use it in GitHub Desktop.
Dynamic code generation in R
# come up with some code elements
num1 <- 10
num2 <- 5
operators <- c('+','-')
# create a string of the desired code
mycode1 <- paste(num1,operators[1],num2)
mycode2 <- paste(num1,operators[2],num2)
# use parse and eval to evaluate each string
eval(parse(text=mycode1))
eval(parse(text=mycode2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment