On Jun 5, 2008, at 9:13 PM, Duncan Murdoch wrote:
On 05/06/2008 8:23 PM, Rolf Turner wrote:
I just discovered what seems to me to be a slight funny in respect
of formal argument names. If I define a function
foo <- function(a,b){ ... whatever ...}
then ``inside'' foo() the exists() function will return TRUE
from ``exists("a") whether an object named ``a'' exists or not.
But get("a") will yield an error ``object "a" not found''
in these circumstances.
I presume there is a reason for specifying that an object named
by a formal argument always exists --- but it is mysterious by my
standards. Can anyone explain the reason for this behaviour?
Oops, I didn't explain why this is the way it should be.
Say your "whatever" above makes use of a, but you didn't pass an a
in. Then you'd like an error, or you'd like "missing(a)" to
evaluate to TRUE, or something along those lines. But if a was
completely undefined and nonexistent, R would just go looking for a
global, and make use of that. So it has to be marked as missing.
I am now baffled by this:
> foo <- function(a,b){class(get("a"))}
> foo()
[1] "name"
> foo <- function(a,b){x<-get("a");class(x)}
> foo()
Error in foo() : argument "x" is missing, with no default
And similarly with str:
> foo <- function(a,b){str(get("a"))}
> foo()
symbol
> foo <- function(a,b){x<-get("a");str(x)}
> foo()
Error in str(x) : argument "x" is missing, with no default
Well OK, maybe not baffled exactly, it short of fits with your
description above. I guess I am wondering, how is this technically
achieved?
I suppose then that missing(a) the only call we should expect to work
in the absence of a, even though calls like class(get("a")) and str
(get("a")), is.numeric(get("a")) etc seem to return something
reasonable?
Duncan Murdoch
Haris Skiadas
Department of Mathematics and Computer Science
Hanover College
______________________________________________
R-help@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.