Roxygen2-如何@export引用类生成器? [英] Roxygen2 - how to @export reference class generator?
问题描述
例如,假设我有一个名为Test
的以下程序包,并且我想导出类A
:
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))
但是,在构建和加载后,使用A
的生成器时出现以下错误:
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"
我检查了NAMESPACE
文件的内容是否正常:
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?
推荐答案
如果添加@export A
,则生成器功能A
也将被导出,例如
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))
}
)
)
重要提示:不要忘记在export指令中明确提及A
,否则它似乎不起作用,与功能不同.
Important: Don't forget to explicitly mention A
in the export directive or it doesn't appear to work, unlike for functions.
或者,在导出类 时,您仍然可以通过new()
使用该类,例如
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"
但是添加导出很容易.
这篇关于Roxygen2-如何@export引用类生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!