-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify how to extend tibbles #275
Comments
Also connected to making print methods more extensible. |
We should verify that these changes are helpful by looking into how it affects googledrive. |
For what it's worth, I rewrote most of For example, I rely on having a date/datetime index column in the tibble. If that column is lost, there is no reason to be a Also, I'm sure you're aware but we would need a way to extend |
Thanks. I wonder if you could implement your Verbs that operate on grouped data also should call I'll postpone writing this vignette until after CRAN release of tibble + pillar, because otherwise we'll need to wait for sloop as well. |
I think you're right that I could put the checks inside My main question on grouped data is a bit convoluted, but bare with me. No rush on needing an answer. Assume I have created a Now, I want to additionally group the data by a column. I assume the inheritance should look like: The way I do this, attempting to follow the new Advanced R material, is as follows:
group_by.tbl_time <- function(.data, ..., add = FALSE) {
# Normal group then pass to grouped_tbl_time helper
quos <- rlang::quos(...)
.data_grouped <- dplyr::group_by(as_tibble(.data), !!! quos, add = add)
grouped_tbl_time(.data_grouped, !! get_index_quo(.data))
}
Is that right? I don't see any other way to get the inheritance correct unless maybe I have |
I wonder if you can get away with simply removing the #' @export
reconstruct.tbl_time <- function(new, old) {
if ("grouped_df" %in% class(new)) class(new) <- c("grouped_tbl_time", class(new))
...
new
} On a side note, it seems that you can simply pass along |
I think your suggestion removes a lot of unnecessary work on my end. I really appreciate you taking a look at it. I saw your latest PR, removing new_tbl_time <- function(x, index_quo, index_time_zone, ..., subclass = NULL) {
tibble::new_tibble(
x,
index_quo = index_quo,
index_time_zone = index_time_zone,
subclass = c(subclass, "tbl_time")
)
} Where I guess the alternative is for me to assign the attributes to Support from Advanced R (not trying to take this as gospel by bringing it up so often, its just what ive been going off and it seems to make sense to me). "To allow subclasses, the parent constructor needs to have It provides the below example, where the new new_my_class <- function(x, y, ..., subclass = NULL) {
stopifnot(is.numeric(x))
stopifnot(is.logical(y))
structure(
x,
y = y,
...,
class = c(subclass, "my_class")
)
}
new_subclass <- function(x, y, z) {
stopifnot(is.character(z))
new_my_class(x, y, z, subclass = "subclass")
} |
Fine with adv-r as the reference ;-) . I looked in a previous subsection, https://adv-r.hadley.nz/s3.html#constructors, and I haven't found the ellipsis there. It's easy to add back, though. |
I'm actually surprised he doesn't do it there. In theory new_Date <- function(x, ..., subclass = NULL) {
sloop::new_s3_dbl(x, ..., class = c(subclass, "Date"))
} rather than just: new_Date <- function(x) {
sloop::new_s3_dbl(x, class = "Date")
} My guess is that he is just introducing the topic for the first time there in 13.2.1 of Advanced R, and it might be overwhelming to show everything (inheritance) all at once. Which is why the |
I guess a forward reference would help impatient and superficial readers like myself, but that's a tough balance. |
If we are being really picky, the tibble specific attributes go before the function (x, ..., nrow = NULL, subclass = NULL)
# VS
function (x, nrow = NULL, ..., subclass = NULL) Agreed. Effective teaching is hard work. |
This was deliberate, users shouldn't be calling |
@krlmlr As someone who has subclassed tibble using DIY methods, what's the status here? Would it now be welcome for people to test out the more official ways of doing this? Or not quite yet? |
We now have |
I look forward to trying these extensions out. I have to say that current behavior of dropping attributes (and subclasses for that matter) seems inconsistent across R. For example |
Also need a statement about (non-)support of S4 classes. |
I think this should wait until the next release so we can fully internalise the lessons from vctrs. |
We should move the existing "Extending" vignette to {vctrs}, and adapt to the new infrastructure. |
Topics:
|
Are we waiting for #890 here? |
By following the principles defined at http://adv-r.hadley.nz/s3.html#inheritance
Provide a constructor that can also be used to create subclassed objects
Provide a reconstruct method
Call
S3::reconstruct()
in all methods that return a tibbleThis supersedes #155, #211, #218.
The text was updated successfully, but these errors were encountered: