Dear All, in some functions of my package, I use the Matrix S4 class, as defined in the Matrix package. I don't want to depend on Matrix, however, because my package is perfectly fine without Matrix, most of the functionality does not need Matrix. Matrix is so included in the 'Suggests' line. I load Matrix via require(), from the functions that really need it. This mostly works fine, but I have an issue now that I cannot sort out. If I define a function like this in my package: f <- function() { require(Matrix) res <- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5) y <- rowSums(res) res / y } then calling it from the R prompt I get Error in rowSums(res) : 'x' must be an array of at least two dimensions which basically means that the rowSums() in the base package is called, not the S4 generic in the Matrix package. Why is that? Is there any way to work around this problem, without depending on Matrix? I am doing this on R 2.14.0, x86_64-apple-darwin9.8.0. Thank You, Best Regards, Gabor -- Gabor Csardi <csardi at rmki.kfki.hu>? ?? MTA KFKI RMKI
How about calling Matrix's namespace directly? Matrix:::rowSums() Michael On Tue, Nov 22, 2011 at 3:16 PM, G?bor Cs?rdi <csardi at rmki.kfki.hu> wrote:> Dear All, > > in some functions of my package, I use the Matrix S4 class, as defined > in the Matrix package. > > I don't want to depend on Matrix, however, because my package is > perfectly fine without Matrix, most of the functionality does not need > Matrix. Matrix is so included in the 'Suggests' line. > > I load Matrix via require(), from the functions that really need it. > This mostly works fine, but I have an issue now that I cannot sort > out. > > If I define a function like this in my package: > > f <- function() { > ?require(Matrix) > ?res <- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5) > ?y <- rowSums(res) > ?res / y > } > > then calling it from the R prompt I get > Error in rowSums(res) : 'x' must be an array of at least two dimensions > > which basically means that the rowSums() in the base package is > called, not the S4 generic in the Matrix package. Why is that? > Is there any way to work around this problem, without depending on Matrix? > > I am doing this on R 2.14.0, x86_64-apple-darwin9.8.0. > > Thank You, Best Regards, > Gabor > > -- > Gabor Csardi <csardi at rmki.kfki.hu>? ?? MTA KFKI RMKI > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi G?bor, You could import rowSums. This will not fully attach Matrix. I am not sure there is a really good solution for what you want to do. To fully use and validate your package, Matrix appears to be required. This is different from simply, for example, enhancing the Matrix package. You could just write the functions assuming matrix is there, make sure the examples are marked don't run, and tell users if they want to use them, they need to load Matrix first. I do this with OpenMx in my package. Cheers, Josh On Tue, Nov 22, 2011 at 12:16 PM, G?bor Cs?rdi <csardi at rmki.kfki.hu> wrote:> Dear All, > > in some functions of my package, I use the Matrix S4 class, as defined > in the Matrix package. > > I don't want to depend on Matrix, however, because my package is > perfectly fine without Matrix, most of the functionality does not need > Matrix. Matrix is so included in the 'Suggests' line. > > I load Matrix via require(), from the functions that really need it. > This mostly works fine, but I have an issue now that I cannot sort > out. > > If I define a function like this in my package: > > f <- function() { > ?require(Matrix) > ?res <- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5) > ?y <- rowSums(res) > ?res / y > } > > then calling it from the R prompt I get > Error in rowSums(res) : 'x' must be an array of at least two dimensions > > which basically means that the rowSums() in the base package is > called, not the S4 generic in the Matrix package. Why is that? > Is there any way to work around this problem, without depending on Matrix? > > I am doing this on R 2.14.0, x86_64-apple-darwin9.8.0. > > Thank You, Best Regards, > Gabor > > -- > Gabor Csardi <csardi at rmki.kfki.hu>? ?? MTA KFKI RMKI > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
On Tue, Nov 22, 2011 at 3:16 PM, G?bor Cs?rdi <csardi at rmki.kfki.hu> wrote:> Dear All, > > in some functions of my package, I use the Matrix S4 class, as defined > in the Matrix package. > > I don't want to depend on Matrix, however, because my package is > perfectly fine without Matrix, most of the functionality does not need > Matrix. Matrix is so included in the 'Suggests' line. > > I load Matrix via require(), from the functions that really need it. > This mostly works fine, but I have an issue now that I cannot sort > out. > > If I define a function like this in my package: > > f <- function() { > ?require(Matrix) > ?res <- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5) > ?y <- rowSums(res) > ?res / y > } > > then calling it from the R prompt I get > Error in rowSums(res) : 'x' must be an array of at least two dimensions > > which basically means that the rowSums() in the base package is > called, not the S4 generic in the Matrix package. Why is that? > Is there any way to work around this problem, without depending on Matrix? > > I am doing this on R 2.14.0, x86_64-apple-darwin9.8.0. >Try adding these three lines to the package: rowSums <- function(x, na.rm = FALSE, dims = 1L) UseMethod("rowSums") rowSums.dgCMatrix <- Matrix:::rowSums rowSums.default <- base::rowSums -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com