Roxygen2 - how to @export reference class generator?
Asked Answered
H

1

12

For instance, say I have the following package called Test and I want to export class A:

# In /R/Test.R:
#' @docType package
#' @import methods
#' @exportClass A
A <- setRefClass("A", methods = list(foo = identity))

However, after building and loading, I get the following error when using A's generator:

> library(Test)
> A()$foo(1)
Error: could not find function "A"

I've checked the contents of my NAMESPACE file is fine:

exportClasses(A)
import(methods)

So what's going wrong? Why isn't my class generator being exported?

Hexa answered 3/2, 2014 at 16:21 Comment(3)
Does #6759544 help?Torchwood
@Torchwood - not particularly, since that question talks at length about documentation tags, rather than why the @export/@exportClass tags are failing to export the class generator.Hexa
You need @export as well to export the generator.Parris
T
14

If you add @export A then the generator function A will be exported, too, e.g.

#' A class description
#'
#' @import methods
#' @export A
#' @exportClass A
A = setRefClass('A',
  fields=list(name='character', n='numeric'),
  methods=list(
    hello=function() {
      "A greeting"
      return(paste0('Hello, ', name))
    }
  )
)

Important: Don't forget to explicitly mention A in the export directive or it doesn't appear to work, unlike for functions.

Alternatively, as the class is being exported, you can still use the class via new(), e.g.

> a = new('A', name='Josh', n=12345)
> a$hello()
 [1] "Hello, Josh"

but it's easy to just add the export.

Taunt answered 6/5, 2014 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.