s-news
[Top] [All Lists]

Re: (non)-evaluation of an optional argument

To: lorenz@usgs.gov
Subject: Re: (non)-evaluation of an optional argument
From: Agin.Patrick@hydro.qc.ca
Date: Thu, 15 Jul 2004 10:01:02 -0400
Cc: s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu

Thank you David for your answer.
The problem is that myfct() is actually called from another function (say myfct.0) so the variable tmp in my example is not a global variable. In other words, eval("b=tmp") does not return the desired value.

Am I wrong?
Patrick

-----Original Message-----
From: David L Lorenz [mailto:lorenz@usgs.gov]
Sent: Thursday, July 15, 2004 7:56 AM
To: Patrick Joseph
Cc: s-news@lists.biostat.wustl.edu; s-news-owner@lists.biostat.wustl.edu
Subject: Re: [S] (non)-evaluation of an optional argument


Patrick,
  The more common way I've seen of doing what you want is to replace the
first line in your function with:
dots <- list(...)
  That gives you the values associated with the arguments. The match.call()
function can tell you what was specified at the command line, but does not
evaluate the arguments. So when you type b=9 as an argument, S-PLUS can
interpret the value of b to be 9, but when you type b=tmp, it cannot
interpret tmp as a number. You could force S-PLUS to recognize it by using
eval(dots$b).
Dave


                                                                                                                                          

                      "Patrick Joseph"                                                                                                    

                      <patrick_nospam@hotmail.c        To:       s-news@lists.biostat.wustl.edu                                           

                      om>                              cc:                                                                                

                      Sent by:                         Subject:  [S] (non)-evaluation of an optional argument                             

                      s-news-owner@lists.biosta                                                                                           

                      t.wustl.edu                                                                                                         

                                                                                                                                          

                                                                                                                                          

                      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)

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN Premium
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines


--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news





--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>