【问题标题】:fastest way to create xlsx with plots R用地块 R 创建 xlsx 的最快方法
【发布时间】:2017-12-28 16:08:03
【问题描述】:

我有我想要两个写入 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)

我怎样才能提高这个速度?有没有更好的package 来实现这一点?

【问题讨论】:

标签: r excel performance ggplot2 xlsx


【解决方案1】:

一种选择是使用更简单的图形。例如,将绘图更改为base 图形,如:

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。

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2018-12-25
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2012-02-21
    相关资源
    最近更新 更多