用图R创建xlsx的最快方法

我有数据列表和我希望两个写入xlsx文件的图表列表(每个元素分隔表格).示例数据:

require(ggplot2)
require(data.table)

n <- 10
N <- 100

dtList <- lapply(1:n, function(x) data.table(sample(1e6, N), 1:N))
names(dtList) <- 1:n
plots <- lapply(dtList, function(x) ggplot(x, aes(y = V1, x = V2)) + geom_line())

目前我使用的是openxlsx,但对于多个图表来说速度很慢:

require(openxlsx)
wb <- createWorkbook()
modifyBaseFont(wb, fontSize = 10)

writeXlsx <- function(x, sName) {
  addWorksheet(wb, sName, gridLines = FALSE)
  writeData(wb, sName, x = x, xy = c(1, 1))
  print(plots[[sName]])
  insertPlot(wb, sName, width = 19, height = 9, dpi = 200, units = "cm",
             startRow = 2, startCol = 5)
}

system.time(
sapply(seq_along(dtList), function(x) {
  writeXlsx(dtList[[x]], names(dtList)[[x]])
})
) # ~ 17.00 sek

openXL(wb)

我怎么能提高速度呢?有没有更好的方案来实现这一目标?

最佳答案 一种选择是使用更简单的图形.例如,将绘图更改为基本图形,例如:

plots <- lapply(dtList, function(x) plot(x$V2, x$V1, type = 'l'))

将xlsx创建时间减少到~0.72秒对比~7.78秒(原始代码现在比以前更快),大约快10倍.

当需要ggplot图形时,我修改了insertPlot函数以接受这种类型的对象并将其保存到文件而无需在R会话中打印(使用ggsave):

insertggPlot <- function(wb, sheet, width = 6, height = 4, xy = NULL,
                        startRow = 1, startCol = 1, fileType = "png",
                        units = "in", dpi = 300, PLOT) {
  od <- getOption("OutDec")
  options(OutDec = ".")
  on.exit(expr = options(OutDec = od), add = TRUE)
  if (!"Workbook" %in% class(wb)) stop("First argument must be a Workbook.")
  if (!is.null(xy)) {
    startCol <- xy[[1]]
    startRow <- xy[[2]]
  }
  fileType <- tolower(fileType)
  units <- tolower(units)
  if (fileType == "jpg") fileType = "jpeg"
  if (!fileType %in% c("png", "jpeg", "tiff", "bmp")) 
    stop("Invalid file type.\nfileType must be one of: png, jpeg, tiff, bmp")
  if (!units %in% c("cm", "in", "px")) 
    stop("Invalid units.\nunits must be one of: cm, in, px")
  fileName <- tempfile(pattern = "figureImage",
                       fileext = paste0(".", fileType))
  ggsave(plot = PLOT, filename = fileName, width = width, height = height,
         units = units, dpi = dpi)
  insertImage(wb = wb, sheet = sheet, file = fileName, width = width, 
              height = height, startRow = startRow, startCol = startCol, 
              units = units, dpi = dpi)
}

使用它,可以减少~2 sek的时间.

点赞