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

Dario Strbenac d@tr7320 @end|ng |rom un|@@ydney@edu@@u
Mon Nov 8 06:00:03 CET 2021


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


More information about the R-package-devel mailing list