[R] Reading and writing to S-like databases

David Brahm a215020 at agate.fmr.com
Fri Sep 28 20:12:52 CEST 2001


I asked:
> In S-Plus, I build databases of many large objects.  In any given analysis,
> I only need a few of those objects, but attach'ing the whole database is fine
> since objects are only read as needed.  How can I do the same thing in R,
> without reading the entire database?

Responses were generally in 2 categories:
1) Use an external SQL database (fine for dataframes, but not flexible enough),
2) Use autoload() or delay()  (thanks to Agustin Lobo & Ray Brownrigg!).
Here's what I came up with from the second approach.

##### Code: #####
"%&%" <- function(a, b) paste(a, b, sep="")
g.save.data <- function(dir, pos=2) {
  for (i in dir %&% c("", "/data", "/R")) if (!file.exists(i)) dir.create(i)
  obj <- objects(pos, all.names=T)
  for (i in obj) save(list=i, file=dir %&% "/data/" %&% i %&% ".RData")
  code <- obj %&% " <- delay({data(\"" %&% obj %&% "\"); " %&% obj %&% "})"
  cat(code, file=dir %&% "/R/" %&% basename(dir), sep="\n")
}
g.attach <- function(dir) library(basename(dir), lib.loc=dirname(dir), char=T)

##### Example: #####
attach(NULL, name="newdata")    # Create some data in a new environment
assign("x1",  1:10, 2)
assign("x2", 11:20, 2)
g.save.data("/tmp/newdata")     # Save that environment's contents to a pkg
detach(2)
g.attach("/tmp/newdata")        # Open the pkg and see the data!

				-- David Brahm (a215020 at agate.fmr.com)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list