簡體   English   中英

如何在R中的for循環中的同一Excel文件中導出單獨的工作表?

[英]How to export separate worksheets in a same excel file within a for loop in 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 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'

我將不勝感激!

你好親密! 看下面的代碼。 我使用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)

輸出看起來像這樣:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM