Re: [R] substituting an object not found

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 28 Jan 2006 - 01:33:58 EST

This does not answer your question directly but this can relate to calling functions and also to inheritance in object oriented systems. The common thread in 1b and 2 is that if a variable is not found in the current environment R will look into the parent environment so if we give our variables the same name but put them in different environments that have a parent/child relationship then R will use the local variable if its present and the variable of the same name from the parent if not. There is also an example here (1a) that uses function args but that requires one to be explicit.

1a. function args

f <- function(a = b) a
f(1) #1

b <- 2
f() # 2

1b. function free variables

f <- function() { a <- 10; a }
f() # uses a in function f

a <- 20
g <- function() a
g() # uses a from global environment

2. oo

In this example,
the parent is the global environment and the child is object oo.

library(proto)
a <- 1
oo <- proto(f = function(.) .$a)
oo1f() # 1 It has used a in global environment.

oo$a <- 2
oo$f() # #2 It has used a in object oo.

On 1/27/06, Mikkel Grum <mi2kelgrum@yahoo.com> wrote:
> Is there any function in R like
>
> is.not.found(x, y)
>
> meaning if you can't find object x, then use object
> y??
>
>
> Mikkel Grum



R-help@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 Received on Sat Jan 28 01:42:14 2006

This archive was generated by hypermail 2.1.8 : Sat 28 Jan 2006 - 08:46:17 EST