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?
This is just idle curiosity --- or my hunger for knowledge, whichever
way you want to look at it :-) --- it doesn't really matter.
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________
R-h...@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.
I don't see that. I see this:
> foo <- function(a, b) { get("a") }
> get("a")
Error in get("a") : variable "a" was not found
> foo()
I'm guessing that after you got a, you tried to do something with it,
but I can't guess what: I get a different error in this code.
> foo <- function(a, b) { x <- get("a"); x }
> foo()
Error in foo() : argument "x" is missing, with no default
The thing is, formals are always defined in the local scope when you
call a function. But if you didn't assign a value or a default to them,
they are defined as a special missing value. As the second example
shows, you can move that missing around, but not do anything with it.
Duncan Murdoch
test <- function(a) {
exists("a", mode = "symbol")
}
test()
and
test2 <- function(a) {
exists("a", mode = "numeric") #say
}
test2()
and then note that the default mode argument to exists is "any".
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.
Duncan Murdoch
> Note the difference between
>
> test <- function(a) {
> exists("a", mode = "symbol")
> }
> test()
>
> and
>
> test2 <- function(a) {
> exists("a", mode = "numeric") #say
> }
> test2()
>
> and then note that the default mode argument to exists is "any".
Basically, I think, you're saying that a formal argument exists ``as
a symbol''
(even if the object named doesn't exist) and that's why exists("a")
returns TRUE
in these circumstances.
I'm still a bit puzzled, but:
Although test() gives TRUE and test2() gives FALSE,
both test(a) and test2(a) throw an error:
Error in exists("a", mode = "symbol") : object "a" not found
and
Error in exists("a", mode = "numeric") : object "a" not found
whereas if I don't say anything about the mode, as in
test3 <- function(a) {
exists("a")
}
then test3(a) evaluates to TRUE (even though there is no ``a''
anywhere in
the search path.
Also with a redundant specification of the mode, as in
test4 <- function(a){
exists("a",mode="any")
}
one gets test4(a) to be TRUE (no error thrown).
I don't see the pattern. But it's probably not worth wasting time on.
> 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
Don't forget about R's lazy evaluation mechanism. In your example below, a
and b are as yet unevaluated promises. As such, they have various
properties, which you can investigate via something like:
foo <- function(a,b){browser()}
In your example, get("a") is an empty promise. For amusement, check its
length (it's 1 !).
As soon as you try to evaluate the promise, e.g. via <- ("assignment"), R
discovers that the promise cannot be evaluated, an so is missing. This then
throws the error. What is surprising to me, however, is that R does not
throw the error at the first assignment, x <- get("a"), but rather when it
tries to evaluate x in the next statement. However, as you said, these are
primitive functions, and we are now deep into the esoterica of the language,
pointers to pointers, pairlists, and so forth, about which I know nothing.
But hopefully this sheds a glimmer of light. You might be able to dig around
in the R language manual and find something there that sheds a bit more.
Cheers,
Bert Gunter