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.