s-news
[Top] [All Lists]

Re: (non)-evaluation of an optional argument - ANSWERS

To: s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu
Subject: Re: (non)-evaluation of an optional argument - ANSWERS
From: "Patrick Joseph" <patrick_nospam@hotmail.com>
Date: Fri, 16 Jul 2004 09:55:14 -0400


Thanks to all who replied to my question. Here is a summary of the answers:
____________________________

David Lorenz:
"(...) replace the first line in your function with: dots <-list(...)
That gives you the values associated with the arguments. (...)"

(After receipt of this answer I clarified my question, specifying that actually I call myfct() from another function (Jagadish calls it below the super function) that calculates the variable tmp. It was irrelevant since David's solution still works.)

Jagadish Rangrej:
"try using in your super function: do.call('myfct',list(a,b=tmp))"

Tony Plate:
"use eval(dots$b, sys.parent()) or David's suggestion"

Andy Liaw:
uses David's suggestion and forces evaluation with the call 'dots$b'

Steve Riley:
recommends the use of eval(dots$b). It works if I call myfct() from the global frame but not from my super function (but my first question did not mention about it, my fault).

Bill@insightful comments David's suggestion:
"avoids scoping problems and is probably faster. It does mean that all the arguments in the ... list are evaluated, not just the 'b'."




_____________________________________________________

"Patrick Joseph"
<patrick_nospam@hotmail.com        To: s-news@lists.biostat.wustl.edu
Subject:  [S] (non)-evaluation of an optional argument
07/14/2004 09:07 PM

Hi,

I have a question about the evaluation of an optional argument of a
function. Let:

myfct _ function(a,...) {
            dots <- match.call(expand.dots = F)$...
            print(dots)
            if(!is.null(dots$b))
                        return(a + dots$b)
            else return(a)
}

When I do the call myfct(10), obviously it prints "list()" and returns 10.
When I do the call myfct(10,b=9), it prints "list(b = 9)" and returns 19.
Ok.

Now if I define tmp<-9 and do the call myfct(10,b=tmp), it prints "list(b
=
tmp)" and returns the error message: "Error in a + dots$b: Non-numeric
second operand".

Is it possible to force the evaluation of tmp when calling the function?

Thank you for any hint,
Patrick

(I'm using S-Plus 4.5 on Windows 2000)

_________________________________________________________________
MSN Premium with Virus Guard and Firewall* from McAfee® Security : 2 months FREE* http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines


<Prev in Thread] Current Thread [Next in Thread>
  • Re: (non)-evaluation of an optional argument - ANSWERS, Patrick Joseph <=