[Rd] M[cbind()] <- assignment with Matrix object loses attributes

Abby Spurdle @purd|e@@ @end|ng |rom gm@||@com
Sat Aug 22 09:36:05 CEST 2020


Hi Ben,

I had some problems reproducing this.
As far as I can tell *all* indexed assignments drop attributes.
(Maybe we have different versions).

I'm not an expert on S4, but I'm unenthusiastic about mixing slot (S4)
semantics with attribute (S3) semantics.
And str() excludes attributes, but attributes() includes slots.
Highlighting the problems here...

I think R should generate an error or a warning, if a user tries to
assign attributes to S4 objects.

In saying that, mixing OO design with numerical linear algebra is a gold mine...


On Tue, Aug 11, 2020 at 1:23 PM Ben Bolker <bbolker using gmail.com> wrote:
>
>    Does this constitute a bug, or is there something I'm missing?
> assigning sub-elements of a sparse Matrix via M[X]<-..., where X is a
> 2-column matrix, appears to drop user-assigned attributes. I dug around
> in the R code for Matrix trying to find the relevant machinery but my
> brain started to hurt too badly ...
>
>     Will submit this as a bug if it seems warranted.
>
> library(Matrix)
> m1 <- matrix(1:9,3,3)
> m1 <- Matrix(m1)
> attr(m1,"junk") <- 12
> stopifnot(isTRUE(attr(m1,"junk")==12))  ## OK
> m1[cbind(1:2,2:3)] <- 1
> stopifnot(isTRUE(attr(m1,"junk")==12)) ## not OK
> attr(m1,"junk") ## NULL
>
>
> ## note I have to use the ugly stopifnot(isTRUE(...)) because a missing
> attribute returns NULL, an assignment to NULL returns NULL, and
> stopifnot(NULL) doesn't stop ...
>
>
>     cheers
>
>       Ben Bolker
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list