Search code examples
rpackager-s4

In R, how can I reference an S4 class from another package in my class definition?


Suppose I have two packages PackageA and PackageB. I have an S4 class, ClassA, in PackageA that I would like to use as a base class for ClassB in PackageB:

setClass(
    "ClassB", 
    slots = c(), 
    validity = function(object) {

        T
    }
    contains = "ClassA")

However, when I build I get the error:

no definition found for superclass "ClassA"

I have tried adding a reference to the PackageA with devtools:

devtools::use_package("PackageA")

Perhaps I need to use an roxygen directive?


Solution

  • Turns out that ClassA was not being imported properly. Adding the correct roxygen directive solved the problem:

    #' @import PackageA
    setClass(
        "ClassB", 
        slots = c(), 
        validity = function(object) {
    
            T
        }
        contains = "ClassA")