Skip to content

Could as_tibble.data.frame() be stricter? #1556

@TimTaylor

Description

@TimTaylor

Currently as_tibble.data.frame() treats all extended data frames (class = <mydfclass, data.frame>) as if they were normal data frames (class = <data.frame>) and in turn ignores any as.data.frame() method that the object has available. A consequence of this manifested in #1555, and the follow up report in Rdatatable/data.table#5698, where additional attributes (only of use to {data.table}) were not removed on coercion.

In Rdatatable/data.table#5698, Jan proposed the following small addition as a possible solution:

as_tibble.data.frame = function(x, ...) {
  if (!identical(class(x), "data.frame")) return(as_tibble(as.data.frame(x)))
  ... # continue old as_tibble.data.frame body
}

This benefits not just {data.table} but any package that provides an extended object class. The onus is still on those packages to provide an as.data.frame() method but hopefully this is something they are already doing.

Happy to send a PR if receptive?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions