Search code examples
rlistsaveload

Save and reload a 'list' object in R


I am trying to save a list object that I manually created with some efforts into something in list format that I can reference back rather quickly. The saved object should be able to be loaded back as a list not df or anything.

Here's part of my list object down below:

citations <- list(
as.integer(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 111)),
as.integer(c(2, 3, 4, 5, 6, 7, 8, 9, 10, 111)),
as.integer(c(1, 3, 4, 5, 6, 7, 8, 9, 10, 111)),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(111),
as.integer(6),
integer(0),
as.integer(c(14, 15, 16, 17, 18, 19, 20, 21, 22, 23))
)

I have referenced this thread and a much earlier one on nabble, they suggested simply using save and load, but I just couldn't figure out how this works, whenever I applied the save() function on the list object and specified an R object for it, I always got an error message.

save(citations, "/~citations.Rdata")
Error in save(citations, "~/citations.Rdata") : object ‘~/citations.Rdata’ not found

Just don't know if other function calls are needed.


Solution

  • Your options are:

    • base::save and load
    • base::saveRDS and readRDS
    • rlist::list.save and list.load