问题
下图显示了我的问题:
我正在尝试创建一个引用类ClassB
,PackageB
其中扩展ClassA
另一个包中的引用类PackageA
。
一旦ClassB
被实例化,R 就会抱怨PackageA
找不到私有(非导出)对象(特别是函数)。
最小工作示例 (MWE)
下图显示了 MWE 的工作原理:
ClassA
取决于PackageA
(fractions
来自import
-ed 包MASS
,并PrivateFunc
在其内部定义PackageA
)中的 2 个私有函数。ClassB
通过继承ClassA
.
一旦ClassB
被实例化,就会有一个错误抱怨PrivateFunc
找不到。这些错误发生在我构建PackageB
.
编码:
##########################################
# PackageA
### R/PackageA.R ###
#' @import methods MASS
PrivateFunc <- function(...) fractions(0.5) # try a function from MASS
#' @export ClassA
#' @exportClass ClassA
ClassA <- setRefClass("ClassA",
methods = list(
initialize = function(...) PrivateFunc()
)
)
### DESCRIPTION: ###
Package: PackageA
...
Imports: methods, MASS
### NAMESPACE: ###
# Generated by roxygen2 (4.0.1): do not edit by hand
export(ClassA)
exportClasses(ClassA)
import(MASS)
import(methods)
##########################################
# PackageB
### R/PackageB.R ###
#' @import methods PackageA
#' @export ClassB
#' @exportClass ClassB
ClassB <- setRefClass("ClassB", contains = "ClassA")
#' @export ClassBInstance
ClassBInstance <- ClassB() # <- Error here!!
### DESCRIPTION: ###
Package: PackageB
...
Imports: methods, PackageA
### NAMESPACE: ###
# Generated by roxygen2 (4.0.1): do not edit by hand
export(ClassB)
exportClasses(ClassB)
export(ClassBInstance)
import(PackageA)
我尝试构建时的错误PackageB
:
==> devtools::document(roclets=c('rd', 'collate', 'namespace'))
Updating PackageB documentation
Loading PackageB
Error in .Object$initialize(...) : could not find function "PrivateFunc"
Error: Failure in roxygen block beginning PackageB.R:2
using 'as.environment(NULL)' is defunct
Execution halted
Exited with status 1.
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source PackageB
* installing to library .../R/R-3.1.0/library'
* installing *source* package 'PackageB' ...
** R
** preparing package for lazy loading
Error in .Object$initialize(...) : could not find function "PrivateFunc"
Error : unable to load R code in package 'PackageB'
ERROR: lazy loading failed for package 'PackageB'
* removing '.../R/R-3.1.0/library/PackageB'
* restoring previous '.../R/R-3.1.0/library/PackageB'
Exited with status 1.
怎么了?
附加信息
version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance