S7 Replacement Methods and the R CMD Check Crash

Your code is fine. R CMD check just can’t handle S7 replacement ops yet.

r
s7
packages
Author

James Balamuta

Published

February 8, 2026

If you’re using S7 in an R package and registering methods for replacement operators ($<-, [[<-, [<-), you’ve probably hit this:

W  checking replacement functions ...
   Error in get(f, envir = code_env) : invalid first argument
   The argument of a replacement function which corresponds to the right
   hand side must be named 'value'.

Your code is fine. This is a bug in R CMD check.

What’s going on

When S7 registers a replacement method, it stores a function object in the S3 methods table. R CMD check’s checkReplaceFuns() expects string names there. It coerces the function to "", calls get(""), and crashes. The “must be named value” message is boilerplate that prints after the crash. It never actually inspected your function signature.

Is there a fix?

Yes. Martin Maechler fixed this in R-devel across three commits1 2 3:

From the R-devel NEWS (July 11, 2025):

tools::checkReplaceFuns() now deals better with replacement methods not available as regular functions in the namespace.

This ships in R 4.6. It is not in R 4.5.x or earlier.

The workaround

Move your replacement method registrations into .onLoad(). R CMD check only scans top-level source assignments, so methods registered at load time bypass the check entirely while dispatching identically at runtime.

Read-only methods ($, [[, names) are unaffected and can stay at the top level.

# R/methods.R
S7::method(`$`, MyClass)   <- function(x, name) { S7::prop(x, name) }
S7::method(`[[`, MyClass)  <- function(x, i, ...) { S7::prop(x, i) }
S7::method(names, MyClass) <- function(x) { names(S7::props(x)) }

# Implementations for replacement ops (registered in zzz.R)
.my_dollar_set   <- function(x, name, value) { S7::prop(x, name) <- value; x }
.my_bracket2_set <- function(x, i, ..., value) { S7::prop(x, i) <- value; x }

# R/zzz.R
.onLoad <- function(libname, pkgname) {
  S7::methods_register()
  S7::method(`$<-`, MyClass)  <- .my_dollar_set
  S7::method(`[[<-`, MyClass) <- .my_bracket2_set
}

Fin

This is one of those rough edges that comes with adopting a new object system before the toolchain has fully caught up. S7 itself is doing the right thing; R CMD check just hasn’t learned to look for methods stored as function objects rather than name strings. The .onLoad() workaround is clean enough to live with until R 4.6 ships the proper fix.

References

Footnotes

  1. Initial fix to checkReplaceFuns() in R-devel (svn 88398, Jul 10, 2025). View commit.↩︎

  2. Follow-up amendment (svn 88404, Jul 11, 2025). View commit.↩︎

  3. Reporting improvement (svn 88418, Jul 16, 2025). View commit.↩︎