Open jessesadler opened 5 years ago
Is this something you'd be able to help with @romainfrancois?
I think this is very related to https://github.com/rstudio/rstudio/issues/10073
Possibly it isn't working because the format()
method is being registered in the global namespace? I think if you register it in a package then it works?
Yea see this works in the latest RStudio daily build (and i think in the released build)
library(vctrs)
library(zeallot)
new_rational <- function(n = integer(), d = integer()) {
vec_assert(n, ptype = integer())
vec_assert(d, ptype = integer())
new_rcrd(list(n = n, d = d), class = "vctrs_rational")
}
rational <- function(n, d) {
c(n, d) %<-% vec_cast_common(n, d, .to = integer())
c(n, d) %<-% vec_recycle_common(n, d)
new_rational(n, d)
}
registerS3method("format", "vctrs_rational", function(x, ...) {
n <- field(x, "n")
d <- field(x, "d")
out <- paste0(n, "/", d)
out[is.na(n) | is.na(d)] <- NA
out
})
x <- data.frame(x = rational(1, 1:10))
# Error
View(x)
So it has something to do with where format.vctrs_rational
is getting registered
But in general the problem is fixed since this mostly occurs in packages
When trying to to ." It works with
View()
a data frame with avctrs_rcrd
-type column in RStudio the attempt fails with the error: "Error: Can't castx
to
vctrs_vctr
class.I know that
View()
in Rstudio withvctrs_rcrd
types is all funky, but it is difficult to not be able to view a whole data frame because it has one such column.Example from rational class in S3 vignette:
Implementing
vec_cast.character.vctrs_rational
by using format method solves the error.I do not really understand why
View()
works with avctrs_vctr
-type column, since casting tocharacter()
does not automatically work. If it makes sense to solve the issue by implementing a cast fromvec_cast.character.vctrs_rational
maybe just a mention of this in the S3 vignette would be useful.