[R-pkg-devel] match.arg With S4 Methods and Missing Inputs

Georgi Boshnakov georg|@bo@hn@kov @end|ng |rom m@nche@ter@@c@uk
Mon Nov 8 10:46:00 CET 2021


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

Get Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
From: R-package-devel <r-package-devel-bounces using r-project.org> on behalf of Andrew Simmons <akwsimmo using gmail.com>
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
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

        [[alternative HTML version deleted]]

______________________________________________
R-package-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list