Skip to content

Commit 6047327

Browse files
committed
Improve performance and docs of as_tibble.epi_df
perf: Use `new_tibble()` in the common no-`...` case. doc: don't @inheritParams `...` when we wanted @inheritDotParams; but favor just mentioning forwarding over the latter since no `...` is the common case. doc: mention other-attribute dropping.
1 parent b994304 commit 6047327

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,4 @@ importFrom(vctrs,vec_cast)
244244
importFrom(vctrs,vec_data)
245245
importFrom(vctrs,vec_duplicate_any)
246246
importFrom(vctrs,vec_equal)
247+
importFrom(vctrs,vec_size)

R/methods-epi_df.R

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
#' Convert to tibble
22
#'
3-
#' Converts an `epi_df` object into a tibble, dropping metadata and any
4-
#' grouping.
3+
#' Converts an `epi_df` object into a tibble, dropping metadata, any
4+
#' grouping, and any unrelated classes and attributes.
55
#'
66
#' Advanced: if you are working with a third-party package that uses
77
#' `as_tibble()` on `epi_df`s but you actually want them to remain `epi_df`s,
88
#' use `attr(your_epi_df, "decay_to_tibble") <- FALSE` beforehand.
99
#'
1010
#' @param x an `epi_df`
11-
#' @inheritParams tibble::as_tibble
12-
#' @importFrom tibble as_tibble
11+
#' @param ... if present, forwarded to [`tibble::as_tibble`]
12+
#' @importFrom tibble as_tibble new_tibble
13+
#' @importFrom rlang dots_n
14+
#' @importFrom vctrs vec_data vec_size
1315
#' @export
1416
as_tibble.epi_df <- function(x, ...) {
1517
# Note that some versions of `tsibble` overwrite `as_tibble.grouped_df`, which
16-
# also impacts grouped `epi_df`s don't rely on `NextMethod()`. Destructure
17-
# first instead.
18-
destructured <- tibble::as_tibble(vctrs::vec_data(x), ...)
18+
# also impacts grouped `epi_df`s, so don't rely on `NextMethod()`. Destructure
19+
# and redispatch instead.
20+
destructured <- vec_data(x) # -> data.frame, dropping extra attrs
21+
tbl <- if (dots_n(...) == 0 &&
22+
is.null(pkgconfig::get_config("tibble::rownames"))) {
23+
# perf: new_tibble instead of as_tibble.data.frame which performs
24+
# extra checks whose defaults should be redundant here:
25+
new_tibble(destructured)
26+
# (^ We don't need to provide nrow= as we have >0 columns.)
27+
} else {
28+
as_tibble(destructured, ...)
29+
}
1930
if (attr(x, "decay_to_tibble") %||% TRUE) {
20-
return(destructured)
31+
return(tbl)
2132
} else {
2233
# We specially requested via attr not to decay epi_df-ness but to drop any
23-
# grouping.
24-
reclass(destructured, attr(x, "metadata"))
34+
# grouping. (Miscellaneous attrs are also dropped.)
35+
reclass(tbl, attr(x, "metadata"))
2536
}
2637
}
2738

man/as_tibble.epi_df.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)