Learn R Programming

reproducible (version 1.2.16)

Copy: Recursive copying of nested environments, and other "hard to copy" objects

Description

When copying environments and all the objects contained within them, there are no copies made: it is a pass-by-reference operation. Sometimes, a deep copy is needed, and sometimes, this must be recursive (i.e., environments inside environments).

Usage

Copy(object, ...)

# S4 method for ANY Copy(object, ...)

# S4 method for SQLiteConnection Copy(object, ...)

# S4 method for data.table Copy(object, ...)

# S4 method for list Copy(object, ...)

# S4 method for refClass Copy(object, ...)

# S4 method for data.frame Copy(object, ...)

# S4 method for Raster Copy( object, filebackedDir, drv = getOption("reproducible.drv", RSQLite::SQLite()), conn = getOption("reproducible.conn", NULL), ... )

Value

The same object as object, but with pass-by-reference class elements "deep" copied. reproducible has methods for several classes.

Arguments

object

An R object (likely containing environments) or an environment.

...

Only used for custom Methods

filebackedDir

A directory to copy any files that are backing R objects, currently only valid for Raster classes. Defaults to .reproducibleTempPath(), which is unlikely to be very useful. Can be NULL, which means that the file will not be copied and could therefore cause a collision as the pre-copied object and post-copied object would have the same file backing them.

drv

an object that inherits from DBIDriver, or an existing DBIConnection object (in order to clone an existing connection).

conn

A DBIConnection object, as returned by dbConnect().

Author

Eliot McIntire

Details

To create a new Copy method for a class that needs its own method, try something like shown in example and put it in your package (or other R structure).

See Also

.robustDigest(), Filenames()

Examples

Run this code
e <- new.env()
e$abc <- letters
e$one <- 1L
e$lst <- list(W = 1:10, X = runif(10), Y = rnorm(10), Z = LETTERS[1:10])
ls(e)

# 'normal' copy
f <- e
ls(f)
f$one
f$one <- 2L
f$one
e$one ## uh oh, e has changed!

# deep copy
e$one <- 1L
g <- Copy(e)
ls(g)
g$one
g$one <- 3L
g$one
f$one
e$one
## To create a new deep copy method, use the following template
## setMethod("Copy", signature = "the class", # where = specify here if not in a package,
##           definition = function(object, filebackendDir, ...) {
##           # write deep copy code here
##           })

Run the code above in your browser using DataLab