rr-package

R devtools::check() WARNING: "The argument of a replacement function which corresponds to the right hand side must be named 'value'."


I am using devtools::check() in R to check a package and cannot overcome this warning:

The argument of a replacement function which corresponds to the right hand side must be named 'value'.

You can see below in the example code that I do indeed have value as the RHS argument for the function. Other replacement functions in the same package do not throw this warning. What am I doing wrong?

methods::setGeneric(name = "addTable<-", def = function(x, value, ...) standardGeneric("addTable<-"))

#' MyClass
#'
#' @aliases MyClass
#' @rdname MyClass
#' @exportClass MyClass
MyClass <- methods::setClass(
    Class = "MyClass",
    slots = list(
        table = "data.frame"
    ),
    prototype = prototype(
        table = data.frame()
    )
)

#' Attach a data frame to a MyClass object
#'
#' @description `addTable()` adds a table to a `MyClass` object.
#'
#' @param x A `MyClass`.
#' @param value A `data.frame`.
#'
#' @returns A `MyClass` object.
#'
#' @aliases addTable<-
#' @rdname addTable
#' @exportMethod addTable<-
methods::setMethod(
    f = "addTable<-",
    signature = c(x = "MyClass", value = "data.frame"),
    function(x, value) {
    
    x@table <- value
    x
    
    }
)

Upon devtools::check(), this returns the warning:

checking replacement functions ... WARNING 'addTable<-' '\S4method{addTable<-}{MyClass,data.frame}' The argument of a replacement function which corresponds to the right hand side must be named 'value'.

I've tried:

Obviously, I'm just casting rocks in an ocean and hoping to hit a fish.

I'm running R 4.4.0 and RTools 4.4.


Solution

  • S4 Setter Functions

    For S4 setters, the "value" argument must be the last argument in the function definition. Most of the time, setters in the wild have two arguments, the object and the new value like below:

    ## toy class
    methods::setClass("Numbers", slots = c(number = "numeric"))
    
    ## create a setter generic
    methods::setGeneric("number<-", function(x, value) standardGeneric("number<-"))
    
    ## create a method for `Numbers` class
    methods::setMethod("number<-", "Numbers", function(x, value) {
      x@number <- value
      x
    })
    
    ## instantiate a `Numbers` object
    obj <- new("Numbers", number = 5)
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 5
    
    ## usage
    number(obj) <- 3
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 3
    
    ## alternate usage - no assignment
    `number<-`(obj, 1)
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 1
    
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 3
    
    ## alternate usage - requires assignment
    obj <- `number<-`(obj, 1)
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 1
    

    If you want to add additional arguments to the setter, they must be included before the "value" argument either as named arguments or ellipsis like so:

    methods::setGeneric("fancy_number<-", function(x, ..., value) standardGeneric("fancy_number<-"))
    

    These additional arguments can be used to modify how the values are assigned.

    methods::setMethod("fancy_number<-", "Numbers", function(x, operation = c("add", "replace"), value) {
      if (operation == "add") {
        x@number <- x@number + value 
      } else if (operation == "replace") {
        number(x) <- value
      }
      x
    })
    
    ## usage - note position of new arguments on left of arrow
    fancy_number(obj, "add") <- 100
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 101
    
    fancy_number(obj, "replace") <- 7
    obj
    
    # An object of class "Numbers"
    # Slot "number":
    # [1] 7
    

    This is probably why "value" must be the last argument - R needs to know what one value is being assigned.