Learn R Programming

dtlg (version 0.0.2)

dt_copy_semantics: Get or set data.table copy semantics

Description

These functions control how maybe_copy_dt() decides whether to return a data.table by reference (in place) or by value (as a deep copy).

Usage

dt_copy_semantics()

set_dt_copy_semantics(dt_copy_semantics = c("reference", "value"))

Value

  • dt_copy_semantics() returns the current semantics as a string, "reference" or "value".

  • set_dt_copy_semantics() sets the semantics, returning the previous semantics invisibly.

Arguments

dt_copy_semantics

Character string. Either "reference" or "value".

Details

The copy semantics are stored in the global option dtlg_dt_copy_semantics. The option can take two values:

  • "reference" (default): inputs are treated with reference semantics.

    • If the input is already a data.table, it is returned unchanged and aliases are preserved.

    • If the input is a data.frame, it is converted to a data.table in place via data.table::setDT(), mutating the caller’s object.

  • "value": inputs are treated with value semantics.

    • The input is converted to a data.table (if necessary) and a deep copy is returned, leaving the original unchanged.

See Also

maybe_copy_dt()

Examples

Run this code
# Get current semantics (defaults to "reference")
dt_copy_semantics()

# Switch to value semantics
old <- set_dt_copy_semantics("value")
dt_copy_semantics()

# Restore previous semantics
set_dt_copy_semantics(old)

Run the code above in your browser using DataLab