Stats
Stats >> WebLog
>> A simple trick in R (October 11, 2005).
Category: R software
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.
Stats >> WebLog
>> A simple trick in R (October 11, 2005)
This page was last modified on
08/21/07.
You are welcome to link to this page or other pages on this web site.
Individual educational uses are also okay. Please contact me for permission to
use these pages in any other way. For more details, please consult my
copyright notice.