Skip to content

write.table equivalent for lists?

10 messages · Rainer M Krug, Ivan Calandra, Peter Ehlers +2 more

#
-----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-----
#
Hi,

If I understood you correctly, you can use saveObject()/loadObject() 
from package R.utils, like this:

library(R.utils)
saveObject(x, "x.Rbin")
rm(x)
y <- loadObject("x.Rbin")

HTH,
Ivan

Le 1/10/2011 15:24, Rainer M Krug a ?crit :

  
    
#
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/10/2011 03:35 PM, Ivan Calandra wrote:
Correct - thanks - that is what I was looking for.

But why is no such function in R base?
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
- -- 
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/

iEYEARECAAYFAk0rGugACgkQoYgNqgF2egrnPwCfVM3v/pyJdZQ5f9Fd9w9Mnsfm
dDYAnAgkcuZqcI+QG6YRU2pEui6PvE/Q
=4j0d
-----END PGP SIGNATURE-----
#
Why isn't it in R base? Good question... I'm definitely not the one able 
to answer to it!
Maybe someone more involved in the development can?
Ivan

Le 1/10/2011 15:42, Rainer M Krug a ?crit :

  
    
#
On 2011-01-10 06:45, Ivan Calandra wrote:
Well, I suspect that Rainer has been on this list long
enough to know the answer:
  Because no has convinced R-Core to implement it!

While I might _occasionally_ find it useful, I don't
mind if R-Core prefer to pursue other language enhancements.

Peter Ehlers
[...snip...]
#
I agree that the R-Core team has better things to do than that, but 
since it's already there, why not just add it to the base functions?

Le 1/10/2011 16:19, Peter Ehlers a ?crit :

  
    
#
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/10/2011 04:27 PM, Ivan Calandra wrote:
Agree completely - and R.utils is not such an obscure package which
nobody uses - so that these functions should be quite bug-free.

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/

iEYEARECAAYFAk0rJw4ACgkQoYgNqgF2egreYACfTbfDH4E1SadVzq/WoZD9Tw9h
A5EAnREavj5Wy6ru2ALJeKNc8kZPXIlM
=OoKQ
-----END PGP SIGNATURE-----
#
On 2011-01-10 07:34, Rainer M Krug wrote:
Henrik write excellent code, maintains it well, and
responds quickly. Can't ask for much more.

As to putting this into pkg:base or pkg:utils, Ivan,
have a look at the code of R.utils::loadObject; it's not
quite trivial.

Peter Ehlers
#
How about dput and dget in the base package?
#
serialize() and unserialize() in the base package
should do the job also and are usually quite a bit
faster than dput() and dget().  However, the format
is not easily read by other programs.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com