Skip to content

Problem with "missing" in "local" (PR#1241)

2 messages · Jonathan Rougier, Luke Tierney

#
Hi Brian,
Prof Brian Ripley wrote:
By "reasonable" in this context I mean that the operation would appear
to be well-defined and useful.  I can access x within the local()
environment, ie it is found in the enclosing environment, and I do not
think that y should be treated any differently.  If I modify my function
to read

fred <- function(x, y)
{
  x <- as.vector(x)
  local({
    print(x)
    print(y)
    if (missing(y)) print("No \"y\" today")
  })
  x
}

then I get the error message
[1]  1  2  3  4  5  6  7  8  9 10
Error in print(y) : Argument "y" is missing, with no default

This error message suggests to me that "missing(y)" would evaluate
correctly.
The help file for "missing".  This points out that "missing" is not
always reliable, eg after "x <- match.arg(x)".  Perhaps this could be
another example of "unreliability".

Cheers, Jonathan.
#
I think Jonathan has a point.  Here is another variation that does not
involve explicit eval or local:

	f<-function(x){
	    g<-function() missing(x)
	    g()
	}

	> f()
	Error in g() : "missing" illegal use of missing

This sort of thing does not arise in S, but with lexical scope in R it
seems natural to me at least that missing should work on lexically
visible variables.  The error produced by

	f<-function(x){
	    g<-function() x
	    g()
	}

	> f()
	Error in g() : Argument "x" is missing, with no default

also suggests that it would be nice to be able to use missing to guard
against this error.

For the short term we should add a note to the help file for missing
(I will do this), but longer term I think we should revise the missing
inplementation to handle these cases more naturally (unless anyone
sees anything seriously wrong with that).

By the way, the help for missing says:

     `missing(x)' is only reliable if `x' has not been altered since
     entering the function: in particular it will always be true after
     `x <- match.arg(x)'. 

I believe that should be "... it will always be false after ..."?

luke
On Fri, Jan 04, 2002 at 03:48:07PM +0100, J.C.Rougier@durham.ac.uk wrote:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._