Re: [R] Existence of formal arguments.

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Thu, 05 Jun 2008 20:47:56 -0400

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

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



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. Received on Fri 06 Jun 2008 - 01:29:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 06 Jun 2008 - 01:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive