Closed mattwarkentin closed 3 years ago
This is how to create a print method: https://r6.r-lib.org/articles/Introduction.html#printing-r6-objects-to-the-screen
For str
you can create an "external" method I believe, i.e.
str.foo <- function(object, ...) {
...
}
I figured I would be able to avoid creating a print method if I wrote format methods. Shouldn't that work since format is what is called by print.R6
?
That works as well:
foo <- R6::R6Class("foo",
public = list(
format = function(...) "foobar"
)
)
foo$new()
#> foobar
Created on 2020-10-21 by the reprex package (v0.3.0)
Right. But I am not interested in changing how foo
is printed, I was hoping to change how members of foo
are printed when foo
itself is printed (e.g. how an array member is printed).
I might be missing something, but can't you just create a format method for your class that formats its members differently?
For sure. That's my fallback plan. Just create a new print
method for my class that formats encapsulated members differently than the default print.R6
method. I was just wondering whether I could just define new format methods for members that appear in my class and piggy-back on R6
's print method. I'm probably over-engineering the solution...
Alright after looking over R6:::print.R6
some more, the issue was with my mental model. format
isn't called on members of the R6
object, it is only called on the R6
object itself.
For posterity, I think the issue I was facing was beyond my control. In particular, one expects that RStudio uses str()
to figure out how to display an object in the Environment
pane, but it seems like RStudio has been setup to avoid calling str()
on objects of type environment
, such as a R6
object (see rstudio/rstudio#7147 and rstudio/rstudio#796).
So this is why I couldn't get my R6
class to show up differently in the pane. Maybe to be solved down the line.
Hi @wch,
I just have a couple (probably) simple questions about the
R6
print method and also how to modify how anR6
object is displayed in theEnvironment
pane in RStudio.For the first part, if I didn't feel like designing a whole custom
print
method for myR6
class, is there a way to modify how apublic
method or field is presented?I see that
print.R6
is:So for example, the default way an
array
is presented is:But I would like to customize it to show the dimensions, so I thought a new
S3
method forformat
would work, but it doesn't. Movingformat.array
to the global environment didn't help either.Can this be achieved without defining a custom print method (such as by writing a new
S3
method forformat
)?Second, it's my understanding that RStudio uses
str()
S3 methods to present objects in theEnvironment
pane. But no matter what I've tried I can't seem to get myR6
object to show up differently in the pane. Is this possible? If so, any suggestions on how to achieve this?