Return a data.table by reference or by value
maybe_copy_dt.Rd
maybe_copy_dt()
returns its input as a data.table
, with behaviour
controlled by the global copy semantics option dt_copy_semantics()
.
Value
A data.table
. Whether the return value aliases the input depends on
the semantics:
"reference"
: input is mutated in place, aliasing guaranteed ifx
is already adata.table
."value"
: a fresh copy is returned, independent of the input.
Details
If the semantics are
"reference"
(default):If
x
is already adata.table
, it is returned unchanged. Aliasing holds, so mutations with:=
will affect both input and output.If
x
is adata.frame
, it is converted to adata.table
in place viadata.table::setDT()
, mutating the caller’s object. The returned object is adata.table
with the same contents. For efficiency, the column vectors are reused without a deep copy.
If the semantics are
"value"
:x
is converted to adata.table
(if necessary) and a deep copy is returned. Mutating the result does not affect the input.
Examples
# Default: reference semantics
df <- data.frame(a = 1:3)
out <- maybe_copy_dt(df)
data.table::is.data.table(df) # TRUE, converted in place
#> [1] TRUE
# Switch to value semantics
old <- set_dt_copy_semantics("value")
dt <- data.table::data.table(a = 1:3)
out2 <- maybe_copy_dt(dt)
out2[, b := 99L]
#> a b
#> <int> <int>
#> 1: 1 99
#> 2: 2 99
#> 3: 3 99
"b" %in% names(dt) # FALSE, original unchanged
#> [1] FALSE
# Restore previous semantics
set_dt_copy_semantics(old)