copy {cheapr} | R Documentation |
Copy R objects
Description
shallow_copy()
and deep_copy()
are just wrappers to the
R C API functions Rf_shallow_duplicate()
and Rf_duplicate()
respectively. semi_copy()
is something in between whereby it
fully copies the data but only shallow copies the attributes.
Usage
shallow_copy(x)
semi_copy(x)
deep_copy(x)
Arguments
x |
An object to shallow, semi, or deep copy. |
Details
Shallow duplicates are mainly useful for adding attributes to objects in-place as well assigning vectors to shallow copied lists in-place.
Deep copies are generally useful for ensuring an object is fully duplicated, including all attributes associated with it. Deep copies are generally expensive and should be used with care.
semi_copy()
deep copies everything except the attributes.
This is experimental but in theory should be much more efficient and
generally preferred to deep_copy()
.
To summarise:
-
shallow_copy
- Shallow copies data and attributes -
semi_copy
- Deep copies data and shallow copies attributes -
deep_copy
- Deep copies both data and attributes
It is recommended to use these functions only if you know what you are doing.
Value
A shallow, semi or deep copied R object.
Examples
library(cheapr)
library(bench)
df <- new_df(x = sample.int(10^4))
# Note the memory allocation
mark(shallow_copy(df), iterations = 1)
mark(deep_copy(df), iterations = 1)
# In both cases the address of df changes
address(df);address(shallow_copy(df));address(deep_copy(df))
# When shallow-copying attributes are not duplicated
address(attr(df, "names"));address(attr(shallow_copy(df), "names"))
# They are when deep-copying
address(attr(df, "names"));address(attr(deep_copy(df), "names"))
# Adding an attribute in place with and without shallow copy
invisible(attrs_add(df, key = TRUE, .set = TRUE))
attr(df, "key")
# Remove attribute in-place
invisible(attrs_add(df, key = NULL, .set = TRUE))
# With shallow copy
invisible(attrs_add(shallow_copy(df), key = TRUE, .set = TRUE))
# 'key' attr was only added to the shallow copy, and not the original df
attr(df, "key")