A simple trick in R (October 11, 2005)

This page is moving to a new website.

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="")
R.command

[1] "mas(data.raw)"

eval(parse(text=method))

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

The do.call() function can also work.

do.call(R.function,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.