How to define package examples that reference an S4 class in another package

63 Views Asked by At

I am building a package that extends the functionality of another package, crmPack. crmPack uses S4 classes. I'm using roxygen to document my new package. I'm getting an error when running devtools::check() on my new package:

> checking examples ... ERROR
  Running examples in ‘crmreporter-Ex.R’ failed
  The error most likely occurred in:

  > base::assign(".ptime", proc.time(), pos = "CheckExEnv")
  > ### Name: summariseIncrementsRule
  > ### Title: Create a Tibble Describing an Increments Rule
  > ### Aliases: summariseIncrementsRule
  > 
  > ### ** Examples
  > 
  > if (requireNamespace("crmPack")) {
  +   inc <- IncrementsRelative(intervals=c(0, 20), increments=c(1, 0.33))
  +   summariseIncrementsRule("inc")
  + }
  Error in IncrementsRelative(intervals = c(0, 20), increments = c(1, 0.33)) : 
    could not find function "IncrementsRelative"
  Execution halted

Here's the relevant section of the roxygen comments and the definition of the generic in question:

#' @examples
#' if (requireNamespace("crmPack")) {
#'   inc <- IncrementsRelative(intervals=c(0, 20), increments=c(1, 0.33))
#'   summariseIncrementsRule(inc)
#' }
#' @import crmPack
#' @importFrom methods .valueClassTest
#' @export
setGeneric("summariseIncrementsRule", valueClass = "tbl", function(object) {
  standardGeneric("summariseIncrementsRule")
})

I have also tried

#' @importFrom crmPack IncrementsRelative

in the roxygen comments for the generic, with the same result.

crmPack appears in the Imports: section of my DESCRIPTION file and my NAMSEPACE file includes import(crmPack).

How should I document my generic so that its example runs correctly? [Which it does when run from the console.]

1

There are 1 best solutions below

1
Limey On

Following on from @Waldi's question, I tried:

#' @examples
#' if (requireNamespace("crmPack")) {
#'   inc <- crmPack::IncrementsRelative(intervals=c(0, 20), increments=c(1, 0.33))
#'   summariseIncrementsRule(inc)
#' }

And this ran without error. I was also able to remove #' @import crmPack from the roxygen comments, but removing #' @importFrom methods .valueClassTest caused devtools::check() to issue a warning.