g.data.save {g.data}R Documentation

Create and Maintain Delayed-Data Packages

Description

g.data.save reads the data in position "pos", and writes them as a delayed-data package to "dir". Data objects are initially created as promise objects, the promise being to load the data and return it the first time the item is requested.

g.data.attach attaches such a package, in position 2 by default.

g.data.get can be used to get a single piece of data from a package, without attaching the package. g.data.put puts a single item into an unattached package.

g.data.load is an internal subroutine used to create the promise objects.

Usage

 g.data.attach(dir, pos=2, warn=TRUE, readonly=FALSE)
 g.data.get(item, dir)
 g.data.put(item, value, dir)
 g.data.load(i, pkg)
 g.data.save(dir=searchpaths()[pos], obj=all.obj, pos=2, rm.obj,
             compress=FALSE)

Arguments

dir Directory (full pathname) of package. Data is stored in its /data subdirectory, as <item>.RData .
pos Search path position
warn Logical: warn user if directory being attached doesn't exist
readonly Logical: set an attribute on the package that will cause g.data.save to abort.
item Item to retrieve from an unattached package.
value Value for the data item being put with g.data.put.
i Item to evaluate.
pkg Package name where item is stored.
obj Objects to save; defaults to all objects in position "pos".
rm.obj Objects to remove, both in memory and on disk.
compress If TRUE, data is saved in compressed format.

Details

Data stored in a delayed-data package (DDP) are available on demand, but do not take up memory until requested. You attach a DDP with g.data.attach(), then read from it and assign to it in a manner similar to S-Plus, except that you must run g.data.save() to actually commit to disk.

You can create a DDP from any position in the search path; e.g. you can attach a list or dataframe, and its components will become objects in the DDP. In this case, the call to g.data.save(dir) must specify the path where files will be saved. Alternately, you can attach an existing DDP (or create a new, empty one) with g.data.attach(dir). In this case, the path is stored as an attribute of the environment (see searchpaths), and you can call g.data.save() with no arguments.

Value

g.data.get returns the requested data.
g.data.load returns data after loading it.

See Also

delay, autoload, searchpaths, save

Examples

## Don't run: 
ddp <- tempfile("newdir")           # Where to put the files
g.data.attach(ddp)                  # Warns that this is a new directory
assign("x1", matrix(1, 1000, 1000), 2)
assign("x2", matrix(2, 1000, 1000), 2)
g.data.save()                       # Writes the files
detach(2)

g.data.attach(ddp)                  # No warning, because directory exists
ls(2)
system.time(print(dim(x1)))         # Takes time to load up
system.time(print(dim(x1)))         # Second time is faster!
find("x1")                          # x1 still lives in pos=2, is now real
assign("x3", x2*10, 2)
g.data.save()                       # Or just g.data.save(obj="x3")
detach(2)

myx2 <- g.data.get("x2", ddp)       # Get one objects without attaching
unlink(ddp, recursive=TRUE)         # Clean up this example
## End Don't run

## Don't run: 
ddp <- tempfile("newdir")           # New example
y <- list(x1=1:1000, x2=2:1001)
attach(y)                           # Attach an existing list or dataframe
g.data.save(ddp)
detach(2)
unlink(ddp, recursive=TRUE)         # Clean up this example
## End Don't run

[Package Contents]