考虑在R会话中定义的以下简单函数:
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如果我们将这些相同的函数放入包中,则transpose.a.Matrix函数将不再工作。由于描述包创建过程太长,所以我只发布了包http://www.stat.cmu.edu/~nmv/wp-content/uploads/2013/07/minimalbugexample_0.1.tar.gz的副本。我已经把DESCRIPTION和NAMESPACE文件张贴在问题的末尾。如果其他文章是相关的,我也很乐意把它们发出去。就让我知道!
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>我认为这里的关键是名称空间的一些奇怪之处。注意,如果我调试这个函数,我可以手动调用Matrix::t,当base::t失败时,它也会工作,同时也会出现同样的错误。
> 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然而,使用showMethods,它建议,仅仅使用t应该找到正确的一个,尽管它没有。
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"现在,我可以通过编辑包的源代码来“修复”它,以便transpose.a.Matrix函数指定它需要Matrix::t方法:
transpose.a.Matrix <- function(data, nrow, ncol ) {
require(Matrix)
return(Matrix::t( make.a.Matrix(data, nrow=nrow, ncol=ncol) ))
}但这似乎是不应该需要的。我遗漏了什么?
我的描述文件是
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'我的命名空间文件
export(make.a.Matrix)
export(transpose.a.Matrix)如有要求,我还可以再寄一些。
发布于 2013-07-11 20:19:44
关于gitHub的一个工作示例
我在gitHub上放了一个示例,这样就可以轻松地浏览不同的文件了。。
FYI,这不是一个很小的例子,因为它是用devtools构建的。“附加”是(1) roxygen2注释是构建命名空间文件的内容,(2)它将单元测试与testthat结合在一起。就本例而言,所有这些都可以忽略。
关键修复
实际上,我所做的简短回答需要将我的命名空间文件更改为:
export(make.a.Matrix)
export(transpose.a.Matrix)
importFrom(Matrix,Matrix)
importFrom(Matrix,t)这样R就能找到正确的转座子。有关R如何搜索函数的很好的描述,请参阅此帖子。
虽然不是严格意义上的必要,但我修改了描述文件,使其稍微干净一点:
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'请注意,我将Depends:用于Matrix而不是Imports:,以便用户能够使用函数返回的Matrix对象。如果这个例子只在内部使用Matrix,而没有将它呈现给用户,我就会使用Imports:。
证明它有效
> 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注意,如果我在Matrix中指定了Imports:而不是Depends:,那么最后一个命令就会失败。
https://stackoverflow.com/questions/17580935
复制相似问题