[Rd] missing imports not detected by 'R CMD check' (?)

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Fri Sep 27 16:44:36 CEST 2019

>>>>> Henrik Bengtsson 
>>>>>     on Tue, 24 Sep 2019 11:42:13 -0700 writes

[to me (as maintainer of 'Matrix') and allowed me to answer in public] :

> I ran into a problem where 'R CMD check' on my in-house package, which
> only indirectly depends on 'Matrix', produce errors like:
> [where=<environment: namespace:Matrix>, where2=<environment: namespace:Matrix>]
> Error in setGeneric("expand", function(x, ...) standardGeneric("expand")) :
>   could not find function "setGeneric"

> Looking at your https://cran.r-project.org/web/packages/Matrix/NAMESPACE:

> ## try to import all we need, but not more
> importFrom("methods"
>   ## still needed {group generics needed to be explicitly imported} ?
>          , Ops, Arith, Compare, Logic, Math, Math2, Summary, Complex
>   ## generics for which we export new methods:
>          , cbind2, rbind2, coerce, show
>          , kronecker
>   ## things we call,.. necessary when Matrix is loaded, but not attached, as in

>   ## Rscript --vanilla -e 'require(methods);(M <- Matrix::Matrix(0:1,3,3));  as(M,"sparseMatrix")'
>          , as, is, extends, new
>          , callGeneric, callNextMethod
>          , .selectSuperClasses, .slotNames, canCoerce, packageSlot
>          , getClass, getClassDef, validObject
>          , setClass, setClassUnion, setMethod, setOldClass
>          , setValidity, slot, "slot<-", slotNames, .hasSlot
>          , signature, representation, prototype)

> I see that you do not list 'setGeneric' in importFrom("methods", ...).
> If I add that manually and reinstall 'Matrix', then 'R CMD check' no
> longer produces the above error.

> PS. My problem only reveals itself on an R installation that I built
> without installing 'recommended' packages in the system library - they
> do exist in R_LIBS_USER though.  I don't know if that's related or
> not, but just wanted to mention it.
> PPS. I see similar issues with packages that uses globalVariables()
> without importFrom("utils", "globalVariables").  Hence my R-pkg-devel
> on 'Why doesn't R CMD check warn that globalVariables() is
> undefined/not imported?' post on 2019-09-24
> (https://stat.ethz.ch/pipermail/r-package-devel/2019q3/004440.html).

Thank you,  Henrik,
this is somewhat interesting and more widely relevant than just
to Matrix I think, and therefore is going to the R-devel list.

Your scenario (in PS) is a bit unusual,  but of course, you can
easily make it realistic scenario with a non-Recommended package
that is similar to Matrix  (but for "Minimal reproducible
example" !) could be much smaller.

I checked a bit further .. not systematically and found at least
two more methods /  functions I use and  the R CMD check tools
have never warned me about (and so they are not in Matrix/NAMESPACE 
   because Matrix may be older than namespaces, and even if not,
   originally, one did not import the the things from "Depends:" packages,
   where 'methods' had been for a long time in Matrix/DESCRIPTION ) :


 standardGeneric()  and 

are also not in Matrix' NAMESPACE and are used in Matrix/R/*.R
code, and that does not give any warnings from R CMD check.

Unfortunately, I don't have time at the moment to delve into
this to debug .. but I rather "post it" for now, so it is less
easily forgotten.


Martin Maechler
ETH Zurich and R Core

More information about the R-devel mailing list