简体   繁体   中英

Serialization for a list of ffdf objects

I have a list, Z , where each Z[[i]] is an ffdf object.

My question is how to save Z to disk for future sessions, perhaps using something like save.ffdf .

Z <- list()
for(i in 1:10) {
  Z[[i]] <- as.ffdf(data.frame(array(1,dim=c(2,10))))
}

is(Z[[1]])
is(Z)

You can use ffsave() to save a list of objects, but it expects a list of names that can be resolved in the current environment. However, you can write your own helper function put each object (df) in the list into aa variable and the use ffsave to save these objects. Something like this:

saveList <- function(lst, fname) {
    outlist = c()
    for (i in 1:length(lst))  {
        name = paste0('out_',i)
        outlist = c(outlist, name)
        assign(name, lst[[i]])
    }
    ffsave(list=outlist, file=fname)
}

When you load this file with ffload() you will get a bunch of objects names out_1, out_2, ... in your current environment. You probably don't want this, so you need another helper function to put the things back into a list:

loadList <- function(fname) {
    ffload(fname)
    objs = ls(pattern="out")
    outlist = list()
    for (o in objs) {
        idx= as.integer( strsplit(o,"_")[[1]][2] )
        outlist[[idx]] = get(o)
    }
    outlist
}

Note: the source code has to be modified/extended if you want to use names instead of numeric index values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM