[R-lang] Problem with standard generic methods in Matrix package

sloiseau at limsi.fr sloiseau at limsi.fr
Mon Aug 24 23:36:34 PDT 2009


Hello,



I'm puzzled by a problem with call to diag() and rowSums() on object of

class "dgtMatrix", created by sparseMatrix() or spMatrix().



I use Matrix 0.999375-30.



The weird thing is that I don't encounter any problem when I enter the

function on the R prompt, or source()-ing a script file; I encounter

problems when the code is in a package, installed with R CMD INSTALL, and

loaded with library().



I have tried to reduce the package to the bare minimum in order to

discover the problem, but it continue even when reduced up to a single

method, no dependency other than Matrix. The method is defined as:



---

getAdjacentAsSparseMatrix <- function() {



    adjacents <- sparseMatrix(i=1:10, j=1:10, x=1:10)



     print(class(adjacents));



     diagonal <- as.vector(diag(adjacents));

     if (any(diagonal != 0)) {

       diagonal[diagonal > 1] <- 1;

       diag(adjacents) <- diagonal;

     }



    return(adjacents);

}

---



And the Depends field in ./DESCRIPTION :



---

Depends: R (>= 2.5.0), Matrix

---





I have see two outputs:



[1] "dgTMatrix"

Erreur dans y[1L + 0L:(m - 1L) * (n + 1L)] <- x :

    types (de S4 a double) incompatibles dans l'ajustement d'affectation de

type

> traceback()

3: diag(adjacents)

2: as.vector(diag(adjacents))

1: getAdjacentAsSparseMatrix(white3)



The same thing happens with the method rowSums(), colnames() and
rownames(). In brief, it looks as if the generic functions were called,
and not the overloaded one. I can't figure out how to fix that, and will
be interested in any pointers.



Best,

Sylvain








More information about the R-lang mailing list