简体   繁体   English

使用R在同一Excel工作表中优雅输出几个数据框

[英]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: 基本上,我想通过以下方式对表进行分组:

  • 15 tables per sheet 每张15桌
  • 6 sheets per file 每个档案6张
  • 3 files. 3个文件。

I have thought about grouping them the following way: 我已经考虑过通过以下方式对它们进行分组:

  • 1 table per sheet 每张一张桌子
  • 15 sheets per file 每个档案15张
  • 6 files per directory 每个目录6个文件
  • 3 directories 3个目录

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. 我还考虑过使用cbindsrbinds以及空的行/列将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.

相关问题 在Excel的同一张表中添加R输出 - Adding R output in the same sheet in excel R:将几个Excel文件读入不同的数据框并同时进行浏览 - R: Reading several Excel files into different dataframes and exploring these at the same time R Shiny将两个数据框导出到同一Excel工作表中的两个工作表中 - R Shiny export two dataframes into two sheets in same excel sheet 如何针对多个数据帧运行函数并输出与 R 中输入名称相同的数据帧 - How to run a function against several dataframes and output dataframes with the same name as input in R 使用r将多个数据帧导出到一个Excel工作表 - Export multiple dataframes to one Excel Sheet with r 将不同的数据框导出到同一个 Excel 工作表 - Export different dataframes to the same Excel sheet 如何将 R 中生成为 output 的数据帧列表导出到单个 excel 工作表 - How to export a list of dataframes generated as output within R to a single excel sheet 编写一个 excel 或 csv 文件,使数据帧列在同一张纸上,而不是 R 中的多张纸 - Write an excel or csv file in a way that the dataframes are listed on the same sheet, instead of multiple sheets in R 从 Rstudio 中的同一 excel 表导入两个数据帧作为单独的数据帧 - Import two dataframes from the same excel sheet in Rstudio as separated dataframes 如何将两个数据帧水平添加到 R 中的一张 excel 工作表 - How to add two dataframes horizontally to one excel sheet in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM