我有一些使用 ReporteR 运行良好的脚本,并正在尝试更新它们以使用官员。我的脚本非常重复,因为我只需要多次输出几乎相同的东西,有时只是改变字体。转换后,我发现脚本太慢了,我将无法使用它们。这些脚本在 ReporteRs 中运行几分钟,但在官员中需要一段时间。
为什么这是5000次在官:
body_add_par(文档,“”)
比 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)