[R] eval(parse(text vs. get when accessing a function

Brian Ripley ripley at stats.ox.ac.uk
Sat Jan 6 16:30:24 CET 2007

On Sat, 6 Jan 2007, Ramon Diaz-Uriarte wrote:

> Dear Greg,
> On 1/5/07, Greg Snow <Greg.Snow at intermountainmail.org> wrote:
>> Ramon,
>> I prefer to use the list method for this type of thing, here are a couple of reasons why (maybe you are more organized than me and would never do some of the stupid things that I have, so these don't apply to you, but you can see that the general suggestion applys to some of the rest of us).
> Those suggestions do apply to me of course (no claim to being
> organized nor beyond idiocy here). And actually the suggestions on
> this thread are being very useful. I think, though, that I was not
> very clear on the context and my examples were too dumbed down. So
> I'll try to give more detail (nothing here is secret, I am just trying
> not to bore people).
> The code is part of a web-based application, so there is no
> interactive user. The R code is passed the arguments (and optional
> user functions) from the CGI.
> There is one "core" function (call it cvFunct) that, among other
> things, does cross-validation. So this is one way to do things:
> cvFunct <- function(whatever, genefiltertype, whateverelse) {
>      internalGeneSelect <- eval(parse(text = paste("geneSelect",
>                                             genefiltertype, sep = ".")))
>      ## do things calling internalGeneSelect,
> }

That looks like a more complicated alternative to

    get(paste("geneSelect", genefiltertype, sep = "."))

I would worry about scope in both cases: I think you most likely want 
eval.parent in yours, and to pick an environment for use in get() (but 
the view you have shown is still too narrow for us to know).

> and now define all possible functions as
> geneSelect.Fratio <- function(x, y, z) {##something}
> geneSelect.Wilcoxon <- function(x, y, z) {## something else}
> If I want more geneSelect functions, adding them is simple. And I can
> even allow the user to pass her/his own functions, with the only
> restriction that it takes three args, x, y, z, and that the function
> is to be called: "geneSelect." and a user choosen string. (Yes, I need
> to make sure no calls to "system", etc, are in the user code, etc,
> etc, but that is another issue).


Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list