gravatar for luca

21 hours ago by

Hi there,
As a result of my -omics analyses I have a series of data frames that I would like to output. Since they all belong to the same analysis I thought it was nice to save all these dataframes into a single .xls file, putting each dataframe into a separate sheet.
I found the function xlsx.writeMultipleData from the r2excel package that would do the job for me.
The code for this function is:

xlsx.writeMultipleData = function (file, ...)  
  require(xlsx, quietly = TRUE)  
  objects <- list(...)  
  fargs <- as.list( = TRUE))  
  objnames <- as.character(fargs)[-c(1, 2)]  
  nobjects <- length(objects)  
  for (i in 1:nobjects) {  
    if (i == 1)  
      write.xlsx(objects[[i]], file, sheetName = objnames[i])  
    else write.xlsx(objects[[i]], file, sheetName = objnames[i],  
                    append = TRUE)  

If I assume to have two dataframe like this:

df1 <- data.frame(name = c("Jon", "Bill", "Maria"),
                 age = c(23, 41, 32))  
df2 <- data.frame(name =c("Albert", "Mary","Joe"),
                 age = c(80,28,1))

I can save df1 and df2 in the same file by doing: xlsx.writeMultipleData(file="outputfile.xls",df1,df2).

My problem arise from the fact that the number of dataframes that I generate is not always constant (i.e. one time I can have df1 and df2, one time I can have df1, df2 and df3, one time I can have df1, df2, df3 and df4).
So, I was wondering is there a general way to tell the function to save all the objects whose name matches df into one file, without having to explicitly write all of them? I know it is not R, but what I am looking for is something like xlsx.writeMultipleData(file="outputfile.xls", df*).

Thanks a lot in advance for your help


modified 19 hours ago



21 hours ago


Source link