[Rd] S4 default/missing args

John Chambers jmc at r-project.org
Fri May 14 22:59:02 CEST 2010


 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



More information about the R-devel mailing list