I'm trying to test for the existence of an object within a function,
but despite searching the help files and R-list, I can't figure out
how to do it.
Here is some test code:
#-------------------------
a=1 #now I have a in the global environment
tst <- function(a,b=1) {
# but a is not in the local function environment
print(exists("a", inherits=FALSE)) #This is how I think I should be
able to test if it is in the local function environment
print(a)
}
tst()
#----------------------
#Here's the output
[1] TRUE
Error in print(a) : argument "a" is missing, with no default
It says TRUE, even though I specified inherits=FALSE and "a" is not in
the function environment. I've tried to pass in where and envir
arguments to exists() but I haven't been able to solve the problem.
Thanks in advance.
Eli -
I think you're looking for missing():
> tst = function(a,b=1)
+ if(missing(a))print("You must provide a") else
print(a)> tst()
[1] "You must provide a"> tst(7)
[1] 7
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Thu, 7 Oct 2010, Eli Holmes wrote:
> I'm trying to test for the existence of an object within a function,
> but despite searching the help files and R-list, I can't figure out
> how to do it.
>
> Here is some test code:
>
> #-------------------------
> a=1 #now I have a in the global environment
>
> tst <- function(a,b=1) {
> # but a is not in the local function environment
> print(exists("a", inherits=FALSE)) #This is how I think I should
be
> able to test if it is in the local function environment
> print(a)
> }
> tst()
> #----------------------
>
> #Here's the output
> [1] TRUE
> Error in print(a) : argument "a" is missing, with no default
>
> It says TRUE, even though I specified inherits=FALSE and "a" is
not in
> the function environment. I've tried to pass in where and envir
> arguments to exists() but I haven't been able to solve the problem.
>
> Thanks in advance.
>
> ______________________________________________
> R-help at r-project.org 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.
>
David Winsemius
2010-Oct-07 23:51 UTC
[R] Testing for existence of object within a function
On Oct 7, 2010, at 7:27 PM, Eli Holmes wrote:> I'm trying to test for the existence of an object within a function, > but despite searching the help files and R-list, I can't figure out > how to do it. > > Here is some test code: > > #------------------------- > a=1 #now I have a in the global environment > > tst <- function(a,b=1) { > # but a is not in the local function environment > print(exists("a", inherits=FALSE)) #This is how I think I should be > able to test if it is in the local function environment > print(a) > } > tst() > #---------------------- > > #Here's the output > [1] TRUE > Error in print(a) : argument "a" is missing, with no default > > It says TRUE, even though I specified inherits=FALSE and "a" is not in > the function environment.Yes it is, or at least its name is known, even if it doesn't have a value: a=1 #now I have a in the global environment tst2 <- function(a,b=1) { # but a is not in the local function environment print(ls()) } tst2() [1] "a" "b" tst3 <- function(a,b=1) { # but a is not in the local function environment print(formals("tst3")) } ; tst3() $a $b [1] 1> I've tried to pass in where and envir > arguments to exists() but I haven't been able to solve the problem.Exactly what IS the problem? If you tested for "d" there would be a FALSE return. And does this solve it.... > tst4 <- function(a,b=1,d=missing(a)) { + print(d) + } ; tst4() [1] TRUE > tst4 <- function(a,b=1,d=missing(a)) { + print(d) + } ; tst4(2) [1] FALSE -- David Winsemius, MD West Hartford, CT