mara
January 29, 2018, 1:08pm
3
Re. attributes: this is something that's known and being worked on right now. There are a few issues /threads in the dplyr github repo that you can peruse for more detail. And there's ~related discussion in this thread here:
dat1 <- mtcars
dat2 <- mtcars
attr(dat2, "description") <- "# MTCARS\nData description ..."
all.equal(dat1, dat2) ## not TRUE
Returns:
[1] "Attributes: < Names: 1 string mismatch >"
[2] "Attributes: < Length mismatch: comparison on first 2 components >"
[3] "Attributes: < Component 2: Lengths (32, 1) differ (string compare on first 1) >"
[4] "Attributes: < Component 2: 1 string mismatch >"
dat1 <- tibble::as_tibble(mtcars)
dat2 <- tibble::as_tibble(mtcars)
attr(dat2, "description") <- "# MTCARS\nData description ..."
all.equal(dat1, dat2) ## TRUE, ignores attribute
dplyr::all_equal(dat1, dat2) ## TRUE, ignores attribute
Bo…
Re. classes, I don't have a general answer, but the discussion between Hadley and Kiril re. S4 (linked to below) might be useful:
opened 03:46PM - 27 Oct 17 UTC
closed 02:30PM - 11 Mar 20 UTC
vctrs ↗️
Would be great to have formal tested support. Some initial explanations below:
…
```R
library(methods)
# install_github("tidyverse/tibble")
library(tibble)
# Silly minimal vector example --------------------------------------------
# Shouldn't need to contain a vector class, but tibble currently
# checks - need someway to determine if we have a "vector" S4
# class. Maybe just see if it has [ and length methods?
.rando <- setClass("rando", contains = "integer", slots = list(n = "integer"))
rando <- function(n) {
.rando(n = as.integer(n))
}
setMethod("[", "rando", function(x, i, j, ..., drop = TRUE) {
if (is.logical(x)) {
new_n <- sum(i, na.rm = TRUE)
} else {
new_n <- length(i)
}
rando(new_n)
})
setMethod("length", "rando", function(x) x@n)
setMethod("show", "rando", function(object) {
print(runif(object@n))
invisible(object)
})
x <- rando(10)
x
x[1]
length(x)
# Can we put in a tibble? -------------------------------------------------
tb <- tibble(x = x)
tb$x
tb[1:5, ]$x
# doesn't print right
tb
# from colformat code I thought this would be sufficient
# but I'm missing something from a very quick glance
setMethod("as.character", "rando", function(x, ...) {
format(runif(x@n), digits = 2)
})
tb
```
3 Likes