tidyfun / tf

S3 classes and methods for tidy functional data
https://tidyfun.github.io/tf/
GNU Affero General Public License v3.0
5 stars 2 forks source link

Simpler `as.data.frame()` #95

Closed m-muecke closed 2 months ago

m-muecke commented 3 months ago

We can make the as.data.frame() for the unnest = FALSE case simpler and quite a bit faster with either using NextMethod() (~9µs, should be dispatched to list method) or vctrs::new_data_frame() or vctrs::data_frame() most packages building custom vctrs types seem to use vctrs::new_data_frame():

x <- tf::tf_rgp(100L)
bench::mark(
  as.data.frame(x),
  vctrs::new_data_frame(list(x), names = deparse(substitute(x))),
  vctrs::new_data_frame(list(x), n = length(x), names = deparse(substitute(x))),
  vctrs::new_data_frame(list(x), n = vctrs::vec_size(x), names = deparse(substitute(x))),
  vctrs::data_frame(x = x)
)
#> # A tibble: 5 × 6
#>   expression                            min  median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                        <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl>
#> 1 as.data.frame(x)                  48.75µs 53.46µs    18315.   89.27KB     21.2
#> 2 vctrs::new_data_frame(list(x), n…  5.49µs  6.03µs   156012.    6.88KB     15.6
#> 3 vctrs::new_data_frame(list(x), n…  4.63µs  5.12µs   180433.        0B     36.1
#> 4 vctrs::new_data_frame(list(x), n…  6.11µs  6.85µs   134780.        0B     27.0
#> 5 vctrs::data_frame(x = x)          13.16µs 14.19µs    66846.   18.18KB     20.1

Created on 2024-06-15 with reprex v2.1.0

Then it would something like the following for the unnest = FALSE case or substituting with equivalent vctrs code from above instead of the NextMethod call:

as.data.frame.tf <- function(x, row.names = NULL, optional = FALSE, unnest = FALSE, ...) {
  if (unnest) return(tf_2_df(x))
  NextMethod()
}
fabian-s commented 2 months ago

thx, good idea!