[R-pkg-devel] match.arg With S4 Methods and Missing Inputs
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon Nov 8 16:43:30 CET 2021
>>>>> Georgi Boshnakov
>>>>> on Mon, 8 Nov 2021 09:46:00 +0000 writes:
> You need to define the generic with a default value for
> this parameter. Methods can then have a different default
> value for it.
> I remember reading this in S4's documentation but don't remember where.
> Georgi Boshnakov
interesting ... and would make quite some sense.
Can others confirm / disprove ?
Even as co-author of the "using S4 almost everywhere" package 'Matrix'
I wouldn't have known this.
If this is seen to be true (I don't have time for checking just now),
I think it's something we really *should* add to one or more of
the related help pages.
Martin Maechler
> ________________________________
> Sent: Monday, November 8, 2021 5:37:18 AM
> To: Dario Strbenac <dstr7320 using uni.sydney.edu.au>
> Cc: r-package-devel using r-project.org <r-package-devel using r-project.org>
> Subject: Re: [R-pkg-devel] match.arg With S4 Methods and Missing Inputs
>> From the line `function(A, B) standardGeneric("SetOfParams")`, A and B will
> always have default values of R_MissingArg
> Providing default values within the methods does nothing since A and B have
> already been initialized before arriving at the method.
> You could do something like:
> if (missing(A))
> A <- ...
> if (missing(B))
> B <- ...
> within each method, and that would emulate having default values for A and
> B.
> On Mon, Nov 8, 2021 at 12:00 AM Dario Strbenac <dstr7320 using uni.sydney.edu.au>
> wrote:
>> Good day,
>>
>> How can a parameter take a default value from a vector of permitted ones,
>> if it is missing?
>>
>> setClassUnion("characterOrMissing", c("character", "missing"))
>> setClassUnion("integerOrMissing", c("integer", "missing"))
>> setClass("SetOfParams", representation(A = "characterOrMissing", B =
>> "integer"))
>> setGeneric("SetOfParams", function(A, B) standardGeneric("SetOfParams"))
>>
>> setMethod("SetOfParams", c("missing", "missing"), function() # Empty constructor
>> {
>> new("SetOfParams", A = "M", B = 100L)
>> })
>>
>> setMethod("SetOfParams", c("characterOrMissing", "integerOrMissing"),
>> function(A = c("L", "M", "N"), B = 100L)
>> {
>> A <- match.arg(A)
>> new("SetOfParams", A = A, B = B)
>> })
>>
>> SetOfParams(B = 500L)
>> Error in match.arg(A) : argument "A" is missing, with no default.
>>
>> How can I avoid the error about A having no default? I thought I specified
>> it so that it does have one, which match.arg would set for me if the user
>> did not specify one.
>>
>> --------------------------------------
>> Dario Strbenac
>> University of Sydney
>> Camperdown NSW 2050
>> Australia
>> ______________________________________________
>> R-package-devel using r-project.org mailing list
More information about the R-package-devel
mailing list