Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen writing an R package that uses the Matrix package, why do I have to specify Matrix::t() instead of just t()?
    primarykey
    data
    text
    <p>Consider the following simple functions defined in an R session:</p> <pre><code>nathanvan@nathanvan-N61Jq:~$ R R version 3.0.1 (2013-05-16) -- "Good Sport" ... snip ... &gt; make.a.Matrix &lt;- function(data, nrow, ncol) { + require(Matrix) + return( Matrix(data, nrow=nrow, ncol=ncol)) + } &gt; &gt; transpose.a.Matrix &lt;- function(data, nrow, ncol ) { + return(t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) + } &gt; &gt; 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 &gt; 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 </code></pre> <p>If we put those same functions into a package, the <code>transpose.a.Matrix</code> function no longer works. <em>Since describing the package creation process would be too lengthy, I have simply posted a copy of the package <a href="http://www.stat.cmu.edu/~nmv/wp-content/uploads/2013/07/minimalbugexample_0.1.tar.gz">here</a>. I have posted the <code>DESCRIPTION</code> and <code>NAMESPACE</code> files at the end of the question. If other pieces are relevant, I'd be happy to post them too. Just let me know!</em></p> <pre><code>nathanvan@nathanvan-N61Jq:~$ R R version 3.0.1 (2013-05-16) -- "Good Sport" ... snip ... &gt; require(minimalbugexample) Loading required package: minimalbugexample Loading required package: Matrix Loading required package: lattice Loading required package: testthat &gt; 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 &gt; transpose.a.Matrix(1:12, 3, 4) Error in t.default(make.a.Matrix(data, nrow = nrow, ncol = ncol)) : argument is not a matrix &gt; transpose.a.Matrix function(data, nrow, ncol ) { return(t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) } &lt;environment: namespace:minimalbugexample&gt; </code></pre> <p>I think that the key here is something weird about the namespace. Notice that if I debug the function, I can manually call the <code>Matrix::t</code> and it will work while the <code>base::t</code> fails with the same error.</p> <pre><code>&gt; debug(transpose.a.Matrix) &gt; 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]&gt; t(Matrix(1:12, 3, 4)) Error in t.default(Matrix(1:12, 3, 4)) : argument is not a matrix Browse[2]&gt; t function (x) UseMethod("t") &lt;bytecode: 0x46b0a88&gt; &lt;environment: namespace:base&gt; Browse[2]&gt; 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]&gt; base::t(Matrix(1:12, 3, 4)) Error in t.default(Matrix(1:12, 3, 4)) : argument is not a matrix </code></pre> <p>And yet using <code>showMethods</code>, it suggest that just using <code>t</code> should find the right one, even though it did not.</p> <pre><code>Browse[2]&gt; 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" </code></pre> <p>For now, I can “fix” it by editing the source for the package so that the <code>transpose.a.Matrix</code> function specifies that it needs the <code>Matrix::t</code> method:</p> <pre><code>transpose.a.Matrix &lt;- function(data, nrow, ncol ) { require(Matrix) return(Matrix::t( make.a.Matrix(data, nrow=nrow, ncol=ncol) )) } </code></pre> <p>But that seems like it should not be needed. What am I missing?</p> <p>My DESCRIPTION file is</p> <pre><code>Package: minimalbugexample Title: Description: Version: 0.1 Author: Nathan VanHoudnos &lt;nathanvan@letterafterFmail.com&gt; Maintainer: Nathan VanHoudnos &lt;nathanvan@letterafterFmail.com&gt; Depends: R (&gt;= 3.0.1), Matrix (&gt;= 1.0), testthat License: GPL LazyData: true Collate: 'minimalbugexample-package.r' 'use-Matrix-package.R' </code></pre> <p>My NAMESPACE file is</p> <pre><code>export(make.a.Matrix) export(transpose.a.Matrix) </code></pre> <p>and I can post additional pieces upon request. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload