How to save multiple lists of dataframes and limit number of list elements that can be saved?

266 views Asked by At

I have multiple lists with varying number of elements in each list as in screenshot.

enter image description here

My current code allows user to save one list at a time. But I am curious if there is a way I can allow user to save all the lists at once.

Additionally (if possible) I want to limit the number of list elements that can be downloaded at once. Lets say for a dataset, list contains 64 elements, but I want user to be able to download only 15 files at a time.

Current code goes like this:

saveFiles <- Function(list){
sapply(names(list), function(x) write.table(list[[x]], file= paste(x,".txt"), sep= "\t", quote=F))
}

Any advice how can this be resolved.

1

There are 1 answers

2
badgorilla On

I am not sure I understand your question. is this any good?

library(tictoc) #measure time difference
library(doParallel)

LIST<-lapply(1:150, function(X) X) #create data
names(LIST)<-1:150

doParallel::registerDoParallel(5)
# run in parallel 
tic()
foreach(list=LIST) %dopar% {
  write.table(LIST[[list]], file= paste(list,".txt"), sep= "\t", quote=F)
}
toc()

# regular loop
tic()
for(list in LIST) {
  write.table(LIST[[list]], file= paste(list,".txt"), sep= "\t", quote=F)
}
toc()