write list of dataframes to multiple excel files

筅森魡賤 提交于 2021-01-28 22:13:11

问题


I have a list of dataframes. Conveniently named:

list.df

and the objects, which are dataframes, are just this:

list.df[[1]]  
list.df[[2]]  
list.df[[3]]

I am trying to use lapply to write each of the list.df objects to a seperate excel sheet.

I can't use the xlsx library because my workplace disables everything Java... so I've been trying write_xlsx.

I've tried the following:

lapply(names(list.df), 
              function (x) write_xlsx(list.df[[x]], file=paste(x, "xlsx", sep=".")))

But nothing happens.

Any help would be appreciated.


回答1:


For write_xlsx from writexl, there is no file argument. It is the path argument

library(writexl)
lapply(names(list.df), 
          function (x) write_xlsx(list.df[[x]], path=paste(x, "xlsx", sep=".")))

-output directory with files

data

list.df <-  list(A = structure(list(X1 = c("a", "d", "g", "j"), X2 = 1:4, 
    X3 = c("b", "e", "h", "k"), X4 = c("c", "f", "i", "l")),
  class = "data.frame", row.names = c(NA, 
-4L)), B = structure(list(X1 = c("a", "d", "g", "j"), X2 = c(1L, 
2L, 2L, 3L), X3 = c("b", "e", "h", "k"), X4 = c("c", "f", "i", 
"l")), class = "data.frame", row.names = c(NA, -4L)), C = structure(list(
    X1 = c("a", "d", "g", "j"), X2 = 1:4, X3 = c("b", "e", "h", 
    "k"), X4 = c("c", "f", "i", "l")), class = "data.frame", row.names = c(NA, 
-4L)))



回答2:


i think this might help you

require(openxlsx)
available_dfs<- ls()[sapply(ls(), function(x) is.data.frame(get(x)))]

list_of_datasets <- list("Name of DataSheet1" = dataframe1, "Name of Datasheet2" = dataframe2)
write.xlsx(list_of_datasets, file = "writeXLSX2.xlsx")


来源:https://stackoverflow.com/questions/56831195/write-list-of-dataframes-to-multiple-excel-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!