简体   繁体   中英

Using “Matrix” package methods in a custom R package

Following code is a function in a custom R package.

#' @import Matrix
#' @export
asdf <- function(){
    u <- Matrix(0,5,5)
    rowSums(u)
}

When I load the package and execute it, I get the following output.

> library(devtools); document(clean=TRUE); load_all()
Loading required package: roxygen2
Updating gm documentation
Loading gm
Loading required namespace: Matrix
Loading required package: Matrix
Writing gm.generate.Rd
Loading gm
> asdf
function(){
    u <- Matrix(0,5,5)
    rowSums(u)
}
<environment: namespace:gm>
>
> asdf()
Error in rowSums(u) (from tmp.R#5) : 'x' must be an array of at least two dimensions

So, even though the Matrix package is loaded, rowSums in the Matrix package is not getting dispatched even though Matrix package is imported in the NAMESPACE :

export(asdf)
import(Matrix)

In the global environment, the Matrix package is indeed loaded, and rowSums function is available.

> showMethods(rowSums)
Function: rowSums (package base)
x="ANY"
x="CsparseMatrix"
x="ddenseMatrix"
x="denseMatrix"
x="dgCMatrix"
x="dgeMatrix"
x="diagonalMatrix"
x="dsCMatrix"
    (inherited from: x="CsparseMatrix")
x="igCMatrix"
x="indMatrix"
x="lgCMatrix"
x="ngCMatrix"
x="RsparseMatrix"
x="TsparseMatrix"

However, if I define the same function in the global environment, everything works properly:

> qwer <- function(){
    u <- Matrix(0,5,5)
    rowSums(u)
}

qwer <- function(){
+     u <- Matrix(0,5,5)
+     rowSums(u)
+ }
> qwer()
[1] 0 0 0 0 0

I am puzzled by this. What am I doing wrong?

================================================================

In NAMESPACE file:

Package: hola
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2014-01-03
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?

In DESCRIPTION file:

export(asdf)
import(Matrix)
importFrom(Matrix,Matrix)
importFrom(Matrix,rowSums)

In R/asdf.R file:

#' @import Matrix
#' @importFrom Matrix Matrix
#' @importFrom Matrix rowSums
#' @export
asdf <- function(){
    u <- Matrix(0,5,5)
    rowSums(u)
}

You should read 1.5.6 Namespaces with S4 classes and methods

Add this to your NAMESPACE file:

import(Matrix)

or better:

importFrom(Matrix, rowSums)

Also, Imports: Matrix in DESCRIPTION file is needed.

Bare minimum package is as follows:


DESCRIPTION file:

Package: hola
Type: Package
Title: What the package does (short line)
    Version: 1.0
    Date: 2014-01-03
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports:
    Matrix

NAMESPACE file:

export(asdf)
importFrom(Matrix,Matrix)
importFrom(Matrix,rowSums)

R/asdf.R file:

#' @importFrom Matrix Matrix
#' @importFrom Matrix rowSums
#' @export
asdf <- function(){
    u <- Matrix(0,5,5)
    rowSums(u)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM