How can I save a list to a file and read it in again (in R)?

saveRDS(starting.values, file="fname.RData")
?saveRDS

Allows you to save one or more R objects to a single file.

Load with

readRDS("fname.RData")

(Edited from comments below, since the previous save and load functions don't seem to work anymore.)


I would also suggest that you explore rlist package and the offered list.save function as it provides additional flexibility when saving lists.

Example

As available in documentation.

require(rlist)
x <- lapply(1:5,function(i) data.frame(a=i,b=i^2))
list.save(x, 'list.rds')
list.save(x, 'list.rdata')
list.save(x, 'list.yaml')

yaml file preview

- a: 1
  b: 1.0
- a: 2
  b: 4.0
- a: 3
  b: 9.0
- a: 4
  b: 16.0
- a: 5
  b: 25.0

You could then load list with use of the list.load function in the same package or you could read lines from the exported yaml file. You could also consider having a look at the list.select function and selecting objects from within list. Having said that, I presume that the easiest way to approach this would be simply to store/load a full list object.

Tags:

List

R

Save