[英]Elegantly output several dataframes in the same Excel sheet with R
I am currently trying to output several R data frames in the same Excel Sheet. 我目前正在尝试在同一Excel工作表中输出多个R数据帧。 I have a script that generates 270 tables. 我有一个生成270个表的脚本。 Basically, I would like to group the tables this way: 基本上,我想通过以下方式对表进行分组:
I have thought about grouping them the following way: 我已经考虑过通过以下方式对它们进行分组:
But it means that I would have to jump between each file whilst working, which is not really practical. 但这意味着我在工作时必须在每个文件之间跳转,这实际上并不实用。 I also thought about grouping the 15 tables sets in one data frame using cbinds
and rbinds
and empty rows/columns to mark the separation between them. 我还考虑过使用cbinds
和rbinds
以及空的行/列将15个表集分组在一个数据框中,以标记它们之间的分隔。 However, I am not happy with this solution as it is not really elegant. 但是,我对这种解决方案并不满意,因为它并不十分优雅。
I don't know if any package offers the possibility to do this or if xslx/Rexcel packages do, but I haven't found it. 我不知道是否有任何软件包可以做到这一点,或者xslx / Rexcel软件包是否可以这样做,但是我还没有找到。 When I try to find an answer to this question, I only find topics about writing in different Excel sheets. 当我尝试找到此问题的答案时,我仅找到有关在不同Excel工作表中编写的主题。
If anyone has the solution if would be pleased to know about it :). 如果有人有解决方案,将很高兴知道:)。
I have recently moved away form XLConnect , but in the past I have used a function similar to the one below for this sort of thing. 我最近从XLConnect移走了 ,但是在过去,我已经使用了一种类似于以下功能的函数。
require(XLConnect)
write_excel_gap <- function(path = NULL,
data_list = NULL,
gap = 1,
sheet = "Sheet1",
header = TRUE,
firstRow = 1,
add = FALSE,
addSheet = TRUE,
style = TRUE,...){
stopifnot(length(gap) > 0,!is.null(path),!is.null(data_list))
if (add){
wb <- loadWorkbook(path,create = FALSE)
}else{
wb <- loadWorkbook(path,create = TRUE)
}
if (addSheet){
createSheet(wb,sheet)
}
if (!style){
setStyleAction(wb,XLC$"STYLE_ACTION.NONE")
}
n <- length(data_list)
ng <- length(gap)
dl_rows <- sapply(data_list,nrow)
if (ng == 1){
sr <- c(firstRow,firstRow + head(cumsum(dl_rows),-1)) +
c(0,cumsum(rep(gap,n-1)))
}else{
if(ng != n-1){
warning("Length of gap is not one less than length of data_list.
\nGap will be recycled as needed.")
}
sr <- c(firstRow,firstRow + head(cumsum(dl_rows),-1)) +
c(0,cumsum(gap))
}
if (header){
writeWorksheet(object=wb,
data=data_list[[1]],
sheet=sheet,
startRow=firstRow,
header = TRUE,...)
sr <- sr[-1] + 1
writeWorksheet(object = wb,
data = data_list[-1],
sheet = sheet,
startRow = sr,
header = FALSE,...)
}else{
writeWorksheet(object = wb,
data = data_list,
sheet = sheet,
startRow = sr,
header = FALSE,...)
}
saveWorkbook(wb)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.