From jmc at r-project.org  Fri May 14 22:59:02 2010
From: jmc at r-project.org (John Chambers)
Date: Fri, 14 May 2010 13:59:02 -0700
Subject: [Rd] S4 default/missing args
In-Reply-To: <D611103AA7EE3B4DAE7F7D49C72B291A028B8E90@EXMAIL2.bocad.bank-banque-canada.ca>
References: <D611103AA7EE3B4DAE7F7D49C72B291A028B8E90@EXMAIL2.bocad.bank-banque-canada.ca>
Message-ID: <4BEDB996.80004@r-project.org>

 From the documentation for setMethod():

Method definitions can have default expressions for arguments, but a 
current limitation is that the generic function must have some default 
expression for the same argument in order for the method's defaults to 
be used. <....>

(It would be nice to fix this, but would require some significant change 
to how methods are dispatched, with some possible inefficiencies.  The 
issue is that R processes formal arguments differently if there is no 
default expression.  Volunteers among the R hackers welcome, though.)

Meanwhile, if you own the generic function just give it a default, say:

setGeneric("TSdoc",
      def= function(x, con = stop("Sorry, no generic default"), ...) 
standardGeneric("TSdoc"))

John


On 5/14/10 9:50 AM, Paul Gilbert wrote:
> I am trying to understand the missing vs default value for an argument
> to an S4 method. (I'm not sure if this is a bug or my confusion.) In S3
> I can specify a default value for an argument and then both check if the
> argument was missing in the call, and use it because it takes on the
> default value. I do not seem to be able to do that in S4 as shown by
> this simplified example:
>
>
>
> setGeneric("TSdoc",
>
>     def= function(x, con, ...) standardGeneric("TSdoc"))
>
>
>
> setMethod("TSdoc",   signature(x="character", con="missing"),
>
>      definition= function(x, con=getOption("TSconnection"), ...){
>
>          if(is.null(con))
>
>             stop("con should be specified or set with
> options(TSconnection=con).")
>
>          else return('ok')} )
>
>
>
> Then when I try
>
>     TSdoc("B103")
>
> In my original problem I got
>
>     Error in is.null(con) : 'con' is missing
>
> And in the simplified example I get
>
>    Error in TSdoc("B103") :
>
>      element 1 is empty;
>
>       the part of the args list of 'is.null' being evaluated was:
>
>       (con)
>
>
>
> I'm not sure why there is a difference, but the implication is the same
> in both, the default
>
>> getOption("TSconnection")
>
> NULL
>
>
>
> Is not getting through to the is.null().  I can fix (or work around)
> this with
>
>
>
> setMethod("TSdoc",   signature(x="character", con="missing"),
>
>     definition= function(x, con,  ...){
>
>         if(is.null(con<- getOption("TSconnection")))
>
>            stop("con should be specified or set with
> options(TSconnection=con).")
>
>         else return('ok') } )
>
>
>
> TSdoc("B103")
>
> Error in TSdoc("B103") :
>
>    con should be specified or set with options(TSconnection=con).
>
>
>
> So my question is, is this a bug or do I have to adjust my programming
> style for the way missing/default arguments are handled in S4?
>
>
>
> Paul Gilbert
>
> ====================================================================================
>
> La version fran??aise suit le texte anglais.
>
> ------------------------------------------------------------------------------------
>
> This email may contain privileged and/or confidential information, and the Bank of
> Canada does not waive any related rights. Any distribution, use, or copying of this
> email or the information it contains by other than the intended recipient is
> unauthorized. If you received this email in error please delete it immediately from
> your system and notify the sender promptly by email that you have done so.
>
> ------------------------------------------------------------------------------------
>
> Le pr??sent courriel peut contenir de l'information privil??gi??e ou confidentielle.
> La Banque du Canada ne renonce pas aux droits qui s'y rapportent. Toute diffusion,
> utilisation ou copie de ce courriel ou des renseignements qu'il contient par une
> personne autre que le ou les destinataires d??sign??s est interdite. Si vous recevez
> ce courriel par erreur, veuillez le supprimer imm??diatement et envoyer sans d??lai ?
> l'exp??diteur un message ??lectronique pour l'aviser que vous avez ??limin?? de votre
> ordinateur toute copie du courriel re??u.
>
> 	[[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] S4 default/missing args
John Chambers