Hi everyone, I am not sure if there is an existing solution to this, but I want my S4 objects inside a list matrix showing correctly. Currently it shows as: R> str(lst[[1]]) Formal class 'Basic' [package "symengine"] with 1 slot ..@ ptr:<externalptr> R> matrix(lst, 2) [,1] [,2] [,3] [,4] [,5] [1,] ? ? ? ? ? [2,] ? ? ? ? ? Is it possible that the print method for matrix can call some type of generic such as `as.character` or `format` when it encounters such cases? Or is there any other place that I can override without introducing a new S3 class? Thanks! Jialin Ma
> I am not sure if there is an existing solution to this, but I want my S4 > objects inside a list matrix showing correctly. > R> matrix(lst, 2) > [,1] [,2] [,3] [,4] [,5] > [1,] ? ? ? ? ? > [2,] ? ? ? ? ? > Is it possible that the print method for matrix can call some type ofgeneric> such as `as.character` or `format` when it encounters such cases?I had some difficulty understanding this question. So, I'm going to paraphrase it. R, to the best of my knowledge, does not support object arrays, as such. (Or if it does, I've certainly missed the memo on this one). The closest option, is to create an (S3) list of (S3 or S4) objects. This is sufficient in the one-dimension case. However, to provide functionality of two- or three-dimensional object arrays, one can create a matrix (or array) from the list. It's desirable to print such matrices and arrays. This is possible but the output contains an array of question marks, which isn't helpful. Would it be possible for the print method for both matrices and arrays (conditional on having a list type), call the format method for each object, possibly creating a character matrix? Presumably there are other approaches, but the main thing is that the output is useful and it's easy for R users to control the way objects (in matrices and arrays) are printed.> Or is there any other place that I can override without introducing a newS3 class? In theory, the simplest approach is to redefine the print method for matrices. However, that would be unacceptable in a CRAN package, probably... So, unless R Core change the print method, you may have to create a matrix subclass. Abs [[alternative HTML version deleted]]
Hi Abby, Thanks a lot for your paraphrasing and your suggestion! The problem of wrapping the list into a S3/S4 object, i.e. subclassing array or matrix, is that one also has to define a bunch of methods for subsetting, joining, etc, in order to make it behave like a list array. The reason is that most R functions for subsetting, joining, etc. do not preserve class attributes of the input, which is possibly by design. It is not desirable if a simple matrix subsetting will remove the class attributes of the object. There are also many other common functions that are meant to drop the attributes of the input, e.g. lapply, apply -- they are not generics and it is not wise to override them. Overall, introducing a new S3/S4 class often brings some extra price in order to maintain the correct behavior, which is why I tend to avoid them unless it is necessary. Best regards, Jialin On Sunday, July 7, 2019 4:43:59 PM PDT Abby Spurdle wrote:> contains an array of question marks, which > isn't helpful. > > Would it be possible for the print method for both matrices and arrays > (conditional on having a list type), call the format method for each > object, possibly creating a character matrix? > Presumably there are other approaches, but the main thing is that the > output is useful and it's easy for R users to control the way objects (in > matrices and arrays) are printed. > > > Or is there any other place that I can override without introducing a new > > S3 class? > > In theory, the simplest approach is to redefine the print method for > matrices. > However, that would be unacceptable in a CRAN package, probably... > > So, unless R Core change the print method, you may have to create a matrix > subclass.
A generic for displaying an object in a cell would be a reasonable solution for this particular problem. However, as soon as you start treating these objects as data (like putting them into a matrix), you're likely going to want vectorized operations over them, which means formalized vector and matrix classes. S4Vectors in Bioconductor facilitates this for vectors, but not for higher-order arrays. Michael On Sun, Jul 7, 2019 at 4:44 PM Abby Spurdle <spurdle.a at gmail.com> wrote:> > > I am not sure if there is an existing solution to this, but I want my S4 > > objects inside a list matrix showing correctly. > > R> matrix(lst, 2) > > [,1] [,2] [,3] [,4] [,5] > > [1,] ? ? ? ? ? > > [2,] ? ? ? ? ? > > Is it possible that the print method for matrix can call some type of > generic > > such as `as.character` or `format` when it encounters such cases? > > I had some difficulty understanding this question. > So, I'm going to paraphrase it. > > R, to the best of my knowledge, does not support object arrays, as such. > (Or if it does, I've certainly missed the memo on this one). > > The closest option, is to create an (S3) list of (S3 or S4) objects. > This is sufficient in the one-dimension case. > However, to provide functionality of two- or three-dimensional object > arrays, one can create a matrix (or array) from the list. > > It's desirable to print such matrices and arrays. > This is possible but the output contains an array of question marks, which > isn't helpful. > > Would it be possible for the print method for both matrices and arrays > (conditional on having a list type), call the format method for each > object, possibly creating a character matrix? > Presumably there are other approaches, but the main thing is that the > output is useful and it's easy for R users to control the way objects (in > matrices and arrays) are printed. > > > Or is there any other place that I can override without introducing a new > S3 class? > > In theory, the simplest approach is to redefine the print method for > matrices. > However, that would be unacceptable in a CRAN package, probably... > > So, unless R Core change the print method, you may have to create a matrix > subclass. > > > Abs > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Michael Lawrence Scientist, Bioinformatics and Computational Biology Genentech, A Member of the Roche Group Office +1 (650) 225-7760 michafla at gene.com Join Genentech on LinkedIn | Twitter | Facebook | Instagram | YouTube