[R] write.table equivalent for lists?

Rainer M Krug r.m.krug at gmail.com
Mon Jan 10 15:24:08 CET 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

I am writing simulations in R, and quite regularly, I have to save lists
and objects to HDD and load it later again.

So I am wondering: why is there no function to write lists (and S3, S4
objects) onto HDD WITHOUT keeping the name? What I mean is:

For data.frames I can use

x <- data.frame(x = runif(10))
write.table(x, "x.txt)
rm(x)

y <- read.table("x.txt")

to load it into y.

But for lists and S3, S4 objects, I have to use save() and load():

x <- list(x=1:10, y=letters(1:10))
save(x, file="x.rdata")
rm(x)

load("x.rdata") #1
y <- x          #2
rm(x)           #3

to load it into y.
I don't mind the binary of save - what I am really missing is a command
which combines lines #1, #2 and #3.

I know - I could write my own

save.list <- function(x, file) {
  save(s, file=file)
}

load.list <- function(file) {
  load(file)
  return(x)
}

which I could then use as:

x <- list(x=1:10, y=letters(1:10))
save.list(x, "test.rdata")

rm(x)

y <- load.list("test.rdata")

but: why is there not such a function? Am I missing something here?

Cheers,

Rainer

- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:        +33 - (0)9 53 10 27 44
Cell:       +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0rFocACgkQoYgNqgF2egoLMgCfQmZFnLx4Olkb55DtLKmuRXYY
yvoAnieTY3/CsGM9pqyoownLxgeuaDrd
=VOFu
-----END PGP SIGNATURE-----



More information about the R-help mailing list