When writing an R package that uses the Matrix package, why do I need to specify Matrix :: t () instead of t ()?

Consider the following simple functions defined in an R session:

nathanvan@nathanvan-N61Jq :~$ R R version 3.0.1 (2013-05-16) -- "Good Sport" ... snip ... > make.a.Matrix <- function(data, nrow, ncol) { + require(Matrix) + return( Matrix(data, nrow=nrow, ncol=ncol)) + } > > transpose.a.Matrix <- function(data, nrow, ncol ) { + return(t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) + } > > make.a.Matrix(1:12, 3, 4) Loading required package: Matrix Loading required package: lattice 3 x 4 Matrix of class "dgeMatrix" [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > transpose.a.Matrix(1:12, 3, 4) 4 x 3 Matrix of class "dgeMatrix" [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 

If we put the same functions in a package, the transpose.a.Matrix function no longer works. Since the description of the package creation process would be too long, I just posted a copy of the package here . I sent the DESCRIPTION and NAMESPACE at the end of the question. If other parts are relevant, I will also be happy to publish them. Just let me know!

 nathanvan@nathanvan-N61Jq :~$ R R version 3.0.1 (2013-05-16) -- "Good Sport" ... snip ... > require(minimalbugexample) Loading required package: minimalbugexample Loading required package: Matrix Loading required package: lattice Loading required package: testthat > make.a.Matrix(1:12, 3, 4) 3 x 4 Matrix of class "dgeMatrix" [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > transpose.a.Matrix(1:12, 3, 4) Error in t.default(make.a.Matrix(data, nrow = nrow, ncol = ncol)) : argument is not a matrix > transpose.a.Matrix function(data, nrow, ncol ) { return(t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) } <environment: namespace:minimalbugexample> 

I think the key here is something weird in the namespace. Please note that if I debug a function, I can manually call Matrix::t , and it will work until base::t completes with the same error.

 > debug(transpose.a.Matrix) > transpose.a.Matrix(1:12, 3, 4) debugging in: transpose.a.Matrix(1:12, 3, 4) debug at /home/nathanvan/Ubuntu One/workspace/experimental-design/software/minimalbugexample/R/use-Matrix-package.R#31: { return(t(make.a.Matrix(data, nrow = nrow, ncol = ncol))) } Browse[2]> t(Matrix(1:12, 3, 4)) Error in t.default(Matrix(1:12, 3, 4)) : argument is not a matrix Browse[2]> t function (x) UseMethod("t") <bytecode: 0x46b0a88> <environment: namespace:base> Browse[2]> Matrix::t(Matrix(1:12, 3, 4)) 4 x 3 Matrix of class "dgeMatrix" [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 Browse[2]> base::t(Matrix(1:12, 3, 4)) Error in t.default(Matrix(1:12, 3, 4)) : argument is not a matrix 

And yet, using showMethods , he assumes that only using t should find the right one, even if it is not.

 Browse[2]> showMethods('t') Function: t (package base) x="ANY" x="CsparseMatrix" x="dgeMatrix" x="diagonalMatrix" x="dppMatrix" x="dsCMatrix" x="dspMatrix" x="dsTMatrix" x="dsyMatrix" x="dtpMatrix" x="dtrMatrix" x="dtTMatrix" x="lgeMatrix" x="lspMatrix" x="lsTMatrix" x="lsyMatrix" x="ltpMatrix" x="ltrMatrix" x="ltTMatrix" x="matrix" (inherited from: x="ANY") x="Matrix" x="ngeMatrix" x="nspMatrix" x="nsTMatrix" x="nsyMatrix" x="ntpMatrix" x="ntrMatrix" x="ntTMatrix" x="pMatrix" x="RsparseMatrix" x="TsparseMatrix" 

Currently, I can β€œfix” it by editing the source code of the package so that the transpose.a.Matrix function indicates that it needs the Matrix::t method:

 transpose.a.Matrix <- function(data, nrow, ncol ) { require(Matrix) return(Matrix::t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) } 

But it does not seem to be necessary. What am I missing?

My DESCRIPTION file

 Package: minimalbugexample Title: Description: Version: 0.1 Author: Nathan VanHoudnos < nathanvan@letterafterFmail.com > Maintainer: Nathan VanHoudnos < nathanvan@letterafterFmail.com > Depends: R (>= 3.0.1), Matrix (>= 1.0), testthat License: GPL LazyData: true Collate: 'minimalbugexample-package.r' 'use-Matrix-package.R' 

My NAMESPACE File

 export(make.a.Matrix) export(transpose.a.Matrix) 

and I can send additional snippets upon request.

+7
namespaces r sparse-matrix
source share
1 answer

Working example on gitHub

I put a working example on gitHub to make it easy to view different files .

FYI is not a very minimal example, since it was created using devtools . β€œExtra” is (1) that roxygen2 comments are what the NAMESPACE file creates, and (2) it includes unit testing with testthat . All of this can be ignored for the purposes of this example.

Key fix

The short answer I made, essentially, was to change the NAMESPACE file to:

 export(make.a.Matrix) export(transpose.a.Matrix) importFrom(Matrix,Matrix) importFrom(Matrix,t) 

So that R can find the correct transpose version. See This post for a great description of how R looks for functions.

Although this is not strictly necessary, I changed my DESCRIPTION file to a few clean ones:

 Package: minimalbugexample Title: Description: Version: 0.1.3 Author: Nathan VanHoudnos < nathanvan@letterafterFmail.com > Maintainer: Nathan VanHoudnos < nathanvan@letterafterFmail.com > Depends: R (>= 3.0.1), Matrix (>= 1.0) Suggests: testthat (>= 0.7.1.99) License: GPL LazyData: true Collate: 'minimalbugexample-package.r' 'use-Matrix-package.R' 

Note that I use Depends: for Matrix instead of Imports: so that the user can use the Matrix objects returned by the functions. If this example used only Matrix material without presenting it to the user, I would use Imports:

Proof that it works

 > require(minimalbugexample) > make.a.Matrix(1:12, 3, 4) 3 x 4 Matrix of class "dgeMatrix" [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > transpose.a.Matrix(1:12, 3, 4) 4 x 3 Matrix of class "dgeMatrix" [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 > t( make.a.Matrix(1:12, 3, 4)) 4 x 3 Matrix of class "dgeMatrix" [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 

Note that the last command did not work if I specified Matrix in Imports: and not Depends:

+3
source share

All Articles