1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
# Collection of functions and data pre-processing to help with testing
library(officer)
library(xml2)
# xml related functions --------------------------------------------------------
get_docx_xml <- function(x) {
if (inherits(x, "flextable")) {
docx_file <- tempfile(fileext = ".docx")
doc <- read_docx()
doc <- body_add_flextable(doc, value = x)
print(doc, target = docx_file)
x <- docx_file
}
redoc <- read_docx(x)
xml_child(docx_body_xml(redoc))
}
get_pptx_xml <- function(x) {
if (inherits(x, "flextable")) {
pptx_file <- tempfile(fileext = ".pptx")
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(doc, x, location = ph_location_type(type = "body"))
print(doc, target = pptx_file)
x <- pptx_file
}
redoc <- read_pptx(x)
slide <- redoc$slide$get_slide(redoc$cursor)
xml_child(slide$get())
}
get_html_xml <- function(x) {
if (inherits(x, "flextable")) {
html_file <- tempfile(fileext = ".html")
save_as_html(tab, path = html_file)
x <- html_file
}
doc <- read_html(x)
xml_child(doc, "body")
}
get_pdf_text <- function(x, extract_fun) {
stopifnot(grepl("\\.pdf$", x))
doc <- extract_fun(x)
txtfile <- tempfile()
cat(paste0(doc, collapse = "\n"), file = txtfile)
readLines(txtfile)
}
render_rmd <- function(file, rmd_format) {
unlink(file, force = TRUE)
sucess <- FALSE
tryCatch(
{
render(rmd_file,
output_format = rmd_format,
output_file = pdf_file,
envir = new.env(),
quiet = TRUE
)
sucess <- TRUE
},
warning = function(e) {
},
error = function(e) {
}
)
sucess
}
|