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
> rm(list=ls()) > a <- 1 > ifelse(exists("b"), b, a)[1] 1> b <- 2 > ifelse(exists("b"), b, a)[1] 2>Gabor On Fri, Jan 27, 2006 at 04:38:39AM -0800, Mikkel Grum 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 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-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
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
On 27 Jan 2006, 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??Along with exists(), you might find mget() useful since it allows you to specify an ifnotfound value. -- + seth