posit-dev / positron

Positron, a next-generation data science IDE
Other
2.54k stars 79 forks source link

vctrs records in R environment pane #748

Open romainfrancois opened 1 year ago

romainfrancois commented 1 year ago

Follow up to this comment: https://github.com/rstudio/positron/pull/559#discussion_r1195182114

> rcd <- new_rcrd(list(x = 1:10, y = letters[1:10]),  class = "mylist")
> length(rcd)
[1] 10

This creates a record of size 10 with 2 fields. But the environment pane only shows the record as a boring list:

image

even if it has a format() method:

> format.mylist <- function(x, ...) paste("(", field(x, "x"), ", ", field(x, "y"), ")", sep = "")
> rcd <- new_rcrd(list(x = 1:10, y = letters[1:10]),  class = "mylist")
> rcd
<mylist[10]>
 [1] (1, a)  (2, b)  (3, c)  (4, d)  (5, e)  (6, f)  (7, g)  (8, h)  (9, i) 
[10] (10, j)

Should it look more like format(rcd):

fmt <- format(rcd)
image
DavisVaughan commented 1 year ago

Notably there are two issues here:

We may be able to bypass the length() call I mentioned in the first bullet by calling format() and taking the length of that instead, because that should be just a simple character vector we can reliably call Rf_xlength() on.

romainfrancois commented 1 year ago

View(rcd) in rstudio gives this (whatever that is):

image

and environment pane (list) gives:

image

We can top this.

DavisVaughan commented 1 year ago

I think part of that is that you need registerS3Method(). It doesn't fix calling View() directly on the rcd, but if you put it in a data frame as a column then registerS3Method() is required for the format() method to be found correctly.

library(vctrs)
rcd <- vctrs::new_rcrd(list(x = 1:10, y = letters[1:10]),  class = "mylist")
format.mylist <- function(x, ...) paste("(", field(x, "x"), ", ", field(x, "y"), ")", sep = "")
registerS3method("format", "mylist", format.mylist)
View(tibble::tibble(rcd = rcd))
Screen Shot 2023-06-16 at 1 20 16 PM