[R] substituting an object not found

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 27 15:33:58 CET 2006


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 at 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




More information about the R-help mailing list