The Problem
The diagram below shows my problem:
I am trying to create a reference class ClassB
in PackageB
which extends reference class ClassA
in another package PackageA
.
As soon as ClassB
is instantiated, R complains that private (non-exported) objects (functions, in particular) of PackageA
can't be found.
Minimal Working Example (MWE)
The diagram below shows how the MWE works:
ClassA
depends on 2 private functions in PackageA
(fractions
from import
-ed package MASS
, and PrivateFunc
defined within PackageA
itself). ClassB
implicitly also depends on these functions, through its inheritance of ClassA
.
As soon as ClassB
is instantiated, there is an error complaining that PrivateFunc
cannot be found. These errors occur when I build PackageB
.
The code:
##########################################
# 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)
The errors when I try to build 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.
What's going wrong?
Additional Info
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