【问题标题】:Name of tables exporting from R to excel从 R 导出到 excel 的表的名称
【发布时间】:2020-09-29 08:39:46
【问题描述】:

我有不同的数据框列表,在使用此代码导出到 excel 之前,我将它们绑定在一起:

l_listOfDF <- mget(ls(pattern="listofdf"))

导出工作正常使用:

mywb <- createWorkbook()

for (sheetName in names(listofdfs)){
  addWorksheet(mywb , sheetName )
}

# get all lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))

# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)

# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){
  
  for(pageName in names(l_listOfDF[[N_myListOfDF]])){
    
    label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", pageName, " users between 2010-2019")
    
    # write the name/number of your table in the correct sheet, at the correct row
    writeData(mywb, sheet=pageName, startRow=startR[pageName], label_2)
    
    # write your data in the correct sheet at the correct row (the one after the name)
    writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
    
    # update the row number (the + 3 is to leave space for name of table, headers and blank row
    startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3
  }
}

但我无法为要导出的每个表分配正确的标题。

在标签中:

label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", pageName, " users between 2010-2019")

我想添加如下内容:

x <- c("","","females","males")

并获得:

label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", x, pageName, " users between 2010-2019")

为了让每个 excel 表的第一个表具有以下标题:

表 1. 2010-2019 年(pageName)用户数

对于每个excel表的第二个表:

表 2. 2010-2019 年(pageName)用户数

第三个:

表 3. 2010-2019 年女性(页面名称)用户数

第四次:

表 4.男性(页面名称)用户数量(页面名称)2010-2019

但是仅仅在 label_2 中添加向量 x 并不能正常工作。 我在每张表中都增加了表格。

有什么有用的想法吗?谢谢!

这是一个可重现的例子:

listofdfs <- list(data.frame("y"=c(2009,2010,2011),"b"=c(35,30,20)), data.frame("y"=c(2009,2010,2011), "b"=c(6,21,40)), data.frame("y"=c(2009,2010,2011), "a"=c(13,30,5), "b"=c(40,18,25)), data.frame("y"=c(2009,2010,2011), "a"=c(8,36,7), "b"=c(32,9,17))  )

listofdfs_2 <- list(data.frame("y"=c(2009,2010,2011),"b"=c(14,36,8)), data.frame("y"=c(2009,2010,2011), "b"=c(36,27,9)), data.frame("y"=c(2009,2010,2011), "a"=c(9,15,58), "b"=c(7,11,20)), data.frame("y"=c(2009,2010,2011), "a"=c(48,3,67), "b"=c(2,28,37))  )

mywb <- createWorkbook()

for (sheetName in names(listofdfs)){
  addWorksheet(mywb , sheetName )
}

# get all lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))

# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)

# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){

  for(pageName in names(l_listOfDF[[N_myListOfDF]])){

    label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", c("","","females","males"), pageName, " users between 2010-2019")

    # write the name/number of your table in the correct sheet, at the correct row
    writeData(mywb, sheet=pageName, startRow=startR[pageName], label_2)

    # write your data in the correct sheet at the correct row (the one after the name)
    writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])

    # update the row number (the + 3 is to leave space for name of table, headers and blank row
    startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3
  }
}

# save your workbook in a file
saveWorkbook(mywb , "diroutput\\pippo.xlsx")

【问题讨论】:

  • 有什么想法吗?有一个可重现的例子会更好吗?
  • 是的,一个可重现的例子在这里可能会有所帮助。
  • 好的,我要编辑我的问题并添加一个示例!
  • 完成了!现在好点了吗?
  • 下面的答案能满足你的需要吗?

标签: r excel export


【解决方案1】:

我会尝试使用您的标签创建一个简单的向量并在其他地方定义,例如:

label_vec <- c("","","females","males")

然后,要获取您的标题标签,请使用来自N_myListOfDF 的索引来获取正确的文本标签:

label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", label_vec[N_myListOfDF], " ", pageName, " users between 2010-2019")

上面的例子只有两个表,数据框列表没有名字,所以我做了一些小的修改以获得一个可重现的例子(下)。另外,示例中只有2个表,所以我使用test1和test2而不是空字符串来测试它。

可重现的例子

library(openxlsx)

listofdfs <- list(
  "One" = data.frame("y"=c(2009,2010,2011),"b"=c(35,30,20)), 
  "Two" = data.frame("y"=c(2009,2010,2011), "b"=c(6,21,40)), 
  "Three" = data.frame("y"=c(2009,2010,2011), "a"=c(13,30,5), "b"=c(40,18,25)), 
  "Four" = data.frame("y"=c(2009,2010,2011), "a"=c(8,36,7), "b"=c(32,9,17))  
)

listofdfs_2 <- list(
  "One" = data.frame("y"=c(2009,2010,2011),"b"=c(14,36,8)), 
  "Two" = data.frame("y"=c(2009,2010,2011), "b"=c(36,27,9)), 
  "Three" = data.frame("y"=c(2009,2010,2011), "a"=c(9,15,58), "b"=c(7,11,20)), 
  "Four" = data.frame("y"=c(2009,2010,2011), "a"=c(48,3,67), "b"=c(2,28,37))  
)

mywb <- createWorkbook()

for (sheetName in names(listofdfs)){
  addWorksheet(mywb , sheetName )
}

# get all lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))

# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs))
names(startR) <- names(listofdfs)

# create vector of labels to insert into table titles
label_vec <- c("test1","test2","females","males")

# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){
  
  for(pageName in names(l_listOfDF[[N_myListOfDF]])){
    
    #label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", c("","","females","males"), pageName, " users between 2010-2019")
    label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", label_vec[N_myListOfDF], " ", pageName, " users between 2010-2019")
    
    # write the name/number of your table in the correct sheet, at the correct row
    writeData(mywb, sheet=pageName, startRow=startR[pageName], label_2)
    
    # write your data in the correct sheet at the correct row (the one after the name)
    writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
    
    # update the row number (the + 3 is to leave space for name of table, headers and blank row
    startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3
  }
}

# save your workbook in a file
saveWorkbook(mywb , "pippo.xlsx", overwrite = T)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    相关资源
    最近更新 更多