与ReporteRs相比,军官似乎慢

时间:2018-07-19 16:34:45

标签: officer

我有一些使用ReporteR的脚本运行良好,并且正在尝试将其更新为使用Officer。我的脚本非常重复,因为我只需要输出很多相同的东西,有时只是更改字体。转换后,我发现脚本太慢了,我将无法使用它们。这些脚本可以在ReporteR中运行几分钟,但是会花很多时间在警官身上。

为什么这是官员5000次:

body_add_par(doc,“”)

比ReporteRs中的等效速度慢得多:

doc <-addParagraph(doc,'')

非常感谢

代码(所有向量都有2000多个元素):

outputFile <- paste0(OutputDir, "test.docx")

#SET STYLES
norm <- fp_text(color = "black", font.size = 10, bold = FALSE, italic = FALSE,
                underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                shading.color = "transparent")

norm_red <- fp_text(color = "red", font.size = 10, bold = FALSE, italic = FALSE,
                underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                shading.color = "transparent")

norm_blue <- fp_text(color = "blue", font.size = 10, bold = FALSE, italic = FALSE,
                    underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                    shading.color = "transparent")

norm_green <- fp_text(color = "green", font.size = 10, bold = FALSE, italic = FALSE,
                     underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                     shading.color = "transparent")

bold <- fp_text(color = "black", font.size = 10, bold = TRUE, italic = FALSE,
                underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                shading.color = "transparent")

bold_red <- fp_text(color = "red", font.size = 10, bold = TRUE, italic = FALSE,
                    underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                    shading.color = "transparent")

bold_blue <- fp_text(color = "blue", font.size = 10, bold = TRUE, italic = FALSE,
                     underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                     shading.color = "transparent")

bold_green <- fp_text(color = "green", font.size = 10, bold = TRUE, italic = FALSE,
                      underlined = FALSE, font.family = "Arial", vertical.align = "baseline",
                      shading.color = "transparent")

doc <- read_docx()

#ADD TITLE
fpar_ <- fpar(ftext("ASSIGNMENTS", prop = bold))
doc <- body_add_fpar(doc, fpar_, style = "centered", pos = "on")
doc <- body_add_par(doc, "", style = NULL, pos = "after")

#ADD DATE, DIRECTORY
fpar_ <- fpar(ftext("DATE: ", prop = bold),
              ftext(date(), prop = norm))
doc <- body_add_fpar(doc, fpar_, style = "Normal", pos = "after")

fpar_ <- fpar(ftext("DIRECTORY: ", prop = bold),
            ftext(Dir, prop = norm))
doc <- body_add_fpar(doc, fpar_, style = "Normal", pos = "after")

doc <- body_add_par(doc, "", style = NULL, pos = "after")

#Get all
all <- as.character(Summary$Name)

for (i in 1:length(all)) {

  res <- as.numeric(Types[Types$Num==all[i], "Code"])

  if (5 %in% res | 12 %in% res) {
    #Green
    fpar_ <- fpar(ftext(all[i], prop = bold_green))
  } else if (7 %in% res) {
    #Red
    fpar_ <- fpar(ftext(all[i], prop = bold_red))
  } else if (8 %in% res) {
    #Blue
    fpar_ <- fpar(ftext(all[i], prop = bold_blue))
  } else {
    fpar_ <- fpar(ftext(all[i], prop = bold))
  }

  doc <- body_add_fpar(doc, fpar_, style = "Normal", pos = "after")

  #Get list of files
  res <- unique(Detail[Detail$Num==all[i], c("Name", "Cat")])

  #OUTPUT FILE NAME AND CAT
  if (nrow(res) == 0) {
      #NO FILE FOUND
  } else {

     for (j in 1:nrow(res)) {

       fpar_ <- fpar(ftext(paste(as.character(res[j, "Name"]), " "), prop = bold),
                     ftext(as.character(res[j, "Cat"]), prop = norm))
       doc <- body_add_fpar(doc, fpar_, style = "Normal", pos = "after")
     }
  }
  doc <- body_add_par(doc, "", style = NULL, pos = "after")
}

print(doc, target = outputFile)

1 个答案:

答案 0 :(得分:1)

这是与某些可复制代码的比较:

// Video src
 var video_src = [
           "https://www.youtube.com/embed/sdUUx5FdySs?autoplay=1",
           "https://www.youtube.com/embed/YE7VzlLtp-4?autoplay=1"
];

var current_video = "";

var frame = document.getElementById('vid');

// slides
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
  current_video = video_src[n-1];
  frame.src = current_video;

}

结果如下-官员是获胜者:

library(officer)
library(ReporteRs)
library(microbenchmark)

docx()# first run can be slow because of java init. operations


mb <- microbenchmark::microbenchmark(
  officer = {
    doc <- read_docx()
    for(i in 1:100){
      doc <- body_add_par(doc, "")
    }
  }, 
  ReporteRs = {
    doc <- docx()
    for(i in 1:100){
      doc <- addParagraph(doc, '')
    }
  } )

这是我的> mb Unit: milliseconds expr min lq mean median uq max neval officer 224.3742 232.9602 238.8452 237.5110 241.5320 325.4288 100 ReporteRs 311.7194 337.9194 349.7107 343.9703 353.8814 447.2623 100 结果:

sessionInfo()