Skip to content

Allow explicitly setting .datatable.aware <- FALSE to use base R data.frame syntax #5654

@dvg-p4

Description

@dvg-p4

Feature request: Allow setting .datatable.aware <- FALSE to explicitly use base R data.frame syntax in a datatable-aware package

Motivation

I'm coding a package that uses data.table features in some places but not in others. We import data.table in NAMESPACE in order to have access to := etc. for the parts of our code that do use the full features of data.table. We would like to be able to write user-facing functions that take either a base data.frame or a data.table as an argument, and use them as input with some simple selecting and filtering.

Problem

data.table indexing semantics are by design different from data.frame semantics. So in many simple cases, it's difficult or impossible to write a call that will index both a data.frame and a data.table the same way.

As this vignette explains, you can set .datatable.aware = TRUE in a package to use data.table indexing semantics without importing data.table in NAMESPACE. However, as far as I can tell, there's no way to do the reverse--disable data.table semantics and go back to base R data.frame indexing for a certain function call.

Partial workarounds

A couple imperfect strategies I've considered to get around this:

  • as.data.table() / as.data.frame(): Creates an unnecessary copy of the data--slow on big tables and antithetical to the core point of data.table
  • setDT() / setDF(): Does not behave consistently/stably when called on a function argument, see setDT and setkeyv inside function #5618
  • subset(): Lets me do programmatic column selection and subsets, but verbose and not actually consistent in some edge cases

Suggestion:

Allow setting .datatable.aware <- FALSE to tell data.table that it should use base R data.frame indexing semantics in an otherwise data.table-aware package. This does not work currently since data.table:::cedta() does a big OR of this variable with the other conditions. But it seems like adding an if (isFALSE(ns$.datatable.aware)) return FALSE check before the big OR might do the trick.

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