[R] (not so real) function (more like #include)

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 5 18:54:54 CEST 2006


I thought of a few more possibilities.

If options 1, 2 and 3 correspond to longfun, longfun2 and longfun3
then:

4. We could arrange it so that the variables are stored outside of
longfun and then longfun, f1 and f2 reference them symmetrically.

a <- b <- d <- 1
longfun4 <- function() {
   out <- f1()
   # since a is not in longfun4 use <<- to set it
   a <<- a + 1
   f2() + out
}
f1 <- f2 <- function() a + b + d
longfun4()

Given the problem as stated I don't think this approach is really
the best but if the problem gets extended to one where there is
not just one longfun but several longfuns then it will not be feasible
to store all the variables in longfun since the other longfuns won't
be able to access them and this approach becomes desirable.

5. We could also use the proto package for this purpose placing
a, b, d, longfun, f1 and f2 into an object p.  If we wanted to be
able to override f1, f2, a, b and d in delegated subobjects and
be able to refer to f1 and f2 outside of longfun, i.e. outside of
the context of object p, then we would have
to refer to them as .$f1, .$f2, .$a, .$b and .$d and also pass
the target object as arg 1 of f1 and f2 but we have not bothered
with that here since it seems not to be a requirement:

library(proto)
p <- proto(a = 1, b = 1, d = 1)
p$longfun <- function(.) {
   out <- f1()
   # since a is not in longfun4 use <<- to set it
   a <<- a + 1
   f2() + out
}
p$f1 <- p$f2 <- function() a + b + d
p$longfun() # 7

This is probably overkill here but it might be that if one thinks about
the design some more that there are some opportunities to define
subobjects of p in which case the ability to do inheritance would come
into play.

6. I think the R.oo package would work too although I am not sure
its as suitable here since it would require the definition of classes
which you don't really need.  proto lets you define objects directly
even without classes.


On 10/5/06, Meinhard Ploner <meinhardploner at gmx.net> wrote:
> Thanks a lot! longfun2 & longfun3 work perfect for my "very
> untypical" problem. As I have many local variables, usual functions
> with parameters are very uncomfortable, but the code you gave me is
> great!
> Meinhard
>
>
> On Oct 4, 2006, at 5:25 PM, Gabor Grothendieck wrote:
>
> > longfun could just pass a, b and d to each of the individual
> > functions and each of the individual functions could pass
> > out back as a return value.
> >
> > f1 <- f2 <- function(a, b, d) a+b+d
> >
> > longfun1 <- function() {
> >   a <- b <- d <- 1
> >   out <- f1(a, b, d)
> >   out <- f2(a, b, d) + out
> >   out
> > }
> >
> > longfun1() # 6
> >
> > If the problem is that a, b and d actually represent 100 variables,
> > say,
> > and its a nuisance to pass them all explicitly you could do this:
> >
> > f1 <- f2 <- function() with(parent.frame(), a + b + d)
> >
> > longfun2 <- function() {
> >   a <- b <- d <- 1
> >   out <- f1()
> >   out <- f2() + out
> >   out
> > }
> >
> > longfun2() # 6
> >
> >
> > or you could do it this way if you would prefer to have longfun
> > control the scoping rather than having it done within each
> > individual function:
> >
> >
> > f1 <- f2 <- function() a+b+d
> >
> > longfun3 <- function() {
> >       a <- b <- d <- 1
> >       environment(f1) <- environment(f2) <- environment()
> >       out <- f1()
> >       f2() + out
> > }
> >
> > longfun3() # 6
> >
> >
> > On 10/4/06, Meinhard Ploner <meinhardploner at gmx.net> wrote:
> >> Hello R users,
> >>
> >> I have a very long function with parts of that looking like a list of
> >> jobs. The first part of the function defines a lot of local
> >> variables, which are used by the jobs. Now I extracted the "job" part
> >> und putted them into an external file, used by "source(, local=T)",
> >> which is more comfortable to me. Apart from that it gives me the
> >> opportunity that more users can work on the project. Is it possible
> >> to define a function for that code and passing to it the environment
> >> of f() without save(list=ls()) and load() .... ?
> >>
> >> Thanks in advance
> >> Meinhard Ploner
> >>
> >> longfun <- f() {
> >>
> >>        ## lot of local variables:
> >>        a <- ...
> >>        b <- ...
> >>        d <- ...
> >>        ...
> >>
> >>        out <- ...
> >>
> >>        source("job1.txt", local=TRUE)   #changes out, uses a, b,
> >> d, ...
> >>
> >>        source("job2.txt", local=TRUE)  # changes out, uses a, b,
> >> d, ...
> >>        ...
> >> }
> >>
> >>
> >>
> >> version
> >>                _
> >> platform       i386-apple-darwin8.6.1
> >> arch           i386
> >> os             darwin8.6.1
> >> system         i386, darwin8.6.1
> >> status
> >> major          2
> >> minor          3.1
> >> year           2006
> >> month          06
> >> day            01
> >> svn rev        38247
> >> language       R
> >> version.string Version 2.3.1 (2006-06-01)
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-
> >> guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
>
>



More information about the R-help mailing list