I'm trying to import a S3 method, predict
from another package pls
. I have a function which uses these predicted values. The problem is, when compiling the package:
Error : object 'predict' is not exported by 'namespace:pls'
I've put together this Gist as a minimal example which highlights my problem and contains the following R file:
#' Test function
#'
#' @importFrom pls predict
#'
#' @export
myfunc <- function(x){
stopifnot(class(x) == "mvr")
predict(x)*2
}
To summarise this as the original (below) is now out-dated and in places erroneous or misleading.
The proximal issue is that there is no function named predict
in the pls package; there are some unexported S3 methods for predict
but no such predict
. So you can't import this. The predict
generic lives in the stats package and you'll need to import from there as discussed below.
Your package needs to have Depends: pls
in the DESCRIPTION
in order for the correct predict
method to be available to R. There's nothing in pls that you can specifically import.
You also need to import the predict
generic from the stats namespace, so add
#' @importFrom stats predict
as that will import the generic in you packages namespace. You will also want to add Imports: stats
to your DESCRIPTION
file to indicate that you need the stats package; previously we didn't have to declare dependencies on the set of base packages shipped with R (i.e. the non-Recommended ones that ship with R).
Original
The main issue here is the pls doesn't define a function/method predict
. It provides several methods for the predict
generic, but not the generic itself.
You need to import the generic from the stats package, if you need it - I'm not sure you do as you aren't creating a function that needs or builds on the generic. At the minimum you'll need
#' @importFrom stats predict
though you may need/want to import the entire stats namespace - depends what your package does beyond the function your are currently working on.
The other issue is that predict.mvr
is not exported from the pls namespace
> require(pls)
Loading required package: pls
Attaching package: ‘pls’
The following object(s) are masked from ‘package:stats’:
loadings
> predict.mvr
Error: object 'predict.mvr' not found
> pls::predict.mvr
Error: 'predict.mvr' is not an exported object from 'namespace:pls'
> pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c("response",
"scores"), na.action = na.pass, ...)
As such you can't just import it. Hence your package needs to have Depends: pls
in the DESCRIPTION
in order for the correct predict
method to be found.