From ggrothendieck at gmail.com  Thu Oct 26 05:10:46 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Wed, 25 Oct 2006 23:10:46 -0400
Subject: [Rd] how to determine if a function's result is invisible
In-Reply-To: <1161825632.4556.14.camel@localhost.localdomain>
References: <971536df0610251714u451cf80fy80bf38cb2f130734@mail.gmail.com>
	<1161825632.4556.14.camel@localhost.localdomain>
Message-ID: <971536df0610252010j32f33342m78fee7588d1439fd@mail.gmail.com>

On 10/25/06, Marc Schwartz <MSchwartz at mn.rr.com> wrote:
> On Wed, 2006-10-25 at 20:14 -0400, Gabor Grothendieck wrote:
> > Suppose we have a function such as the following
> >
> > F <- function(f, x) f(x)+1
> >
> > which runs function f and then transforms it.  I would like the
> > corresponding function which works the same except that
> > unlike F returns an invisible result if and only if f does.
> >
> > Is there some way of determining whether f returns
> > an invisible result or not?
> >
> > Thus we want this:
> >
> > f <- function(x) x
> > g <- function(x) invisible(x)
> >
> > > F(f, 1)
> > 2
> > >
> >
> > > F(g, 1)
> > >
>
> Gabor,
>
> There may be a better way of doing this and/or this will spark some
> thoughts.
>
> Let's create two simple functions:
>
>  f.inv <- function(x) {invisible(x)}
>
>  f <- function(x) {x}
>
> So we now have:
>
> > f.inv(1)
> > f(1)
> [1] 1
>
>
> > any(grep("invisible", (deparse(f))))
> [1] FALSE
>
> > any(grep("invisible", (deparse(f.inv))))
> [1] TRUE
>
>
> This is not extensively tested of course, but another function that
> comes to mind that does return a result 'invisibly' is:
>
> > any(grep("invisible", (deparse(barplot.default))))
> [1] TRUE
>
> So there seems to be some foundation for working, as long as the target
> function can be deparsed, which may limit things with respect to
> C/FORTRAN based functions.
>
> HTH,
>
> Marc Schwartz
>
>
>

Clever idea.  As others have mentioned there are some limitations
plus generics would not work since the code is in the methods
but its an interesting suggestion that would work part of the time.




[Rd] how to determine if a function's result is invisible
Gabor Grothendieck