I have some scripts working well using ReporteRs and am trying to update them to use officer. My scripts are quite repetitive as I just need to output pretty much the same thing lots of times just changing font sometimes. After conversion I found the scripts are so slow I will not be able to use them. The scripts run in a few minutes in ReporteRs but take an age in officer.
Why is this 5000 times in officer:
body_add_par(doc, "")
So much slower than the equivalent in ReporteRs:
doc <- addParagraph(doc, '')
Many thanks
Code (all vector has 2000+ elements):
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)