Skip to content

Conversation

juliasilge
Copy link
Contributor

Addresses posit-dev/positron#8053

With this PR, you can see Surv objects in the Data Explorer:

Screenshot 2025-09-03 at 9 29 43 PM

And we no longer get this error I point out here, when you click to expand a Surv object in the Variables Pane:

Screenshot 2025-09-03 at 8 38 45 PM

Good news on both fronts! 🎉

This manhandling of dim() was introduced for posit-dev/positron#1862, also motivated by Surv objects. However, I think we must have updated other pieces that handle this, because those original examples are fine even after I remove this code:

Screenshot 2025-09-03 at 9 32 18 PM

No panics! More good news 🎉

There is still a little bit of bad news. Now when you click to expand a vector of Surv objects in the Variables pane, there is a new error:

Screenshot 2025-09-03 at 9 35 03 PM

Try with this code:

library(tidyverse)
library(survival)

res <- modeldata::cat_adoption |>
    slice_sample(n = 5) |>
    mutate(
        event_time = Surv(time, event),
        .keep = "unused",
        .before = everything()
    )

And the error message has:

Error expanding variable item: Failed to process positron.variables request: Error evaluating 'harp_subset_vec(structure(c(76, 95, 28, 104, 18, 1, 1, 1, 1, 1), dim = c(5L, 2L), dimnames = list(NULL, c("time", "status" )), type = "right", class = "Surv"), 6:10)': subscript out of bounds R backtrace: 0. harp_subset_vec(structure(c(76, 95, 28, 104, 18, 1, 1, 1, 1, 1), dim = c(5L, 2L), dimnames = list(NULL, c("time", "status" )), type = "right", class = "Surv"), 6:10)

Notice that it is trying to subset 6:10 here, which doesn't exist. I suspect this is related to #646, and how we changed to subsetting before formatting.

@dfalbel could you take a look at this and see if you have suggested changes we could make here to make this work in the Variables pane? It would be nice to just get it all fixed and write some tests to make sure it does not regress.

@@ -65,6 +65,7 @@ jobs:
tibble
haven
R6
survival
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added survival here, despite it being (because it is?) such a problem child so we can write tests for this especially weird/bad case.

Copy link
Contributor

@dfalbel dfalbel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it's failing is that we are nor dispatching on length, so we are seeing length = 10 when formatting.
We should likely dispatch on the S3 implementation of length here:

let length = r_length(self.vector.sexp);
let n = n.min(length as usize);

and

let length = r_length(self.vector.sexp);

Another possibibily is to implement a custom display method for Surv objects:

```r
ark_positron_variable_display_value.foo <- function(x, ..., width = NULL) {
toString(x, width)
}
```
These methods don't need to be exported in the `NAMESPACE` file. Ark automatically finds and registers them when the package is loaded.
You can also register a method outside an R package using `.ps.register_ark_method()`, similar to `base::.S3method()`:
```r
.ps.register_ark_method("ark_positron_variable_display_value", "foo", function(x, width) {
toString(x, width)
})
```

Although, I think it should be fine to dispatch on length(). We may find some other commonly used S3 object that doesn't correctly report it's length and could cause different problems.

@juliasilge
Copy link
Contributor Author

Thanks, @dfalbel!

  • In 539b8ae I am showing what I tried using the .ark.register_method() approach, but adding these keeps R from starting up ("R 4.5.0 failed to start up (exit code -1) The process exited abnormally (signal: 6 (SIGABRT))")
  • In 2a132dd I am showing how I started to sketch out dispatching on length() but I could not get that to work because a Surv object is a matrix; I could not get the Variables pane handling for matrices untangled from how/when we format vs. subset.

I tend to think it might be better to use .ark.register_method() and make special handling for Surv objects? But open to what you think.

@dfalbel dfalbel force-pushed the bugfix/s3-vec-formatting branch from 2161a4a to 3c6271e Compare September 5, 2025 17:44
@juliasilge juliasilge marked this pull request as ready for review September 5, 2025 19:01
@juliasilge
Copy link
Contributor Author

Amazing, @dfalbel, thank you so much!

Release Notes

New Features

  • N/A

Bug Fixes

  • R: Fixed how Surv objects are represented and formatted in the Data Explorer and Variables pane

QA Notes

For code like this:

library(tidyverse)
library(survival)

res <- modeldata::cat_adoption |>
    slice_sample(n = 15) |>
    mutate(
        event_time = Surv(time, event),
        .keep = "unused",
        .before = everything()
    )

You can:

  • run View(res) and see the Surv column in the Data Explorer
  • click to expand the res object in the Variables pane, including expanding the event_time column (which is like a matrix nested inside the dataframe)
Screenshot 2025-09-05 at 1 07 15 PM

@juliasilge juliasilge requested a review from dfalbel September 5, 2025 19:07
@juliasilge juliasilge merged commit fbbb713 into main Sep 5, 2025
6 checks passed
@juliasilge juliasilge deleted the bugfix/s3-vec-formatting branch September 5, 2025 19:14
@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants