StATS: A simple trick in R (October 11, 2005)

There may be times when you have a string in R that represents a specific R command. How would you run this command. This was answered recently on the Bioconductor mailing list. You use the eval() and parse() functions. Here's an example:

R.function <- "mas"
R.args <- "data.raw"
R.command <- paste(R.function,"(",R.args,")",sep="")

[1] "mas(data.raw)"


The parse() function turns the string into an unevaluated expression and the eval() function evaluates this expression.

The function can also work.,list(R.args))

This sort of thing is useful if you need to loop through a sequence of commands, a sequence of of arguments, or a sequence of models. I'll try to show an example where you could not do this through a simpler approach when I have time.

This page was written by Steve Simon while working at Children's Mercy Hospital. Although I do not hold the copyright for this material, I am reproducing it here as a service, as it is no longer available on the Children's Mercy Hospital website. Need more information? I have a page with general help resources. You can also browse for pages similar to this one at Category: R software.