如何在R中的for循环中的同一Excel文件中导出单独的工作表?

时间:2019-05-24 08:27:01

标签: r

我有一些列表,每个列表都包含一些数据帧。假设第一个列表如下:

df1 <- data.frame("id" = 1:2, "weight" = c(10,15), "Name" = c("ha","hu"))
df2 <- data.frame("id" = 3:4, "weight" = c(20,15), "Name" = c("hi","he"))
df3 <- data.frame("id" = 5:6, "weight" = c(10,20), "Name" = c("ho","hy"))
my_list_1 <- list(df1, df2, df3)

和第二个:

df4 <- data.frame("id" = 7:8, "weight" = c(5,6), "Name" = c("ma","mu"))
df5 <- data.frame("id" = 9:10, "weight" = c(20,12), "Name" = c("mi","me"))
df6 <- data.frame("id" = 11:12, "weight" = c(8,20), "Name" = c("mo","my"))
my_list_2 <- list(df4, df5, df6)

实际上,还有更多!

我想编写一个for循环,并在每个循环中将lists之一写入一个Excel worksheet 与列表相同的名称

Dataframes应该在工作表中一个接一个地放置,并且在任意两个dataframes之间有两个空白行。我尝试了以下代码:

library(openxlsx)
wb <- createWorkbook()
for (i in 1:2){
     addWorksheet(wb, paste0("my_list_",i))
     currRow <- 1
     for(j in 1:3){
          cs <- CellStyle(wb) + Font(wb, isBold=TRUE) + Border(position=c("BOTTOM", "LEFT", "TOP", "RIGHT"))
          addDataFrame(eval(parse(text=paste0("my_list_",i,"[[j]]"))),
                       sheet=paste0("my_list_",i),
                       startRow=currRow,
                       row.names=FALSE,
                       colnamesStyle=cs)
          currRow <- currRow + eval(parse(text=paste0("nrow(my_list_",i,"[[j]])"))) + 2 
     }
}
saveWorkbook(wb, file = "myfile.xlsx"))

但是我得到了错误术语:

Error in as.vector(x, "character") : 
  cannot coerce type 'environment' to vector of type 'character'

我将不胜感激!

1 个答案:

答案 0 :(得分:1)

您是如此亲密! 看下面的代码。我使用writeData (doc)而不是addDataFrame。参数是几乎相同的。

您的数据:

# First list
df1 <- data.frame("id" = 1:2, "weight" = c(10,15), "Name" = c("ha","hu"))
df2 <- data.frame("id" = 3:4, "weight" = c(20,15), "Name" = c("hi","he"))
df3 <- data.frame("id" = 5:6, "weight" = c(10,20), "Name" = c("ho","hy"))
my_list_1 <- list(df1, df2, df3)

# Second list
df4 <- data.frame("id" = 7:8, "weight" = c(5,6), "Name" = c("ma","mu"))
df5 <- data.frame("id" = 9:10, "weight" = c(20,12), "Name" = c("mi","me"))
df6 <- data.frame("id" = 11:12, "weight" = c(8,20), "Name" = c("mo","my"))
my_list_2 <- list(df4, df5, df6)

导出excel

# Module
library(openxlsx)

# Header style of each table in the excel file
hs1 <- createStyle(fgFill = "#DCE6F1", halign = "CENTER", textDecoration = "italic",
                   border = "Bottom")

# create workbook object
wb <- createWorkbook("Fred")

# For each list
for (i in 1:2){
  sheet <- paste0("my_list_",i)
  addWorksheet(wb, sheet)
  currRow <- 1
  for(j in 1:3){
    # Write the data frame
    writeData(wb = wb, 
              sheet = sheet,
              x = eval(parse(text=paste0("my_list_",i,"[[j]]"))),
              startRow = currRow, 
              borders="rows",
              headerStyle = hs1,
              borderStyle = "dashed",
              borderColour = "black"
             )
    # Update index
    currRow <- currRow + eval(parse(text=paste0("nrow(my_list_",i,"[[j]])"))) + 3 
  }
}

# Save file 
saveWorkbook(wb, file = "myfile.xlsx") #, overwrite = TRUE)

输出看起来像这样:

enter image description here