Nothing
context("DOCX Tests")
base_path <- paste0(getwd(),"/tests/testthat")
data_dir <- base_path
base_path <- tempdir()
data_dir <- "."
cnt <- paste0("Lorem ipsum dolor sit amet, consectetur adipiscing elit, ",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ",
"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in ",
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ",
"pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa ",
"qui officia deserunt mollit anim id est laborum.")
fnt <- "Arial"
fsz <- 10
dev <- FALSE
# Basic Tests 1 - 10 ------------------------------------------------------
test_that("docx0: Basic docx works as expected.", {
fp <- file.path(base_path, "docx/test0.docx")
txt <- create_text(cnt, align = "left", width = 5, borders = c("all")) %>%
titles("Here is my first title", blank_row = "below", borders = "all",
align = "center", font_size = 14) %>%
footnotes("Here is a footnotey", blank_row = "both", borders = "all")
rpt <- create_report(fp, output_type = "DOCX", font = "Times",
font_size = 10, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(txt, align = "center") %>%
#add_content(create_text("Goodbye")) %>%
page_header(c("Left1", "Left2"), "Right") %>%
page_footer(c("Page [pg] of [tpg]", "Left"), "Center", "Right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx1: Basic text with title header works as expected.", {
fp <- file.path(base_path, "docx/test1.docx")
txt <- create_text(cnt, align = "left", width = 5, borders = c("all")) %>%
title_header("Here is my first title", right = "Right", blank_row = "none",
borders = "all") %>%
footnotes("Here is my footnote", blank_row = "both", borders = "all")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 11, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(txt, align = "center") %>%
page_header(c("Left1", "Left2"), "Right") %>%
page_footer(c("Left", "Left2"), "Center", "Right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
# expect_equal(res$pages, 1)
})
test_that("docx2: Basic table works as expected.", {
fp <- file.path(base_path, "docx/test2.docx")
dat <- mtcars[1:15, ]
attr(dat[[2]], "label") <- "Cylin."
attr(dat[[2]], "width") <- 1
attr(dat[[2]], "justify") <- "center"
tbl <- create_table(dat, first_row_blank = TRUE, borders = "top") %>%
titles("Table 1.0", "My Nice Table", borders = c("top"),
width = "content", align = "left") %>%
footnotes("My footnote 1", "My footnote 2 Page [pg] of [tpg]",
borders = c( "top"),
align = "left", width = "content", blank_row = "none") %>%
define(wt, width = 2, label = "Weight", align = "center",
label_align = "right")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1 and more", "Right2", "Page [pg] of [tpg]"),
blank_row = "below") %>%
add_content(tbl, align = "left") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx3: Basic table with title header works as expected.", {
fp <- file.path(base_path, "docx/test3.docx")
dat <- mtcars[1:15, ]
attr(dat[[2]], "label") <- "Cylin."
attr(dat[[2]], "width") <- 1
tbl <- create_table(dat, borders = c("outside")) %>%
title_header("Table 1.0", "My Nice Table", right = "Right",
borders = c("top", "bottom"), blank_row = "none",
width = "content") %>%
footnotes("My footnote 1", "My footnote 2", borders = "outside",
align = "left", width = "content")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"),
blank_row = "none") %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx4: Spanning headers work as expected.", {
fp <- file.path(base_path, "docx/test4")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c( "none")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = FALSE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(drat, gear, "Super Duper\nWrapped Span",
n = 11, level = 2) %>%
titles("Table 1.0", "My Nice Table", blank_row = "none",
borders = c("top", "bottom")) %>%
footnotes("My footnote 1", "My footnote 2",
blank_row = "none", borders = c("top", "bottom"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx5: Multi page table works as expected.", {
fp <- file.path(base_path, "docx/test5")
dat <- iris
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Irises", "Another Title") %>%
define(Sepal.Length, label = "Sepal Length", width = 1.5, align = "center") %>%
define(Sepal.Width, label = "Sepal Width", width = 1.25, align = "centre") %>%
define(Species, blank_after = TRUE)
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
add_content(tbl, blank_row = "none") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]") %>%
footnotes("My footnote 1", "My footnote 2")
res <- write_report(rpt)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 8)
})
test_that("docx6: Basic text works as expected.", {
fp <- file.path(base_path, "docx/test6")
txt <- create_text(cnt, width = 6, borders = "outside", align = "center") %>%
titles("Text 1.0", "My Nice Text", borders = "none", width = "content") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier",
font_size = 12, paper_size = "letter") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", "Right") %>%
add_content(txt, align = "center") %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx7: Basic plot works as expected.", {
library(ggplot2)
fp <- file.path(base_path, "docx/test7.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
plt <- create_plot(p, height = 4, width = 7, borders = c("all")) %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none",
font_size = 12, align = "left") %>%
footnotes("* Motor Trend, 1974", borders = "none")
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size =fsz) %>%
page_header("Client", "Study: XYZ") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center") %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
# Good for testing
test_that("docx8: Page by works as expected.", {
fp <- file.path(base_path, "docx/test8")
dat <- iris
tbl <- create_table(dat, borders = "all") %>%
titles("Table 1.0", "My Nice Irises", "Another Title",
borders = "none") %>%
page_by(Species, label = "Species: ", borders = "outside",
blank_row = "both", align = "center") %>%
define(Sepal.Length, label = "Sepal Length", width = 1.5, align = "center") %>%
define(Sepal.Width, label = "Sepal Width", width = 1.25, align = "centre") %>%
define(Species, visible = FALSE) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "portrait",
paper_size = "letter") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
add_content(tbl, blank_row = "none", align = "left") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]")
res <- write_report(rpt)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 6)
})
# Good for testing. Check bottom footnote positioning.
test_that("docx9: Page by on report works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test9.docx")
dat <- iris
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Irises", "Another Title") %>%
define(Sepal.Length, label = "Sepal Length", width = 1.5, align = "center") %>%
define(Sepal.Width, label = "Sepal Width", width = 1.25, align = "centre")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "landscape",
paper_size = "letter") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
page_by(Species, label = "Species: ") %>%
add_content(tbl, blank_row = "none") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]") %>%
footnotes("My footnote 1", "My footnote 2")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 9)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx10: Title Header and page header/footer wrapping work as expected.", {
fp <- file.path(base_path, "docx/test10.docx")
dat <- iris[1:10, ]
tbl <- create_table(dat, borders = "none") %>%
title_header("Table 1.0", "My Nice Report with Borders",
right = c("Right1", "Right2"),
#"Right3 long enough to wrap around at least once"),
borders = "none",
blank_row = "none") %>%
# titles("Here is a title\nthat is going to wrap", "Another title") %>%
footnotes("My footnote 1",
paste("My footnote 2 Center1 here is a whole bunch of stuff to try and make it wrap",
"like more down here note 2 Center1 here is a whole bunch of stuff." ),
valign = "top",
borders = "none",
blank_row = "above") %>%
define(Sepal.Width, label = "Here is a rather long header label")
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_header(c("Left1", "Left2\nwrap"), "Right 1") %>%
page_footer("Left1",
"Center1 here is a whole bunch of stuff to try and make it wrap",
"Right1\nwrap\n and wrap again")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
expect_equal(length(res$column_widths[[1]]), 5)
})
test_that("docx11: Basic plot with titles on report works as expected.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test11.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
plt <- create_plot(p, height = 4, width = 8, borders = c("none"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size =fsz) %>%
page_header("Client", "Study: XYZ") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center") %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none") %>%
footnotes("* Motor Trend, 1974", borders = "none")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx12: Text with titles on report works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test12")
txt <- create_text(cnt, width = 6, borders = "none", align = "center")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier",
font_size = 12, paper_size = "letter") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", "Right") %>%
add_content(txt, align = "center") %>%
page_footer("Left1", "Center1", "Right1")%>%
titles("Text 1.0", "My Nice Text", borders = "none", width = "page") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
# Works on all combinations on font and font size. Needed adjustments to row height.
test_that("docx13: Table with break between sections works as expected.", {
fp <- file.path(base_path, "docx/test13.docx")
# Setup
subjid <- 100:109
name <- c("Quintana, Gabriel", "Allison, Blas", "Minniear, Presley",
"al-Kazemi, Najwa", "Schaffer, Ashley", "Laner, Tahma",
"Perry, Sean", "Crews, Deshawn Joseph", "Person, Ladon here is some more",
"Smith, Shaileigh and \nmore and more and even more and more and more")
sex <- c("M", "F", "F", "M", "M", "F", "M", "F", "F", "M")
age <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
arm <- c(rep("A", 5), rep("B", 5))
# Create data frame
df <- data.frame(subjid, name, sex, age, arm)
tbl1 <- create_table(df, first_row_blank = TRUE, borders = "all") %>%
define(subjid, label = "Subject ID", align = "left", width = 1) %>%
define(name, label = "Subject Name", width = 1) %>%
define(sex, label = "Sex") %>%
define(age, label = "Age") %>%
define(arm, label = "Arm",
blank_after = TRUE,
dedupe = TRUE,
align = "right") #%>%
# spanning_header(sex, arm, label = "Here is a spanning header")
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size = fsz) %>%
page_header(left = "Experis", right = c("Study ABC", "Status: Closed")) %>%
# options_fixed(line_count = 46) %>%
titles("Table 1.0", "Analysis Data Subject Listing\n And more stuff",
"Safety Population", align = "center", bold = TRUE) %>%
footnotes("Program Name: table1_0.R",
"Here is a big long footnote that is going to wrap\n at least once") %>%
page_footer(left = "Time", center = "Confidential",
right = "Page [pg] of [tpg]") %>%
add_content(tbl1)
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx14: Plot with page by on plot works as expected.", {
library(ggplot2)
fp <- file.path(base_path, "docx/test14.docx")
fmt <- value(condition(x == 4, "4 Cylinder"),
condition(x == 6, "6 Cylinder"),
condition(x == 8, "8 Cylinder"))
dat <- mtcars[order(mtcars$cyl), ]
p <- ggplot(dat, aes(x=disp, y=mpg)) + geom_point()
brdrs <- "outside"
#dats <- split(p$data, p$data$grp)
#tbl <- create_table(dat[1:3, ])
plt <- create_plot(p, height = 4, width = 8, borders = "all") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot",
borders = "all",
blank_row = "both") %>%
page_by(cyl, "Cylinders: ", borders = "all", blank_row = "both",
format = fmt) %>%
footnotes("* Motor Trend, 1974", borders = "all", valign = "top",
blank_row = "both")
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size = fsz) %>%
page_header("Client", "Study: XYZ") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt) %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 3)
})
test_that("docx15: Title bold and font size works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test15.docx")
dat <- mtcars[1:15, ]
attr(dat[[2]], "label") <- "Cylin."
attr(dat[[2]], "width") <- 1
attr(dat[[2]], "justify") <- "center"
tbl <- create_table(dat, borders = "outside", first_row_blank = TRUE) %>%
titles("Table 1.0", "My Nice Table", borders = c("outside"),
width = "content", font_size = 14, bold = TRUE, blank_row = "none") %>%
footnotes("My footnote 1", "My footnote 2", borders = "outside",
align = "left", width = "content") %>%
define(wt, width = 1, label = "Weight", align = "center",
label_align = "right")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 9, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"),
blank_row = "none") %>%
add_content(tbl, align = "left") %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx16: 9 pt font inches works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test16.docx")
rpt <- create_report(fp, output_type = "DOCX", font_size = 12,
font = "Courier",
orientation = "portrait") %>%
page_header("left", "right") %>%
titles("IRIS Data Frame") %>%
add_content(create_table(iris)) %>%
page_footer("left", "center", "Page [pg] of [tpg]") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx17: 9 pt font cm works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test17.docx")
rpt <- create_report(fp, output_type = "DOCX", font_size = 9,
font = "Courier",
orientation = "portrait") %>%
page_header("left", "right") %>%
titles("IRIS Data Frame") %>%
add_content(create_table(iris)) %>%
page_footer("left", "center", "Page [pg] of [tpg]") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx18: 11 pt font inches works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test18.docx")
tbl <- create_table(iris) %>%
define(Species, page_break = TRUE)
rpt <- create_report(fp, output_type = "DOCX", font_size = 11,
font = "Courier",
orientation = "portrait") %>%
page_header("left", "right") %>%
titles("IRIS Data Frame") %>%
add_content(tbl) %>%
add_content(tbl) %>%
page_footer("left", "center", "Page [pg] of [tpg]") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx19: 11 pt font cm works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test19.docx")
rpt <- create_report(fp, output_type = "DOCX", font_size = 11,
font = "Courier",
orientation = "portrait") %>%
page_header("left", "right") %>%
titles("IRIS Data Frame") %>%
add_content(create_table(iris)) %>%
page_footer("left", "center", "Page [pg] of [tpg]") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx20: JPG Image file works as expected.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test20.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
pltpath <- file.path(base_path, "docx/test20.jpg")
ggsave(pltpath, width = 8, height = 4,
units = "in",
dpi = 300)
plt <- create_plot(pltpath, height = 4, width = 8)
rpt <- create_report(fp, output_type = "DOCX", font = "Arial") %>%
page_header("Client", "Study: XYZ") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center") %>%
footnotes("* Motor Trend, 1974") %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx21: Check content blanks.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test21.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
plt <- create_plot(p, height = 4, width = 7, borders = c("all")) %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none",
font_size = 12, align = "left", blank_row = "none") %>%
footnotes("* Motor Trend, 1974", borders = "none", blank_row = "none")
dat <- mtcars[1:5, ]
attr(dat[[2]], "label") <- "Cylin."
attr(dat[[2]], "width") <- 1
attr(dat[[2]], "justify") <- "center"
tbl <- create_table(dat, borders = c("all"), first_row_blank = TRUE) %>%
titles("Table 1.0", "My Nice Table", borders = c("none"),
width = "content", align = "left", blank_row = "none") %>%
footnotes("My footnote 1", "My footnote 2 Page [pg] of [tpg]",
borders = "none",
align = "left", width = "content", blank_row = "none") %>%
define(wt, width = .75, label = "Weight", align = "center",
label_align = "right")
txt <- create_text(cnt, align = "left", width = 5, borders = c("all")) %>%
titles("Here is my first title", blank_row = "none", borders = "all",
align = "center", font_size = 14) %>%
footnotes("Here is a footnotey", blank_row = "none", borders = "all")
rpt <- create_report(fp, output_type = "DOCX", font = "Times",
font_size = 10, orientation = "portrait") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center", page_break = FALSE) %>%
add_content(tbl, align = "center", page_break = FALSE) %>%
add_content(txt, align = "center", page_break = FALSE, blank_row = "none") %>%
add_content(plt, align = "center", page_break = FALSE, blank_row = "none") %>%
add_content(tbl, align = "center", page_break = FALSE, blank_row = "none") %>%
add_content(txt, align = "center", page_break = FALSE, blank_row = "none") %>%
page_header(c("Left1", "Left2"), "Right") %>%
page_footer("Page [pg] of [tpg]", "Center", "Right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx22: Check titles and footnotes on report.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test22.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
plt <- create_plot(p, height = 4, width = 7, borders = c("all"))
dat <- mtcars[1:5, ]
attr(dat[[2]], "label") <- "Cylin."
attr(dat[[2]], "width") <- 1
attr(dat[[2]], "justify") <- "center"
tbl <- create_table(dat, borders = c("all"), first_row_blank = TRUE) %>%
define(wt, width = .75, label = "Weight", align = "center",
label_align = "right")
txt <- create_text(cnt, align = "left", width = 5, borders = c("all"))
rpt <- create_report(fp, output_type = "DOCX", font = "Times",
font_size = 10, orientation = "portrait") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center", page_break = TRUE) %>%
add_content(tbl, align = "center", page_break = TRUE) %>%
add_content(txt, align = "center", page_break = TRUE) %>%
page_header(c("Left1", "Left2"), "Right") %>%
page_footer("Page [pg] of [tpg]", "Center", "Right") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none",
font_size = 12, align = "left", blank_row = "none") %>%
footnotes("* Motor Trend, 1974", borders = "none", blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 3)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx23: Preview works as expected.", {
fp <- file.path(base_path, "docx/test23.docx")
dat <- iris
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Irises", "Another Title") %>%
footnotes("My footnote 1", "My footnote 2")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial",
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt, preview = 2)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 2)
})
test_that("docx24: Header_bold works as expected", {
dat <- mtcars[1:10, 1:3]
fp <- file.path(base_path, "docx/test24.docx")
tbl <- create_table(dat, header_bold = TRUE, borders = "all") %>%
column_defaults(width = 1) %>%
titles("Report 1.0", "Simple Report", borders = "outside",
blank_row = "none", bold = TRUE) %>%
footnotes("My footnote", blank_row = "none")
rpt <- create_report(fp, orientation = "portrait",
output_type = "DOCX", font = "Arial") %>%
add_content(tbl) %>%
set_margins(top = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docs25: Label row is one cell.", {
fp <- file.path(base_path, "docx/test25.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
ll <- "Here is a super long label to see if it can span the entire table."
# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders = "all") %>%
stub(c("var", "label")) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = ll, cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19) %>%
define(B, label = "Group B", align = "center", n = 13)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = Sys.time(),
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx26: Blank after on invisible column.", {
fp <- file.path(base_path, "docx/test26.docx")
tbl <- create_table(iris, borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX") %>%
page_header("Left", "Right") %>%
add_content(tbl) %>%
page_footer("left", "", "right") %>%
titles("Table 1.0", "IRIS Data Frame",
blank_row = "below") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx27: Page header width works.", {
fp <- file.path(base_path, "docx/test27.docx")
tbl <- create_table(iris[1:10, ], borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
page_header("Left and here is a really long left cell text to put it",
"Right", width = 8) %>%
add_content(tbl) %>%
page_footer("left", "", "right") %>%
titles("Table 1.0", "IRIS Data Frame",
blank_row = "below") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx28: Carriage return in label row works.", {
fp <- file.path(base_path, "docx/test28.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - \n21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
ll <- "Here is a super long label to \nsee if it can span\nthe entire table."
# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders = c("all")) %>%
stub(c("var", "label")) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = ll, cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19) %>%
define(B, label = "Group B", align = "center", n = 13)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx29: Titles and footnotes in header and footer works as expected", {
dat <- mtcars[1:10, 1:3]
fp <- file.path(base_path, "docx/test29.docx")
tbl <- create_table(dat) %>%
column_defaults(width = 1)
rpt <- create_report(fp, orientation = "landscape",
output_type = "DOCX", font = "Arial") %>%
add_content(tbl) %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", "Right") %>%
titles("Report 1.0", "Simple Report",
blank_row = "none", header = TRUE, align = "left") %>%
footnotes("My footnote", "Another footnote", "And another",
blank_row = "none", footer = TRUE) %>%
page_footer("Left", "Center", "Right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx30: Titles and footnotes variations in header and footer work as expected", {
dat <- mtcars[1:10, 1:3]
fp <- file.path(base_path, "docx/test30.docx")
tbl <- create_table(dat) %>%
column_defaults(width = 1)
rpt <- create_report(fp, orientation = "landscape",
output_type = "DOCX", font = "Arial") %>%
add_content(tbl) %>%
set_margins(top = 1) %>%
page_header("Left", "Right") %>%
titles("Report 1.0", "Simple Report", align = "left", width = 6,
blank_row = "none", header = TRUE) %>%
titles("Report 1.0", "Simple Report", align = "right", width = 6,
blank_row = "below", header = TRUE, borders = "bottom") %>%
footnotes("My footnote", blank_row = "none", footer = TRUE, borders = "top") %>%
footnotes("My footnote2", blank_row = "none", footer = TRUE, align = "right") %>%
footnotes("My footnote3", blank_row = "none", footer = TRUE, align = "center") %>%
page_footer("Left", "Center", "Right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx31: Titles and footnotes in header and footer no page header/footer works as expected", {
dat <- mtcars[1:10, 1:3]
fp <- file.path(base_path, "docx/test31.docx")
tbl <- create_table(dat) %>%
column_defaults(width = 1)
rpt <- create_report(fp, orientation = "landscape",
output_type = "DOCX", font = "Arial") %>%
add_content(tbl) %>%
set_margins(top = 1, bottom = 1) %>%
titles("Report 1.0 here is a big long title", "Simple Report", align = "left",
blank_row = "none", header = TRUE) %>%
titles("Report 1.0", "Simple Report", align = "center", width = 6,
blank_row = "below", header = TRUE) %>%
footnotes("My footnote1", "My footnote2", blank_row = "none", footer = TRUE)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx32: Title columns work 1 column.", {
fp <- file.path(base_path, "docx/test32.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame3",
blank_row = "both", columns = 1, align = "center",
borders = c("outside"))
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx33: Title columns work 2 columns.", {
fp <- file.path(base_path, "docx/test33.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame", "Left", "Right",
blank_row = c("above", "below"), columns = 2, borders = "outside")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx34: Title columns work 3 columns.", {
fp <- file.path(base_path, "docx/test34.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE)
rght <- paste("Here is a big long text string to see how the automatic",
"wrapping is happening in a reduced size cell on the right.")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier",
font_size = 10) %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame",
" My right thing", "", "Center", rght,
blank_row = "below", columns = 3, borders = "none") %>%
footnotes("Here is a footnote", "And another", "A",
"Here is a longer footnote to see if I can figure out the alignment pattern.",
align = "right")
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx35: Multiple title blocks work as expected.", {
fp <- file.path(base_path, "docx/test35.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
titles("Table 1.0", "IRIS Data Frame",
blank_row = "below", columns = 1, align = "center", width = 7,
borders = "all") %>%
titles("Table 2.0", "IRIS Data Frame2", "Left", "Right",
blank_row = "below", columns = 2, borders = "all") %>%
titles("Table 3.0", "IRIS Data Frame3", "My right thing", "", "Center",
blank_row = "below", columns = 3, borders = "all") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx36: Custom page size works as expected.", {
fp <- file.path(base_path, "docx/test36.docx")
tbl <- create_table(iris[1:15, ]) %>%
define(Species, visible = FALSE)
ttl <- c("Title1", "Title2", "Title3")
rpt <- create_report(fp, output_type = "DOCX",
font = "Courier",
paper_size = c(6.5, 7.5),
orientation = "portrait") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
titles(ttl,
blank_row = "below", columns = 1, align = "center",
borders = "none") %>%
footnotes("Here is a footnote", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx37: Basic cell style bold works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test37.docx")
dat <- mtcars[, 1:5]
dat$hpflg <- ifelse(dat$hp > 100, TRUE, FALSE)
tbl <- create_table(dat, width = 7) %>%
titles("My title") %>%
column_defaults(width = .75) %>%
define(mpg, width = 1, style = cell_style(bold = TRUE)) %>%
define(cyl, width = 1) %>%
define(disp, style = cell_style(bold = TRUE)) %>%
define(hp, style = cell_style(bold = TRUE, indicator = hpflg)) %>%
define(hpflg, visible = FALSE) %>%
footnotes("My footnotes", blank_row = "none")
rpt <- create_report(fp, output_type = "DOCX",
font = "Arial", orientation = "portrait") %>%
add_content(tbl) %>%
footnotes("Here", footer = TRUE)
res <- write_report(rpt)
# file.show(res$modified_path)
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx38: Bolding works with stub.", {
fp <- file.path(base_path, "docx/test38.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
df$cylflg <- ifelse(df$var == "cyl", TRUE, FALSE)
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
column_defaults(vars = c("stub", "A"),
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
stub(c("var", "label"),
style = cell_style(bold = TRUE, indicator = "labelrow")) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = "Miles Per Gallon", cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19,
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
define(B, label = "Group B", align = "center", n = 13,
style = cell_style(bold = TRUE, indicator = "datarow")) %>%
define(cylflg, visible = FALSE)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
# file.show(res$modified_path)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx38b: Bolding lines wrap works with stub.", {
fp <- file.path(base_path, "docx/test38b.docx")
# Width calculation should be correct for bold text. No overflow
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N and more and more" "19" "13"
"ampg" "Mean and more and more" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median and more and more" "16.4" "21.4"
"ampg" "Q1 - Q3 and more and more" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range and more and more" "10.4 - 33.9" "14.7 - 38.4"
"cyl" "8 Cylinder and more and more" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder and more and more" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder and more and more" "5 ( 26.3%)" "6 ( 46.2%)"')
df$cylflg <- ifelse(df$var == "cyl", T, FALSE)
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
column_defaults(vars = c("stub", "A"),
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
stub(c("var", "label"),
style = cell_style(bold = TRUE, indicator = "datarow")) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = "Miles Per Gallon", cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19,
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
define(B, label = "Group B", align = "center", n = 13,
style = cell_style(bold = TRUE, indicator = "datarow")) %>%
define(cylflg, visible = FALSE)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "docx",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
# file.show(res$modified_path)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx38c: Bolding works with fix width.", {
fp <- file.path(base_path, "docx/test38c.docx")
# With fixed width, bold text should be wrapped correct. No overflow
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N and more and more" "19" "13"
"ampg" "Mean and more and more" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median and more and more" "16.4" "21.4"
"ampg" "Q1 - Q3 and more and more" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range and more and more" "10.4 - 33.9" "14.7 - 38.4"
"cyl" "8 Cylinder and more and more" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder and more and more" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder and more and more" "5 ( 26.3%)" "6 ( 46.2%)"')
df$cylflg <- ifelse(df$var == "cyl", T, FALSE)
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
stub(c("var", "label"), width = 2.06, style = cell_style(bold = TRUE)) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = "Miles Per Gallon and more bold text to be added to test function",
cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19,
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
define(B, label = "Group B", align = "center", n = 13,
style = cell_style(bold = TRUE, indicator = "datarow")) %>%
define(cylflg, visible = FALSE)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "docx",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974", "* Motor Trend, 1974", "* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
# Footnote position should be the same when no bold, which proves that bold
# line wrapping is correct.
res <- write_report(rpt)
# file.show(res$modified_path)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx39: Bold cell style with column defaults.", {
if (dev) {
fp <- file.path(base_path, "docx/test39.docx")
dat <- mtcars[, 1:5]
dat$hpflg <- ifelse(dat$hp > 100, TRUE, FALSE)
tbl <- create_table(dat) %>%
titles("My title") %>%
column_defaults(width = .75, vars = c("cyl", "disp", "hp"),
style = cell_style(bold=TRUE, indicator = hpflg)) %>%
define(mpg) %>%
define(cyl) %>%
define(disp) %>%
define(hp) %>%
define(hpflg, visible = FALSE) %>%
footnotes("My footnotes", blank_row = "none")
rpt <- create_report(fp, output_type = "DOCX",
font = "Arial", orientation = "portrait") %>%
add_content(tbl) %>%
footnotes("Here", footer = TRUE)
res <- write_report(rpt)
# file.show(res$modified_path)
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx40: Bolding, column defaults, and stub works.", {
fp <- file.path(base_path, "docx/test40.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
df$cylflg <- ifelse(df$var == "cyl", FALSE, TRUE)
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
column_defaults(style = cell_style(bold = TRUE, indicator = cylflg)) %>%
stub(c("var", "label"), width = 2,
style = cell_style(bold = TRUE, indicator = cylflg)) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = "Miles Per Gallon", cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19) %>%
define(B, label = "Group B", align = "center", n = 13,
style = cell_style(bold = TRUE, indicator = "datarow")) %>%
define(cylflg, visible = FALSE)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
# file.show(res$modified_path)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx41: Spanning header bold work as expected.", {
fp <- file.path(base_path, "docx/test41")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c( "none")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = FALSE, bold = TRUE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(drat, gear, "Super Duper\nWrapped Span",
n = 11, level = 2, bold = TRUE) %>%
titles("Table 1.0", "My Nice Table", blank_row = "none",
borders = c("top", "bottom")) %>%
footnotes("My footnote 1", "My footnote 2",
blank_row = "none", borders = c("top", "bottom"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx42: Italic footnotes work as expected.", {
fp <- file.path(base_path, "docx/test42.docx")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c( "none")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = FALSE, bold = FALSE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(drat, gear, "Super Duper\nWrapped Span",
n = 11, level = 2, bold = FALSE) %>%
titles("Table 1.0", "My Nice Table", blank_row = "none",
borders = c("top", "bottom")) %>%
footnotes("My italic footnote1", "My italic footnote2", italics = TRUE,
blank_row = "none", borders = "top") %>%
footnotes("My italic footnote", "My footnote 2",
blank_row = "none", borders = c("bottom"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
})
test_that("docx43: Two plots on report works as expected.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test43.docx")
p1 <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
plt1 <- create_plot(p1, height = 4, width = 8, borders = c("none"))
p2 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
plt2 <- create_plot(p2, height = 4, width = 8, borders = c("none"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size =fsz) %>%
page_header("Client", "Study: XYZ") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt1, align = "center") %>%
add_content(plt2, align = "center") %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none") %>%
footnotes("* Motor Trend, 1974", borders = "none")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 2)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx44: Stub indent.", {
fp <- file.path(base_path, "docx/test44.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder and more perhaps more" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
ll <- "Here is a super long label to see if it can span the entire table."
# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders = c("all")) %>%
stub(c("var", "label"), width = .9) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = ll, cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19) %>%
define(B, label = "Group B", align = "center", n = 13)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx45: Footnote columns work 1 column.", {
fp <- file.path(base_path, "docx/test45.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame3",
blank_row = "both", columns = 1, align = "center",
borders = c("outside")) %>%
footnotes("Here is a footnote", "And another", columns = 1, borders = "all")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
footnotes("Here is a footnote", "And another", columns = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx46: Footnote columns work 2 columns.", {
fp <- file.path(base_path, "docx/test46.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame", "Left", "Right",
blank_row = c("above", "below"), columns = 2,
borders = "outside") %>%
footnotes("Here is a footnote", "And another", columns = 2, borders = "all")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
footnotes("Here is a footnote", "And another", columns = 2)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx47: Footnote columns work 3 columns.", {
fp <- file.path(base_path, "docx/test47.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE) %>%
footnotes("Here is a footnote", "And another", "And more", "", "More",
columns = 3, borders = "all")
rght <- paste("Here is a big long text string to see how the automatic",
"wrapping is happening in a reduced size cell on the right.")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier",
font_size = 10) %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
titles("Table 1.0\nsecond row", "IRIS Data Frame",
" My right thing", "", "Center", rght,
blank_row = "below", columns = 3, borders = "none") %>%
footnotes("Here is a footnote", "And another", "A",
"Here is a longer footnote", "to see if I can figure",
"out the alignment pattern.",
columns = 3)
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx48: Multiple footnote blocks work as expected.", {
fp <- file.path(base_path, "docx/test48.docx")
tbl <- create_table(iris[1:15, ], borders = "all") %>%
define(Species, blank_after = TRUE, visible = FALSE) %>%
footnotes("Here is a footnote", "And another", "And more", "", "More",
columns = 3, borders = "all", blank_row = "both")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left", "right") %>%
page_footer("left", "", "right") %>%
footnotes("Table 1.0", "IRIS Data Frame",
blank_row = "below", columns = 1, align = "center", width = 7,
borders = "all") %>%
footnotes("Table 2.0", "IRIS Data Frame2", "Left", "Right",
blank_row = "below", columns = 2, borders = "all") %>%
footnotes("Table 3.0", "IRIS Data Frame3", "My right thing", "", "Center",
blank_row = "below", columns = 3, borders = "all") %>%
titles("Here is a title", "And another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx49: Greater and Less characters are escaped as expected.", {
fp <- file.path(base_path, "docx/test49.docx")
dt <- iris[1:15, ]
dt$Species <- as.character(dt$Species)
dt[1, "Species"] <- "set > osa"
dt[2, "Species"] <- "set < osa"
dt[3, "Species"] <- "set & osa"
tbl <- create_table(dt, borders = "all") %>%
define(Species, label = "Spe>ies") %>%
footnotes("Here is a > footnote", "And < another", "And & more", "", "More",
columns = 3, borders = "all", blank_row = "both")
rpt <- create_report(fp, output_type = "DOCX", font = "Courier") %>%
add_content(tbl) %>%
page_header("left<", "right&") %>%
page_footer("left>", "", "right<") %>%
footnotes("Table> 1.0", "IRIS Data Frame",
blank_row = "below", columns = 1, align = "center", width = 7,
borders = "all") %>%
footnotes("Table 2.0", "IRIS Data Frame2", "Left", "Right",
blank_row = "below", columns = 2, borders = "all") %>%
footnotes("Table 3.0", "IRIS Data Frame3", "My right thing", "", "Center",
blank_row = "below", columns = 3, borders = "all") %>%
titles("Here is>a title", "And<another")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
})
test_that("docx50: Page by with wrap works as expected.", {
fp <- file.path(base_path, "docx/test50.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat$Pgby <- paste0("Flower Type\n", dat$Pgby)
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Report with a Page By", borders = "none") %>%
page_by(Pgby, label = "Species: ", align = "right", borders = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_header("Left", "Right") %>%
page_footer("Left1", "Center1", "Right1") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 9)
expect_equal(length(res$column_widths[[1]]), 5)
})
test_that("docx51: Page by with format works as expected.", {
fp <- file.path(base_path, "docx/test51.docx")
fmt1 <- c(setosa = 1, versicolor = 2, virginica = 3)
fmt2 <- value(condition(x == 1, "Setosa"),
condition(x == 2, "Versicolor"),
condition(x == 3, "Virginica"))
dat <- iris
fmtval <- fmt1[dat$Species]
names(fmtval) <- NULL
dat$Pgby <- fmtval
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Report with a Page By", borders = "none") %>%
page_by(Pgby, align = "left", label = "Flower:", borders = "none", format = fmt2) %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX",
orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_header("Left", "Right") %>%
page_footer("Left1", "Center1", "Right1") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 6)
expect_equal(length(res$column_widths[[1]]), 5)
})
test_that("docx52: Borders work as expected.", {
fp <- file.path(base_path, "docx/test52.docx")
fmt1 <- c(setosa = 1, versicolor = 2, virginica = 3)
fmt2 <- value(condition(x == 1, "Setosa"),
condition(x == 2, "Versicolor"),
condition(x == 3, "Virginica"))
dat <- iris
fmtval <- fmt1[dat$Species]
names(fmtval) <- NULL
dat$Pgby <- fmtval
tbl <- create_table(dat, borders = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX",
orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with a Page By", borders = "none") %>%
page_by(Pgby, align = "left", label = "Flower:", borders = c("top", "bottom"),
format = fmt2, blank_row = "both") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_header("Left", "Right") %>%
page_footer("Left1", "Center1", "Right1") %>%
footnotes("My footnote 1", "My footnote 2", borders = c("top", "bottom"),
columns = 2, blank_row = "both") %>%
footnotes("hello", borders = "none", blank_row = "none")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 9)
expect_equal(length(res$column_widths[[1]]), 5)
})
test_that("docx53: Page by with wrap on report works as expected.", {
fp <- file.path(base_path, "docx/test53.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat$Pgby <- paste0("Flower Type\n", dat$Pgby)
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Report with a Page By", borders = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_by(Pgby, label = "Species: ", align = "right", borders = "none") %>%
page_header("Left", "Right") %>%
page_footer("Left1", "Center1", "Right1") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 9)
expect_equal(length(res$column_widths[[1]]), 5)
})
# Not right yet. Can't reproduce problem.
test_that("docx55: Top margin 1.5 works as expected.", {
fp <- file.path(base_path, "docx/test55.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat$Pgby <- paste0("Flower Type\n", dat$Pgby)
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Report with a Page By", borders = "none") %>%
page_by(Pgby, label = "Species: ", align = "right", borders = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "DOCX", font = "Courier",
font_size = 9, orientation = "landscape") %>%
set_margins(top = 1.5, bottom = 1) %>%
add_content(tbl) %>%
page_header("Left", "Right") %>%
page_footer("Left1", "Center1", "Right1") %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 6)
expect_equal(length(res$column_widths[[1]]), 5)
})
test_that("docx-56: Multi page table removes blank spaces.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test56.docx")
dat1 <- iris[1:10, ]
dat2 <- iris[11:20, ]
dat3 <- iris[21:30, ]
tbl1 <- create_table(dat1, borders = "none") %>%
titles("Table 1.0", "My Nice Irises1")
tbl2 <- create_table(dat2, borders = "none") %>%
titles("Table 1.0", "My Nice Irises2")
tbl3 <- create_table(dat3, borders = "none") %>%
titles("Table 1.0", "My Nice Irises3")
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl1, blank_row = "none") |>
add_content(tbl2, blank_row = "none") |>
add_content(tbl3, blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 3)
} else
expect_equal(TRUE, TRUE)
})
# No errors, but Word converts the EMG to JPG automatically.
# Even if you just insert in manually.
test_that("docx-57: EMF Image file works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test57.docx")
pltpath <- file.path(base_path, "data/example10.emf")
plt <- create_plot(pltpath, height = 4, width = 8)
rpt <- create_report(fp, output_type = "DOCX", font = "Arial") %>%
page_header("Client", "Study: XYZ") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center") %>%
footnotes("* Motor Trend, 1974") %>%
page_footer("Time", "Confidential", "Page [pg] of [tpg]")
res <- write_report(rpt)
#print(res)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-58: Spanning header gap works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test58.docx")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c("outside")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = TRUE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(cyl, hp, "Super Span", n = 11, level = 2) |>
spanning_header(drat, gear, "Super Duper\nWrapped Span", n = 11, level = 2)
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
titles("Table 1.0", "My Nice Table") %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2") %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_true(TRUE)
}
})
test_that("docx-58-2: Spanning header gap should not exist with all border.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test58-2.docx")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c("all")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = TRUE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(cyl, hp, "Super Span", n = 11, level = 2) |>
spanning_header(drat, gear, "Super Duper\nWrapped Span", n = 11, level = 2)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
titles("Table 1.0", "My Nice Table") %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2") %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-58-3: Spanning header gap should not exist with inside border.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test58-3.docx")
dat <- mtcars[1:15, ]
tbl <- create_table(dat, borders = c("inside")) %>%
spanning_header(cyl, disp, "Span 1", label_align = "left") %>%
spanning_header(hp, wt, "Span 2", underline = TRUE) %>%
spanning_header(qsec, vs, "Span 3", n = 10) %>%
spanning_header(cyl, hp, "Super Span", n = 11, level = 2) |>
spanning_header(drat, gear, "Super Duper\nWrapped Span", n = 11, level = 2)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
titles("Table 1.0", "My Nice Table") %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2") %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
res
res$column_widths
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-58-4: Spanning header with invisible columns works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test58-4.docx")
dat <- mtcars[1:20,]
tbl <- create_table(dat, borders = c("all")) %>%
spanning_header(mpg, qsec, "Span 1", label_align = "left") %>%
spanning_header(vs, carb, "Span 2", underline = TRUE) %>%
define(cyl, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Right3"), blank_row = "below") %>%
titles("Table 1.0", "My Nice Table") %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2") %>%
page_footer("Left1", "Center1", "Right1")
# Make sure spanning header width is matched.
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-59: Table with blank_before works as expected.", {
fp <- file.path(base_path, "docx/test59.docx")
# Setup
subjid <- 100:109
name <- c("Quintana, Gabriel", "Allison, Blas", "Minniear, Presley",
"al-Kazemi, Najwa", "Schaffer, Ashley", "Laner, Tahma",
"Perry, Sean", "Crews, Deshawn Joseph", "Person, Ladon here is some more",
"Smith, Shaileigh and \nmore and more and even more and more and more")
sex <- c("M", "F", "F", "M", "M", "F", "M", "F", "F", "M")
age <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
arm <- c(rep("A", 5), rep("B", 5))
# Create data frame
df <- data.frame(subjid, name, sex, age, arm)
tbl1 <- create_table(df, first_row_blank = FALSE, borders = "all") %>%
define(subjid, label = "Subject ID", align = "left", width = 1) %>%
define(name, label = "Subject Name", width = 1) %>%
define(sex, label = "Sex") %>%
define(age, label = "Age") %>%
define(arm, label = "Arm",
blank_before = TRUE,
dedupe = TRUE,
align = "right") #%>%
# spanning_header(sex, arm, label = "Here is a spanning header")
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size = fsz) %>%
page_header(left = "Experis", right = c("Study ABC", "Status: Closed")) %>%
# options_fixed(line_count = 46) %>%
titles("Table 1.0", "Analysis Data Subject Listing\n And more stuff",
"Safety Population", align = "center", bold = TRUE) %>%
footnotes("Program Name: table1_0.R",
"Here is a big long footnote that is going to wrap\n at least once") %>%
page_footer(left = "Time", center = "Confidential",
right = "Page [pg] of [tpg]") %>%
add_content(tbl1)
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
})
test_that("docx-60: Three level stub and indentation work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test60.docx")
# Setup
cat <- c(rep("Kaplan-Meier estimates", 6),
rep("Cox PH estimates and some more really long stuff", 6))
grp <- c("25th percentile", "25th percentile",
"median (weeks)", "median (weeks)",
"75th percentile", "75th percentile",
"25th percentile", "25th percentile",
"median (weeks)", "median (weeks)",
"75th percentile", "75th percentile")
ci <- c(NA, "95% confidence interval",
NA, "95% confidence interval",
NA, "95% confidence interval",
NA, "95% confidence interval",
NA, "95% confidence interval",
NA, "95% confidence interval")
values <- c(41, 53, 43, 39, 47, 52, 38, 25, 37, 23, 78, 21)
# Create data frame
df <- data.frame(cat, grp, ci, values, stringsAsFactors = FALSE)
tbl1 <- create_table(df) %>%
stub(c(cat, grp, ci), "Estimates", width = 1.5) %>%
define(cat, label_row = TRUE, blank_after = TRUE, indent = 0.15) %>%
define(grp, indent = .25) %>%
define(ci, indent = .5) %>%
define(values, label = "Values")
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz) %>%
titles("Table 3.0", "Analysis of Time to Initial PSGA Success in Weeks") %>%
page_header("Sponsor", "Study") %>%
add_content(tbl1) %>%
page_footer("Time", "Confidential", "Page")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-61: Test 5 inch stub works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test61.docx")
# Setup
subjid <- 100:109
name <- c("Quintana, Gabriel", "Allison, Blas", "Minniear, Presley",
"al-Kazemi, Najwa", "Schaffer, Ashley", "Laner, Tahma",
"Perry, Sean", "Crews, Deshawn Joseph", "Person, Ladon here is some more",
"Smith, Shaileigh and \nmore and more and even more and more and more")
sex <- c("M", "F", "F", "M", "M", "F", "M", "F", "F", "M")
age <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
arm <- c(rep("A", 5), rep("B", 5))
# Create data frame
df <- data.frame(subjid, name, sex, age, arm)
tbl1 <- create_table(df, first_row_blank = FALSE, borders = "all") %>%
define(subjid, label = "Subject ID", align = "left", width = 5) %>%
define(name, label = "Subject Name", width = 1) %>%
define(sex, label = "Sex") %>%
define(age, label = "Age") %>%
define(arm, label = "Arm",
blank_before = TRUE,
dedupe = TRUE,
align = "right") #%>%
# spanning_header(sex, arm, label = "Here is a spanning header")
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size = fsz) %>%
page_header(left = "Experis", right = c("Study ABC", "Status: Closed")) %>%
# options_fixed(line_count = 46) %>%
titles("Table 1.0", "Analysis Data Subject Listing\n And more stuff",
"Safety Population", align = "center", bold = TRUE) %>%
footnotes("Program Name: table1_0.R",
"Here is a big long footnote that is going to wrap\n at least once") %>%
page_footer(left = "Time", center = "Confidential",
right = "Page [pg] of [tpg]") %>%
add_content(tbl1)
res <- write_report(rpt)
res
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-62: Page footers with one assigned width work as expected.",{
if (dev) {
fp <- file.path(base_path, "docx/test62.docx")
dat <- mtcars[,c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs")]
tbl <- create_table(dat, borders = "outside") %>%
footnotes("This is testing footnote", blank_row = "none")
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz) %>%
set_margins(top = 1, bottom = 1) %>%
titles("Table 1.0") %>%
add_content(tbl) %>%
page_footer(left = "this is a very long sentence whose length exceeds the limitation of the left part",
center = "Center footer", right = "Right footer",
width = c(5.5))
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
}
)
test_that("docx-63: Page footers with multiple assigned widths work as expected.",{
if (dev) {
fp <- file.path(base_path, "docx/test63.docx")
dat <- mtcars[,c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs")]
tbl <- create_table(dat, borders = "outside") %>%
footnotes("This is testing footnote", blank_row = "none")
rpt <- create_report(fp, output_type = "DOCX", font = fnt,
font_size = fsz) %>%
set_margins(top = 1, bottom = 1) %>%
titles("Table 1.0") %>%
add_content(tbl) %>%
page_footer(left = "this is a very long sentence whose length exceeds the limitation of the left part",
center = "", right = "Right Footer",
width = c(5.5, 0 , 2))
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
}
)
test_that("docx-64: Page by with bold label and value works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test64.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = "Flower Type: ", bold = TRUE, blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-65: Page by with only bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test65.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = "Flower Type: ", bold = "label", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-66: Page by with only bold value works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test66.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = "Flower Type: ", bold = "value", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-67: Page by with long bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test67.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long label with\nmanual line break: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = "label", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
# There is a unknown problem for rs footnote when label takes three lines
# In get_page_footnotes_docx, when len_diff is 0, rs$table_break isn't inserted
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-68: Page by with long non-bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test68.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long label without manual line change and more text: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = "value", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-69: Page by with long bold value works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test69.docx")
dat <- iris
long_value <- paste0(
"This is a very long\nvalue with manual line change and, "
)
dat$Pgby <- paste0(long_value, as.character(dat$Species))
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = "Flower Type: ", bold = "value", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
# When value takes three lines, the footnote will exceed a page because
# get_content_offsets_docx only consider label lines
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-70: Page by with long label filling one line works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test70.docx")
dat <- iris
dat$Pgby <- as.character(dat$Species)
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long label without manual line change: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = "value", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
# Below unit tests are commented out because two reasons:
# (1) When page_by takes too many lines, it causes len_diff is 0 in get_page_footnotes_docx,
# so rs$table_break isn't inserted, which let page footer disable.
# (2) When value takes multi-lines, the footnote will exceed a page because
# get_content_offsets_docx only consider label lines
# Because multi-line page by is rare, hold this issue and see if it's really
# necessary to fix in the future.
test_that("docx-71: Page by with long non-bold value works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test71.docx")
dat <- iris
long_value <- paste0(
"This is a very long value without manual line change and more text, "
)
dat$Pgby <- paste0(long_value, as.character(dat$Species))
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = "Flower Type: ", bold = "label", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-72: Page by with very long non-bold value and long bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test72.docx")
dat <- iris
long_value <- paste0(
"This is a very long value without intentionally line change and also",
" long text which should take at least three lines, "
)
dat$Pgby <- paste0(long_value, as.character(dat$Species))
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long\nlabel with intentionally line change and also",
" long text which should take at least three lines: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = "label", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-73: Page by with very long bold value and long non-bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test73.docx")
dat <- iris
long_value <- paste0(
"This is a very long value without intentionally line change and also",
" long text which should take at least three lines, "
)
dat$Pgby <- paste0(long_value, as.character(dat$Species))
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long\nlabel with intentionally line change and also",
" long text which should take at least three lines: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = "value", blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-74: Page by with very long bold value and long bold label works as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test74.docx")
dat <- iris
long_value <- paste0(
"This is a very long value without intentionally line change and also",
" long text which should take at least three lines, "
)
dat$Pgby <- paste0(long_value, as.character(dat$Species))
dat <- dat[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Pgby")]
long_label <- paste0(
"This is a very long\nlabel with intentionally line change and also",
" long text which should take at least three lines: "
)
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a Page By") %>%
page_by(Pgby, label = long_label, bold = TRUE, blank_row = "none") %>%
define(Pgby, visible = FALSE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-75: Group border works as as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test75.docx")
# Setup
arm <- c(rep("A", 3), rep("B", 2), rep("C", 3), rep("D", 2))
subjid <- 100:109
name <- c("Quintana, Gabriel", "Allison, Blas", "Minniear, Presley",
"al-Kazemi, Najwa \nand more and more", "Schaffer, Ashley", "Laner, Tahma",
"Perry, Sean", "Crews, Deshawn Joseph", "Person, Ladon",
"Smith, Shaileigh")
sex <- c("M", "F", "F", "M", "M", "F", "M", "F", "F", "M")
age <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
# Create data frame
df <- data.frame(arm, subjid, name, sex, age, stringsAsFactors = FALSE)
df <- rbind(df, df, df, df)
tbl1 <- create_table(df, first_row_blank = FALSE, borders = "outside") %>%
define(subjid, label = "Subject ID for a patient", n = 10, align = "left",
width = 1) %>%
define(name, label = "Subject Name", width = 1) %>%
define(sex, label = "Sex", n = 10, align = "center") %>%
define(age, label = "Age", n = 10) %>%
define(arm, label = "Arm",
dedupe = TRUE,
group_border = TRUE) %>%
footnotes("This is the footnote")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz) %>%
titles(c("Table 1.0", "This is a table with group border"), align = "center") %>%
add_content(tbl1) %>%
footnotes(c("This is the footnote 1")) %>%
page_header(left = "Test header", right = "Test header") %>%
page_footer(left = "Test footer", right = "Test footer") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-76: Group border with blank after works as as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test76.docx")
# Setup
arm <- c(rep("A", 3), rep("B", 2), rep("C", 3), rep("D", 2))
subjid <- 100:109
name <- c("Quintana, Gabriel", "Allison, Blas", "Minniear, Presley",
"al-Kazemi, Najwa \nand more and more", "Schaffer, Ashley", "Laner, Tahma",
"Perry, Sean", "Crews, Deshawn Joseph", "Person, Ladon",
"Smith, Shaileigh")
sex <- c("M", "F", "F", "M", "M", "F", "M", "F", "F", "M")
age <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
# Create data frame
df <- data.frame(arm, subjid, name, sex, age, stringsAsFactors = FALSE)
df <- rbind(df, df, df, df)
tbl1 <- create_table(df, first_row_blank = FALSE, borders = "outside") %>%
define(subjid, label = "Subject ID for a patient", n = 10, align = "left",
width = 1) %>%
define(name, label = "Subject Name", width = 1) %>%
define(sex, label = "Sex", n = 10, align = "center") %>%
define(age, label = "Age", n = 10) %>%
define(arm, label = "Arm",
dedupe = TRUE,
blank_after = TRUE,
group_border = TRUE) %>%
footnotes("This is the footnote")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz) %>%
titles(c("Table 1.0", "This is a table with group border"), align = "center") %>%
add_content(tbl1) %>%
footnotes(c("This is the footnote 1")) %>%
page_header(left = "Test header", right = "Test header") %>%
page_footer(left = "Test footer", right = "Test footer") %>%
set_margins(top = 1, bottom = 1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-77: Center page header works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test77.docx")
dat <- iris[1:50,]
dat <- dat[, c("Species" ,"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
tbl <- create_table(dat, borders = "outside")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with a center header", header = T) %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none") %>%
page_header(left = "", center = "center", right = "right",
width = c(0, 5, 4)) %>%
page_footer(left = "Left", center = "center", right = "right",
width = c(3, 4, 2))
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-78: Images in page header/footer work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test78.docx")
dat <- iris[1:50,]
dat <- dat[, c("Species" ,"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
image_path <- file.path(base_path, "data/logo.jpg")
image_path2 <- file.path(base_path, "data/logo.png")
tbl <- create_table(dat, borders = "outside") %>%
titles("Table 1.0", "My Nice Report with a page header picture")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none") %>%
page_header() %>%
header_image(image_path, height = 0.5, width = 0.8, align = "left") %>%
header_image(image_path, height = 0.6, width = 0.85, align = "right") %>%
header_image(c(image_path, image_path2), height = 0.38, width = 0.7, align = "center") %>%
page_footer() %>%
footer_image(c(image_path2, image_path), height = 0.38, width = 0.7, align = "left") %>%
footer_image(image_path, height = 0.5, width = 0.8, align = "right") %>%
footer_image(image_path, height = 0.6, width = 0.85, align = "center")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-79: Spanned images in page header/footer work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test79.docx")
dat <- iris[1:50,]
dat <- dat[, c("Species" ,"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
image_path <- file.path(base_path, "data/span_logo.jpg")
tbl <- create_table(dat, borders = "outside")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with a page header picture", header = T) %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none") %>%
page_header(width = c(0, 9, 0), left = "No display because width is 0") %>%
header_image(image_path, height = 1, width = 7.5, align = "centre") %>%
page_footer(width = c(0, 0, 9), center = "No display because width is 0") %>%
footer_image(image_path, height = 0.8, width = 7, align = "right")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-80: Plot with header/footer images works as expected.", {
if (dev == TRUE) {
library(ggplot2)
fp <- file.path(base_path, "docx/test80.docx")
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
image_path <- file.path(base_path, "data/logo.jpg")
plt <- create_plot(p, height = 4, width = 8, borders = c("none"))
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size =fsz) %>%
page_header("Client", "Study: XYZ") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(plt, align = "center") %>%
page_header(left = "left Header") %>%
header_image(image_path, height = 0.5, width = 0.8, align = "right") %>%
page_footer(right = "Page [pg] of [tpg]") %>%
footer_image(image_path, height = 0.38, width = 0.7, align = "left") %>%
titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none") %>%
footnotes("* Motor Trend, 1974", borders = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
# expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-81: Return error when header_image/footer_image is used without page_header/page_footer.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test81.docx")
dat <- iris[1:50,]
dat <- dat[, c("Species" ,"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
image_path <- file.path(base_path, "data/span_logo.png")
tbl <- create_table(dat, borders = "outside")
# page_header is not set
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with a page header picture", header = T) %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none") %>%
header_image(image_path, height = 1, width = 7.5, align = "centre")
expect_error(
res <- write_report(rpt),
"`page_header` must be used when using `header_image`."
)
# page_footer is not set
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with a page header picture", header = T) %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none") %>%
footer_image(image_path, height = 0.8, width = 7, align = "right")
expect_error(
res <- write_report(rpt),
"`page_footer` must be used when using `footer_image`."
)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-82: group_cohesion puts the group in same page as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test82.docx")
dat <- iris[1:86,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 31)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 6), # 12 lines + 1
c(rep("Flower C", 19)), # 19 lines + 1
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E", 31)) # 31 lines + 1
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, group_cohesion = T,
label = "Group", blank_after = T)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Group Cohesion") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-83: group_cohesion with min page proportion works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test83.docx")
dat <- iris[1:86,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 31)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 6), # 12 lines + 1
c(rep("Flower C", 19)), # 19 lines + 1
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E", 31)) # 31 lines + 1
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, group_cohesion = 0.6,
label = "Group", blank_after = T) %>%
define(test_string, label = "Flower")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Group Cohesion") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-84: Multiple group_cohesion work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test84.docx")
dat <- iris[1:86,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 31)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 3), # 12 lines + 1
rep("Flower B\nSubgroup B2", 3),
c(rep("Flower C1", 11)), # C1 ends at 24 lines, which is the minimum requirement
c(rep("Flower C2", 8)), # C2 would be moved to next page
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E1", 24), rep("Flower E2", 7)) # E2 would be moved to next page
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, group_cohesion = TRUE, label = "Group", blank_after = T) %>%
define(test_string, group_cohesion = TRUE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Group Cohesion") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
# C2 would be moved to next page
# E2 would be moved to next page
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-85: Multiple group_cohesion with different cohesion strengths work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test85.docx")
dat <- iris[1:89,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 34)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 6), # 12 lines + 1
c(rep("Flower C1", 11)), # C1 ends at 24 lines, which is the minimum requirement
c(rep("Flower C2", 8)), # C2 would be not moved to next page
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E1", 30), rep("Flower E2", 4)) # E2 would be moved to next page
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, group_cohesion = TRUE, label = "Group", blank_after = T) %>%
define(test_string, group_cohesion = 0.1)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Group Cohesion") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
# C2 would not be moved to next page because previous lines are only 24
# E2 would be moved to next page because previous lines are 27, which meets 30*0.9
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-86: break_label works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test86.docx")
dat <- iris[1:86,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 31)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 6), # 12 lines + 1
c(rep("Flower C", 19)), # 19 lines + 1
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E", 31)) # 31 lines + 1
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, break_label = "(Continued)", blank_before = T) %>%
define(test_string,
break_label = paste0("(This is a long text which should take more ",
"than one line. It's to test if the package is able to handle this ",
"situation.)"),
indent = 0.16)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Break Label") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-86b: Bold break_label works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test86b.docx")
dat <- iris[1:86,]
dat$test_group <- c(
rep("A", 12),
rep("B", 6),
rep("C", 19),
rep("D", 18),
rep("E", 31)
)
dat$test_string <- c(
rep("Flower A\nSubgroup A1", 12), # 24 lines + 1
rep("Flower B\nSubgroup B1", 6), # 12 lines + 1
c(rep("Flower C", 19)), # 19 lines + 1
c(rep("Flower D", 16), rep("Flower D\nSubgroup D1", 2)), # 20 lines + 1
c(rep("Flower E", 31)) # 31 lines + 1
)
dat <- dat[, c("test_group", "test_string", "Sepal.Length",
"Sepal.Width", "Petal.Length","Petal.Width")]
tbl <- create_table(dat, borders = "outside") %>%
define(test_group, break_label = "(Continued)", blank_before = T) %>%
define(test_string,
break_label = paste0("(This is a long text which should take more ",
"than one line. It's to test if the package is able to handle this ",
"situation.)"),
indent = 0.16, style = cell_style(bold = T))
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
titles("Table 1.0", "My Nice Report with Break Label") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
footnotes("My footnote 1", "", "My footnote 2", borders = "none",
blank_row = "none")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-87: Page numbers work in every title, footnote, header, and footer.", {
if (dev) {
fp <- file.path(base_path, "docx/test87.docx")
dat <- iris
tbl <- create_table(dat, borders = "outside") %>%
titles("Table Title: Page [pg] of [tpg]", "", "Third table title") %>%
footnotes("Table footnote: Page [pg] of [tpg]", "", "Third table footnote")
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = fsz, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
add_content(tbl) %>%
titles("Report Title: Page [pg] of [tpg]", "Second report title") %>%
# footnotes("Report footnote: Page [pg] of [tpg]", "Second report footnote",
# borders = "none") %>%
footnotes("Report footnote: Page [pg] of [tpg]",
borders = "none") %>%
page_header("Header: Page [pg] of [tpg]") %>%
page_footer("Footer: Page [pg] of [tpg]")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-88: Page wrap can be turned off as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test88.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
USUBJID STUDYID DOMAIN SUBJID RFSTDTC RFENDTC RFXSTDTC RFXENDTC RFICDTC
"001" "ABC" "DM" "01" "2021-12-01" "2021-12-20" "2021-12-02" "2021-12-20" "2021-12-01"
"002" "ABC" "DM" "02" "2021-12-02" "2021-12-21" "2021-12-03" "2021-12-21" "2021-12-02"
"003" "ABC" "DM" "03" "2021-12-03" "2021-12-22" "2021-12-04" "2021-12-22" "2021-12-03"
"004" "ABC" "DM" "04" "2021-12-04" "2021-12-23" "2021-12-05" "2021-12-23" "2021-12-04"
"005" "ABC" "DM" "05" "2021-12-05" "2021-12-24" "2021-12-06" "2021-12-24" "2021-12-05"
"006" "ABC" "DM" "06" "2021-12-06" "2021-12-25" "2021-12-07" "2021-12-25" "2021-12-06"
"007" "ABC" "DM" "07" "2021-12-07" "2021-12-26" "2021-12-08" "2021-12-26" "2021-12-07"
"008" "ABC" "DM" "08" "2021-12-08" "2021-12-27" "2021-12-09" "2021-12-27" "2021-12-08"')
# Test that any assigned formats are applied
# attr(df$SUBJID, "width") <- 1
attr(df$SUBJID, "justify") <- "left"
attr(df$SUBJID, "format") <- "S:%s"
# Define table
tbl <- create_table(df, page_wrap = FALSE) |>
titles("page_wrap = FALSE in create_table()")
tbl2 <- create_table(df) |>
titles("page_wrap = FALSE in report_options()")
tbl3 <- create_table(df, page_wrap = TRUE) |>
titles("page_wrap = TRUE in report_options()")
# Define Report
# !! Please note that when `page_wrap` is set in `create_table`. It'll be the top choice
# even though we set TRUE in `report_options`. Therefore, table 1 doesn't have
# the page wrap, but table 2 have the page wrap.
rpt <- create_report(fp, font = "Arial", font_size = 10, units = "cm",
orientation = "portrait") %>%
report_options(page_wrap = FALSE) %>%
titles("Listing 1.0",
"Demographics Dataset") %>%
add_content(tbl, align = "left") %>%
add_content(tbl2, align = "left") %>%
add_content(tbl3, align = "left") %>%
page_header("Sponsor", "Drug") %>%
page_footer(left = "Time", right = "Page [pg] of [tpg]") %>%
footnotes("My footnote")
# If page is wrapped, RFICDTC will be in the next page
# Now page is not wrapped, we can see RFICDTC is out of the margin.
res <- write_report(rpt, output_type = "docx")
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-89: Line break can be turned off by report_options as expected.", {
if (dev) {
fp <- file.path(base_path, "docx/test89.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder and more perhaps more" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
ll <- "Here is a label\nwith manual line break."
df$sub_title <- "This is a long page by value which would not be inserted any line break characters."
# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders = c("all")) %>%
stub(c("var", "label"), width = .8) %>%
page_by(sub_title, label = "This is a long label which would make multiple lines:",
bold = "label") %>%
define(sub_title, visible = FALSE) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = ll, cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center") %>%
define(B, label = "Group B", align = "center")
# Create report and add content
# !! Please note that `line_break` in `report_options` would affect:
# 1. All the data content of tables
# 2. All the titles and footnotes in both table objects and report objects
# 3. All page headers and footers
rpt <- create_report(fp, orientation = "portrait", output_type = "docx",
font = "Arial") %>%
report_options(line_break = FALSE) %>%
page_header(left = "This is a long left header which would not be inserted any line break characters.",
right = "This is a long right header which would not be inserted any line break characters.") %>%
titles("Table 1.0",
paste0("This is a title which is turned off the automatically line breaks",
" so it should not contain any line break characters and it should",
" have overflow because the wrapping lines are not considered.")) %>%
add_content(tbl) %>%
footnotes(paste0("This is a long footnote which would not be inserted any line break characters even though it",
" has multiple lines. Users should be responsible for the overflow.")) %>%
page_footer(left = "This is a long left footer which would not be inserted any line break characters.",
center = "This is a long center footer which would not be inserted any line break characters.",
right = "This is a long right footer which would not be inserted any line break characters.")
# The output should be overflow because the line-break characters are not inserted
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-90: auto_page can be turned off as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test90.docx")
dat <- iris[1:100, ]
dat$Species <- as.character(dat$Species)
dat$Species[1:25] <- rep("setosa1", 25)
dat$Species[26:50] <- rep("setosa2", 25)
tbl <- create_table(dat, auto_page = FALSE) %>%
footnotes("First Table with auto_page=FALSE in create_table", "My footnote 2", valign = "bottom") %>%
define(Species, page_break = TRUE)
tbl2 <- create_table(dat) %>%
footnotes("Second Table with auto_page=FALSE in report_options", "My footnote 2", valign = "bottom") %>%
define(Species, page_break = TRUE)
tbl3 <- create_table(dat, auto_page = TRUE) %>%
footnotes("Third Table with auto_page=TRUE in create_table", "My footnote 2", valign = "bottom") %>%
define(Species, page_break = TRUE)
rpt <- create_report(fp, output_type = "docx", font = "Arial",
font_size = 10, orientation = "landscape") %>%
report_options(auto_page = FALSE) %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"), blank_row = "below") %>%
titles("Table 1.0", "My Nice Table") %>%
add_content(tbl) %>%
add_content(tbl2) %>%
add_content(tbl3) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
# Please note that res$pages would be 3 instead of 4 because versicolor is overflow
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-91: line_count can be set in report_options as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test91.docx")
dat <- iris[1:100, ]
tbl <- create_table(dat) %>%
footnotes("line_count setting in report_options()", "My footnote 2", valign = "bottom")
rpt <- create_report(fp, output_type = "docx", font = "Arial",
font_size = 10, orientation = "landscape") %>%
report_options(line_count = 18) %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"), blank_row = "below") %>%
titles("Table 1.0", "Table with line_count = 18") %>%
add_content(tbl) %>%
page_footer("Left1", "Center1", "Right1")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-92: Empty page_by works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test92.docx")
# Prepare data
dat <- mtcars[order(mtcars$cyl), ]
dat <- data.frame(vehicle = rownames(dat), dat)
dat$cyl[1] <- ""
dat$cyl[2] <- NA
# Define table
tbl <- create_table(dat, show_cols = 1:8) %>%
page_by(cyl, label="Cylinders: ")
# Create the report
rpt <- create_report(fp, output_type = "docx",
font = "Courier", font_size = 12) %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Listing 3.0", "Empty and NA page_by") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_footer(left = Sys.time(),
center = "Confidential",
right = "Page [pg] of [tpg]")
# Write the report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-93: Numeric page_by works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test93.docx")
# Prepare data
dat <- mtcars[order(mtcars$cyl), ]
dat <- data.frame(vehicle = rownames(dat), dat)
dat$cyl[1] <- NA
# Define table
tbl <- create_table(dat, show_cols = 1:8) %>%
page_by(cyl, label="Cylinders: ")
# Create the report
rpt <- create_report(fp, output_type = "docx",
font = "Courier", font_size = 12) %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Listing 3.0", "Numeric and NA page_by") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_footer(left = Sys.time(),
center = "Confidential",
right = "Page [pg] of [tpg]")
# Write the report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-94: Multiple dedupe works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test94.docx")
# Setup
subjid <- c(100,100,101,101,101,102,103,103,104,104)
param <- c(rep("Hemoglobin", 3), rep("Triglycerides", 4), rep("Platelets", 3))
result <- c(41, 53, 43, 39, 47, 52, 21, 38, 62, 26)
Lab <- c(NA, NA, "central", "central", "central", NA, "local", "local", NA, NA)
arm <- c(rep("A", 5), rep("B", 5))
df <- data.frame(subjid, arm, param, Lab, result)
tbl1 <- create_table(df, first_row_blank = FALSE) %>%
define(subjid, label = "Subject", align = "left", dedupe = TRUE) %>%
define(param, label = "Lab Test", dedupe = c("subjid", "param")) %>%
define(result, label = "Result") %>%
define(Lab, visible = FALSE) %>%
define(arm, label = "Arm",
blank_after = TRUE,
dedupe = c("Lab", "arm"),
align = "right")
rpt <- create_report(fp, output_type = "docx", font = fnt, font_size = fsz) %>%
page_header(left = "Company", right = c("Study ABC", "Status: Closed")) %>%
titles("Table 1.0", "Multiple Dedupe of subjid, arm, and lab test",
"Lab is non-visible for arm's dedupe", align = "center") %>%
footnotes("Program Name: table1_0.R") %>%
page_footer(left = "Time", center = "Confidential",
right = "Page [pg] of [tpg]") %>%
add_content(tbl1)
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-95: Customized small line height works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test95.docx")
dat <- iris[1:100, ]
dat$row_number <- 1:100
dat <- dat[, c("row_number", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")]
tbl <- create_table(dat) %>%
footnotes("line_height setting in report_options()", "My footnote 2", valign = "bottom") %>%
spanning_header(from = 1, to = 2, label = "First Spanning") %>%
spanning_header(from = 3, to = 5, label = "Second Spanning") %>%
page_by(Species, label = "Page by Label:")
rpt <- create_report(fp, output_type = "docx", font = "Arial",
font_size = 10, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
# report_options(line_height = 0.12, title_block = "paragraph") %>%
report_options(line_height = 0.12) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"), blank_row = "below") %>%
titles("Table 1.0", "Table with line height adjustment") %>%
# title_header("Table 1.0", "Table with line height adjustment") %>%
add_content(tbl) %>%
page_footer(c("Left1", "Left2"), "Center1", "Right1")
# The text squishing is expected.
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-95b: Customized large line height works as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test95b.docx")
dat <- iris[1:100, ]
dat$row_number <- 1:100
dat <- dat[, c("row_number", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")]
tbl <- create_table(dat) %>%
footnotes("line_height setting in report_options()", "My footnote 2", valign = "bottom") %>%
spanning_header(from = 1, to = 2, label = "First Spanning") %>%
spanning_header(from = 3, to = 5, label = "Second Spanning") %>%
page_by(Species, label = "Page by Label:")
rpt <- create_report(fp, output_type = "docx", font = "Arial",
font_size = 10, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
report_options(line_height = 0.2) %>%
page_header("Left", c("Right1", "Right2", "Page [pg] of [tpg]"), blank_row = "below") %>%
titles("Table 1.0", "Table with line height adjustment") %>%
# title_header("Table 1.0", "Table with line height adjustment") %>%
add_content(tbl) %>%
page_footer(c("Left1", "Left2"), "Center1", "Right1")
# Large line height should not cause overflow.
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-96: Percentage column widths work as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test96.docx")
dat <- iris
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Irises", "Another Title") %>%
define(Sepal.Length, label = "Sepal Length", width = "33.33333%", align = "center") %>%
define(Sepal.Width, label = "Sepal Width", width = "20%", align = "centre") %>%
define(Species, blank_after = TRUE)
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
add_content(tbl, blank_row = "none") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]") %>%
footnotes("My footnote 1", "My footnote 2")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-96b: Percentage column widths work with 100% as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test96b.docx")
dat <- iris
tbl <- create_table(dat, borders = "none") %>%
titles("Table 1.0", "My Nice Irises", "Another Title") %>%
define(Sepal.Length, label = "Sepal Length", width = "20.17%", align = "center") %>%
define(Sepal.Width, label = "Sepal Width", width = "20.17%", align = "centre") %>%
define(Petal.Length, label = "Petal Length", width = "20.17%", align = "centre") %>%
define(Petal.Width, label = "Petal.Width", width = "20.17%", align = "centre") %>%
define(Species, width = "19.32%", blank_after = TRUE)
total_width <- round(9*0.2017, 2) * 4 + round(9*0.1932, 2)
# The total width is 9.02, which is greater than 9
# The algorithm needs to prevent from total width exceeding 9
rpt <- create_report(fp, output_type = "docx", font = fnt,
font_size = 12, orientation = "landscape") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Left", c("Right1")) %>%
add_content(tbl, blank_row = "none") %>%
page_footer("Left1", "Center1", "Page [pg] of [tpg]") %>%
footnotes("My footnote 1", "My footnote 2")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-97: Output Chinese as expected.", {
if (dev == TRUE) {
# Read in prepared data
df <- read.table(header = TRUE, text = '
group1 group2 trt1 trt2 subgroup
"性别" "男" "75 (51.7)" "91 (59.5)" "≥65岁"
"性别" "女" "70 (48.3)" "62 (40.5)" "≥65岁"
"年龄" "例数" "145" "153" "≥65岁"
"年龄" "平均数" "64.7" "65.8" "≥65岁"
"年龄" "中位数" "65.0" "66.0" "≥65岁"
"年龄" "标准差" "9.7" "8.3" "≥65岁"
"年龄" "最小值, 最大值" "40, 83" "43.85" "≥65岁"')
df$trt3 <- df$trt1
df$trt4 <- df$trt2
df$trt5 <- df$trt1
font_lst <- c(8, 9, 10, 11, 12)
for (f in font_lst) {
if (f != 8) {
df_output <- rbind(df, df, df, df)
} else {
df_output <- rbind(df, df, df, df, df)
}
tbl <- create_table(df_output, borders = "all") %>%
titles("表 14-2.1. 基线人口学特征", "(安全分析集)", borders = "all") %>%
# stub(c("group1", "group2"), width = 1) %>%
stub(c("group1", "group2")) %>%
page_by(subgroup, label = "亚组 - 这是一段很长的字,预计会占两行以上。用来测试空白行的宽度是否需要缩减。切割文字的函數需 修改,分割不需要用空白:",
borders = "all") %>%
define(group1, label_row = T, blank_after = T) %>%
define(group2, indent = 0.25) %>%
define(trt1, label = "试验药物一") %>%
define(trt2, label = "试验药物二") %>%
define(trt3, label = "试验药物三") %>%
define(trt4, label = "试验药物四") %>%
define(trt5, label = "安慰剂") %>%
define(subgroup, visible = FALSE) %>%
spanning_header(from = "trt1", to = "trt3", label = "高试验药物") %>%
spanning_header(from = "trt4", to = "trt5", label = "低试验药物")
file_name <- paste0("test97_", f, ".docx")
fp <- file.path(base_path, "docx/", file_name)
rpt <- create_report(fp, output_type = "docx", font = "SimSun",
font_size = f, orientation = "portrait") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("研究123", "分析数据审阅说明") %>%
add_content(tbl, blank_row = "none") %>%
page_footer("左页尾", "中页尾", "第 [pg] 页, 共 [tpg] 页") %>%
footnotes("用于分析目的。其中包括人口统计学、治疗组和人群标帜。",
"受试者水平分析数据集。", borders = "all")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
}
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-98: Various cell styles work as expected", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test98.docx")
# Width calculation should be correct for bold text. No overflow
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N and more and more" "19" "13"
"ampg" "Mean and more and more" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median and more and more" "16.4" "21.4"
"ampg" "Q1 - Q3 and more and more" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range and more and more" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder and more and more" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder and more and more" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder and more and more" "5 ( 26.3%)" "6 ( 46.2%)"')
df$cylflg <- ifelse(df$var == "cyl", TRUE, FALSE)
df$ampgflg <- ifelse(df$var == "ampg", T, F)
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
stub(c("var", "label"), width = 2.06,
style = list(
cell_style(bold = TRUE,
italic = TRUE,
cell_color = "#BBFFBB",
borders = "all",
indicator = "labelrow"),
cell_style(bold = FALSE,
italic = FALSE,
cell_color = "cyan",
indicator = "blankrow"),
cell_style(bold = FALSE,
italic = FALSE,
cell_color = "yellow",
indicator = NULL)
)) %>%
define(var, blank_after = TRUE, label_row = TRUE,
format = c(ampg = "Miles Per Gallon and more bold text to be added to test function",
cyl = "Cylinders")) %>%
define(label, indent = .25) %>%
define(A, label = "Group A", align = "center", n = 19,
style = list(
cell_style(italic = TRUE,
bold = TRUE,
font_color = "blue",
cell_color = "gray",
borders = c("top", "bottom"),
indicator = "ampgflg"),
cell_style(italic = FALSE,
bold = FALSE,
font_color = "white",
cell_color = "olive",
borders = "all",
indicator = "cylflg")
)) %>%
define(B, label = "Group B", align = "center", n = 13,
style = list(
cell_style(bold = FALSE,
font_color = "red",
borders = "left",
indicator = "datarow"),
cell_style(bold = TRUE,
font_color = "blue",
borders = "left",
indicator = "ampgflg")
)) %>%
define(cylflg, visible = FALSE)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "docx",
font = "Times") %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "Various cell styles") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = "Left",
center = "Confidential",
right = "Page [pg] of [tpg]")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else {
expect_equal(TRUE, TRUE)
}
})
test_that("docx-99: Percentage column widths work with 100% as expected.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/test99.docx")
date_location <- paste0(
"Date: 24 Sep 2026 (Thursday) Time: 12:30-14:00\n",
"Venue: Hybrid meeting (Telephone Conference / Taipei meeting room 07/08)")
txt <- create_text(date_location, align = "center", borders = c("outside")) |>
titles("Theme: Sharing", blank_row = "none", borders = "none",
align = "center", font_size = 13, bold = T)
rpt <- create_report(fp, output_type = "docx", font = "Arial",
font_size = 11, orientation = "portrait") |>
add_content(txt, align = "center")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
# User Tests --------------------------------------------------------------
test_that("docx-user1: demo table works.", {
if (dev) {
library(tidyr)
library(dplyr)
# Data Filepath
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user1.docx")
# Load Data
data_demo <- file.path(dir_data, "dm.csv") %>%
read.csv()
data_demo <- subset(data_demo, data_demo$ARM != "SCREEN FAILURE")
sex_decode <- c("M" = "Male",
"F" = "Female")
race_decode <- c("WHITE" = "White",
"BLACK OR AFRICAN AMERICAN" = "Black or African American",
"ASIAN" = "Asian or Pacific Islander",
"NATIVE AMERICAN" = "Native American",
"UNKNOWN" = "Unknown")
arm_pop <- table(data_demo$ARM)
demo_age <-
data_demo %>%
group_by(ARM) %>%
summarise(across(.cols = AGE,
.fns = list(N = ~ fmt_n(.),
Mean = ~ fmt_mean_sd(.),
Median = ~ fmt_median(.),
`Q1 - Q3` = ~ fmt_quantile_range(.),
Range = ~ fmt_range(.)
))) %>%
pivot_longer(-ARM,
names_to = c("var", "label"),
names_sep = "_",
values_to = "value") %>%
pivot_wider(names_from = ARM,
values_from = "value")
demo_sex <-
data_demo %>%
add_count(ARM, SEX, name = "n_SEX") %>%
select(ARM, SEX, n_SEX) %>%
distinct() %>%
pivot_longer(cols = c(SEX),
names_to = "var",
values_to = "label") %>%
pivot_wider(names_from = ARM,
values_from = n_SEX,
values_fill = 0) %>%
mutate(label = factor(label, levels = names(sex_decode),
labels = sex_decode),
`ARM A` = fmt_cnt_pct(`ARM A`, arm_pop["ARM A"]),
`ARM B` = fmt_cnt_pct(`ARM B`, arm_pop["ARM B"]),
`ARM C` = fmt_cnt_pct(`ARM C`, arm_pop["ARM C"]),
`ARM D` = fmt_cnt_pct(`ARM D`, arm_pop["ARM D"]))
demo_race <-
data_demo %>%
add_count(ARM, RACE, name = "n_RACE") %>%
select(ARM, RACE, n_RACE) %>%
distinct() %>%
pivot_longer(cols = RACE,
names_to = "var",
values_to = "label") %>%
pivot_wider(names_from = ARM,
values_from = n_RACE,
values_fill = 0) %>%
mutate(label = factor(label, levels = names(race_decode),
labels = race_decode),
`ARM A` = fmt_cnt_pct(`ARM A`, arm_pop["ARM A"]),
`ARM B` = fmt_cnt_pct(`ARM B`, arm_pop["ARM B"]),
`ARM C` = fmt_cnt_pct(`ARM C`, arm_pop["ARM C"]),
`ARM D` = fmt_cnt_pct(`ARM D`, arm_pop["ARM D"])) %>%
arrange(var, label)
demo <- bind_rows(demo_age, demo_sex, demo_race)
#View(demo)
# Stub decode
block_fmt <- c(AGE = "Age", SEX = "Sex", RACE = "Race")
# Define table
tbl <- create_table(demo, first_row_blank = TRUE) %>%
column_defaults(from = "ARM A", to = "ARM D", width = 1.25) %>%
define(var, blank_after = TRUE, dedupe = TRUE,
format = block_fmt, label = "") %>%
define(label, label = "") %>%
define(`ARM A`, align = "center", label = "Placebo", n = 36) %>%
define(`ARM B`, align = "center", label = "Drug 10mg", n = 38) %>%
define(`ARM C`, align = "center", label = "Drug 20mg", n = 38) %>%
define(`ARM D`, align = "center", label = "Competitor", n = 38)
# Define Report
rpt <- create_report(fp, output_type = "DOCX", font = fnt, font_size = fsz) %>%
set_margins(top = 1, bottom = 1) %>%
options_fixed(font_size = 10) %>%
titles("Table 14.1/4",
"Demographics and Baseline to Characteristics",
"Specify Population Ω µ β ¥ ∑ ≠ ≤ £ ∞ ؈ ლ \Ub8a 鬼") %>%
add_content(tbl) %>%
footnotes("Special symbols \U221e to mess things up: Ω µ β ¥ ∑ ≠ ≤ £ ∞ ؈ ლ \Ub8a 鬼") %>%
footnotes("Special symbols µ Ω £ there to mess things up: ", "Page [pg] of [tpg]") %>%
page_header("Left µ Ω £ ", "Right") %>%
page_footer("Time µ Ω £ ", right = "Page [pg] of [tpg]")
# Write out report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-user2: demo table with stub works.", {
if (dev) {
library(tidyr)
library(dplyr)
# Data Filepath
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user2.docx")
# Load Data
data_demo <- file.path(dir_data, "dm.csv") %>%
read.csv()
data_demo <- subset(data_demo, data_demo$ARM != "SCREEN FAILURE")
sex_decode <- c("M" = "Male",
"F" = "Female")
race_decode <- c("WHITE" = "White",
"BLACK OR AFRICAN AMERICAN" = "Black or African American",
"ASIAN" = "Asian or Pacific Islander",
"NATIVE AMERICAN" = "Native American",
"UNKNOWN" = "Unknown")
arm_pop <- table(data_demo$ARM)
demo_age <-
data_demo %>%
group_by(ARM) %>%
summarise(across(.cols = AGE,
.fns = list(N = ~ fmt_n(.),
Mean = ~ fmt_mean_sd(.),
Median = ~ fmt_median(.),
`Q1 - Q3` = ~ fmt_quantile_range(.),
Range = ~ fmt_range(.)
))) %>%
pivot_longer(-ARM,
names_to = c("var", "label"),
names_sep = "_",
values_to = "value") %>%
pivot_wider(names_from = ARM,
values_from = "value")
demo_sex <-
data_demo %>%
add_count(ARM, SEX, name = "n_SEX") %>%
select(ARM, SEX, n_SEX) %>%
distinct() %>%
pivot_longer(cols = c(SEX),
names_to = "var",
values_to = "label") %>%
pivot_wider(names_from = ARM,
values_from = n_SEX,
values_fill = 0) %>%
mutate(label = factor(label, levels = names(sex_decode),
labels = sex_decode),
`ARM A` = fmt_cnt_pct(`ARM A`, arm_pop["ARM A"]),
`ARM B` = fmt_cnt_pct(`ARM B`, arm_pop["ARM B"]),
`ARM C` = fmt_cnt_pct(`ARM C`, arm_pop["ARM C"]),
`ARM D` = fmt_cnt_pct(`ARM D`, arm_pop["ARM D"]))
demo_race <-
data_demo %>%
add_count(ARM, RACE, name = "n_RACE") %>%
select(ARM, RACE, n_RACE) %>%
distinct() %>%
pivot_longer(cols = RACE,
names_to = "var",
values_to = "label") %>%
pivot_wider(names_from = ARM,
values_from = n_RACE,
values_fill = 0) %>%
mutate(label = factor(label, levels = names(race_decode),
labels = race_decode),
`ARM A` = fmt_cnt_pct(`ARM A`, arm_pop["ARM A"]),
`ARM B` = fmt_cnt_pct(`ARM B`, arm_pop["ARM B"]),
`ARM C` = fmt_cnt_pct(`ARM C`, arm_pop["ARM C"]),
`ARM D` = fmt_cnt_pct(`ARM D`, arm_pop["ARM D"])) %>%
arrange(var, label)
demo <- bind_rows(demo_age, demo_sex, demo_race)
#View(demo)
# Stub decode
block_fmt <- c(AGE = "Age", SEX = "Sex", RACE2 = "Race")
# Define table
tbl <- create_table(demo, first_row_blank = TRUE, borders = "all") %>%
stub(c("var", "label"), width = 1.5) %>%
column_defaults(width = 1) %>%
define(var, blank_after = TRUE,
format = block_fmt, label = "", label_row = TRUE) %>%
define(label, label = "", indent = .25) %>%
define(`ARM A`, align = "center", label = "Placebo", n = 36) %>%
define(`ARM B`, align = "center", label = "Drug 10mg", n = 38) %>%
define(`ARM C`, align = "center", label = "Drug 20mg", n = 38) %>%
define(`ARM D`, align = "center", label = "Competitor", n = 38) %>%
titles("Table 14.1/4", bold = TRUE, blank_row = "above",
align = "center", borders = c("top", "left", "right")) %>%
titles( "Demographics and Baseline Characteristics",
"Specify Population", borders = c("left", "right", "bottom"),
blank_row = "below",
align = "center") %>%
footnotes("Here is a footnote", "Here is another footnote", valign = "top",
borders = "outside", blank_row = "both", align = "left")
# Define Report
rpt <- create_report(fp, output_type = "DOCX",
font = "Arial", font_size = 10) %>%
add_content(tbl, align = "center") %>%
page_header("Sponsor", "Drug") %>%
page_footer(left = "Time", right = "Page [pg] of [tpg]") %>%
footnotes("Here is a footnote on the bottom") #%>%
#page_by(var = "var", label = "Variable: ")
# Write out report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-user3: listings works.", {
if (dev == TRUE) {
# Data Filepath
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user3.docx")
# Removing to make last page exactly equal to available rows on page.
# In this case, any added blank rows should be skipped.
fil <- c("ABC-14-124",
"ABC-15-153",
"ABC-15-154",
"ABC-15-155",
"ABC-15-156",
"ABC-16-045",
"ABC-16-046",
"ABC-16-047",
"ABC-16-157",
"ABC-16-158",
"ABC-16-159",
"ABC-16-160")
# Load Data
data_demo <- file.path(dir_data, "dm.csv") %>%
read.csv()
data_demo <- data_demo[!data_demo$USUBJID %in% fil, ]
# Test that any assigned formats are applied
attr(data_demo$SUBJID, "width") <- 1
attr(data_demo$SUBJID, "justify") <- "left"
attr(data_demo$SUBJID, "format") <- "S:%s"
#print(widths(data_demo))
names(data_demo)
# Define table
tbl <- create_table(data_demo, borders = "none") %>%
define(USUBJID, id_var = TRUE) #%>%
#define(ETHNIC, width = 1)
# Define Report
rpt <- create_report(fp, font = "Arial", font_size = 10,
orientation = "portrait") %>%
titles("Listing 1.0",
"Demographics Dataset") %>%
add_content(tbl, align = "left") %>%
page_header("Sponsor", "Drug") %>%
page_footer(left = "Time", right = "Page [pg] of [tpg]") %>%
footnotes("My footnotes")
#Write out report
res <- write_report(rpt, output_type = "DOCX")
expect_equal(file.exists(fp), TRUE)
# pdfpth <- file.path(base_path, "user/user3.pdf")
# write_report(rpt, pdfpth, output_type = "PDF")
# expect_equal(file.exists(pdfpth), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-user4: listing in cm and times works.", {
if (dev == TRUE) {
# Data Filepath
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user4.docx")
# Removing to make last page exactly equal to available rows on page.
# In this case, any added blank rows should be skipped.
fil <- c("ABC-14-124",
"ABC-15-153",
"ABC-15-154",
"ABC-15-155",
"ABC-15-156",
"ABC-16-045",
"ABC-16-046",
"ABC-16-047",
"ABC-16-157",
"ABC-16-158",
"ABC-16-159",
"ABC-16-160")
# Load Data
data_demo <- file.path(dir_data, "dm.csv") %>%
read.csv()
data_demo <- data_demo[!data_demo$USUBJID %in% fil, ]
# Test that any assigned formats are applied
attr(data_demo$SUBJID, "width") <- 2.54
attr(data_demo$SUBJID, "justify") <- "left"
attr(data_demo$SUBJID, "format") <- "S:%s"
#print(widths(data_demo))
# Define table
tbl <- create_table(data_demo, borders = "none") %>%
define(USUBJID, id_var = TRUE)
# Define Report
rpt <- create_report(fp, font = "Times", font_size = 10,
units = "cm", orientation = "landscape") %>%
titles("Listing 1.0",
"Demographics Dataset") %>%
add_content(tbl, align = "left") %>%
page_header("Sponsor", "Drug") %>%
page_footer(left = "Time", right = "Page [pg] of [tpg]") %>%
footnotes("My footnote")
#Write out report
res <- write_report(rpt, output_type = "DOCX")
expect_equal(file.exists(fp), TRUE)
#print(res$column_widths)
# pdfpth <- file.path(base_path, "user/user3.pdf")
# write_report(rpt, pdfpth, output_type = "PDF")
# expect_equal(file.exists(pdfpth), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-user5: Portrait in 12pt Arial works as expected.", {
if (dev == TRUE) {
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user5.docx")
# Read in prepared data
df <- read.table(header = TRUE, text = '
var label A B
"ampg" "N" "19" "13"
"ampg" "Mean" "18.8 (6.5)" "22.0 (4.9)"
"ampg" "Median" "16.4" "21.4"
"ampg" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
"ampg" "Range" "10.4 - 33.9" "14.7 - 32.4"
"cyl" "8 Cylinder" "10 ( 52.6%)" "4 ( 30.8%)"
"cyl" "6 Cylinder" "4 ( 21.1%)" "3 ( 23.1%)"
"cyl" "4 Cylinder" "5 ( 26.3%)" "6 ( 46.2%)"')
# Create table
tbl <- create_table(df, first_row_blank = TRUE) %>%
define(var, label = "Variable", blank_after = TRUE, dedupe = TRUE,
format = c(ampg = "Miles Per Gallon", cyl = "Cylinders")) %>%
define(label, label = "") %>%
define(A, label = "Group A", align = "center", n = 19) %>%
define(B, label = "Group B", align = "center", n = 13)
# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "DOCX",
font = "Arial", font_size = 12) %>%
page_header(left = "Client: Motor Trend", right = "Study: Cars") %>%
titles("Table 1.0", "MTCARS Summary Table") %>%
add_content(tbl) %>%
footnotes("* Motor Trend, 1974") %>%
page_footer(left = Sys.time(),
center = "Confidential",
right = "Page [pg] of [tpg]")
# Write out report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("docx-user6: Check wrapping.", {
if (dev == TRUE) {
dir_data <- file.path(data_dir, "data")
fp <- file.path(base_path, "docx/user6.docx")
# Load Data
data_demo <- file.path(dir_data, "dm.csv") %>%
read.csv()
dt <- data_demo[ , c("USUBJID", "BRTHDTC", "AGE", "SEX", "RACE", "ARM")]
attr(dt$USUBJID, "label") <- "Universal Subject ID"
attr(dt$BRTHDTC, "label") <- "Subject Birth Date"
attr(dt$AGE, "label") <- "Subject Age in Years"
attr(dt$SEX, "label") <- "Subject Biological Sex at Birth"
attr(dt$RACE, "label") <- "Subject Race Category"
attr(dt$ARM, "label") <- "Treatment Group"
# Create table
tbl <- create_table(dt)
# Create report
rpt <- create_report(fp, orientation = "portrait", font_size = 12,
output_type = "DOCX", font = "Times") %>%
titles("Our first SASSY report", bold = TRUE) %>%
add_content(tbl)
# write out the report
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
} else
expect_equal(TRUE, TRUE)
})
test_that("user7: Borders with spanning headers work as expected.", {
if (dev == TRUE) {
df <- read.table(header = TRUE, text = '
var label A B C
"AGE" "n" "19" "13" "32"
"AGE" "Mean" "18.8 (6.5)" "22.0 (4.9)" "20.0 (5.9)"
"AGE" "Median" "16.4" "21.4" "20.1"
"AGE" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8" "15.2 - 21.8"
"RACE" "White" "10 (52.6)" "4 (30.8)" "14 (43.8)"
"RACE" "Black" "4 (21.1)" "3 (23.1)" "7 (21.9)"
"RACE" "Others\U1D47" "5 (26.3)" "6 (46.2)" "11 (34.4.2)"
')
var_fmt <- c(AGE = "Age (yrs)", RACE = "Race - n (%)")
fp <- file.path(base_path, "docx/user7.docx")
tbl <- create_table(df, first_row_blank = TRUE, borders = c("outside")) %>%
stub(vars = c("var", "label"), " ", width = 2.5) %>%
spanning_header(from = "A", to = "B", label = "Treatments\U1D43") %>%
define(var, blank_after = TRUE, format = var_fmt, label_row = TRUE) %>%
define(label, indent=0.25) %>%
define(A, align = "center", label = "Placebo\n(N = 19)") %>%
define(B, align = "center", label = "Drug\n(N = 13)") %>%
define(C, align = "center", label = "Total\n(N = 32)") %>%
footnotes("\U1D43 study drug", blank_row="none" ) %>%
footnotes("\U1D47 Asian, Japanese and Chinese", blank_row="none")
rpt <- create_report(fp, output_type = "DOCX",
font = "Arial") %>%
set_margins(top = 1, bottom = 1) %>%
add_content(tbl) %>%
page_footer(left = paste("Date:", Sys.time()), right = "Page [pg] of [tpg]", blank_row="none") %>%
footnotes("Program: C:/Users/Home/AppData/Local/Temp/tdemo.R", blank_row="above") %>%
# Turn them down for issue 355
titles("Table 1.0", "My Nice Table") %>%
page_header("Test")
res <- write_report(rpt)
expect_equal(file.exists(fp), TRUE)
expect_equal(res$pages, 1)
# file.show(fp)
} else
expect_equal(TRUE, TRUE)
})
# This is good
test_that("user8: Check footnotes on page by.", {
if (dev == TRUE) {
fp <- file.path(base_path, "docx/user8")
df <- read.table(header = TRUE, text = '
var stat A B
"Age (yrs)" "n" "19" "13"
" " "Mean" "18.8 (6.5)" "22.0 (4.9)"
" " "Median" "16.4" "21.4"
" " "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8"
" " " " " " " "
"Race" "White" "10 ( 52.6%)" "4 ( 30.8%)"
" " "Black" "4 ( 21.1%)" "3 ( 23.1%)"
" " "Others\U1D47" "5 ( 26.3%)" "6 ( 46.2%)"')
df1 <- df
df2 <- df
df3 <- df
df1$SEX <- 'Female'
df2$SEX <- 'Male'
df3$SEX <- 'Other'
df <- rbind(df1, df2, df3)
# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders=c("top")) %>%
page_by(SEX, "Sex: ", align = "left", blank_row="none") %>%
#stub(c("var", "label")) %>%
column_defaults(width = 1.25) %>%
spanning_header(from = "A", to = "B", label = "Treatments\U1D43") %>%
define(SEX, visible = FALSE) %>%
define(var, label = " ", align = "left") %>%
define(stat,label = " ", align = "left") %>%
define(A, label = "Treament A", align = "center", n = 19) %>%
define(B, label = "Treament B", align = "center", n = 13) %>%
titles("Table 1.1 Demographics", "Randomised Population", font_size = 10) %>%
footnotes("Page [pg] of [tpg]", align = "right", blank_row="none", borders=c("top")) %>%
footnotes("\U1D43 study drug treatments", blank_row="none", borders=c("top") ) %>%
footnotes("\U1D47 Asian, Japanese and Chinese", blank_row="none", borders = "bottom")
rpt <- create_report(fp, output_type = "DOCX", font = "Arial", font_size = 10) %>%
#This is page header and it goes into the header of the table
page_header("Protocol: 9999") %>%
add_content(tbl) %>%
page_footer(left = paste("Date:", Sys.time()), right = "Page [pg] of [tpg]", blank_row="none") %>%
footnotes("Program: C:/Users/Home/AppData/Local/Temp/tdemo.R", blank_row="above", valign = "bottom")
res <- write_report(rpt)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 3)
} else
expect_equal(TRUE, TRUE)
})
# Custom line count. Needed because something is wrong. But I can't figure it out.
test_that("user9: Report with top and bottom borders stays on one page.", {
if (dev) {
fp <- file.path(base_path, "docx/user9")
df <- read.table(header = TRUE, text = '
var label A B C
"AGE" "n" "19" "13" "32"
"AGE" "Mean" "18.8 (6.5)" "22.0 (4.9)" "20.0 (5.9)"
"AGE" "Median" "16.4" "21.4" "20.1"
"AGE" "Q1 - Q3" "15.1 - 21.2" "19.2 - 22.8" "15.2 - 21.8"
"RACE" "White" "10 (52.6)" "4 (30.8)" "14 (43.8)"
"RACE" "Black" "4 (21.1)" "3 (23.1)" "7 (21.9)"
"RACE" "Others\U1D47" "5 (26.3)" "6 (46.2)" "11 (34.4.2)"
')
var_fmt <- c(AGE = "Age (yrs)", RACE = "Race - n (%)")
tbl <- create_table(df, first_row_blank = TRUE, borders = c("top", "bottom")) %>%
stub(vars = c("var", "label"), " ", width = 2.5) %>%
# define(var, blank_after = TRUE, dedupe = TRUE, label = "Variable", format = var_fmt,label_row = TRUE) %>%
spanning_header(from = "A", to = "B", label = "Treatments\U1D43") %>%
define(var, blank_after = TRUE, format = var_fmt, label_row = TRUE) %>%
define(label, indent=0.25) %>%
# define(A, align = "center", label = "Placebo\n(N = 19)", n = 19) %>%
# define(B, align = "center", label = "Drug\n(N = 13)", n = 13) %>%
# define(C, align = "center", label = "Total\n(N = 32)", n = 32) %>%
define(A, align = "center", label = "Placebo", n = 19) %>%
define(B, align = "center", label = "Drug", n = 13) %>%
define(C, align = "center", label = "Total", n = 32) %>%
footnotes("\U1D43 study drug", blank_row="none" ) %>%
footnotes("\U1D47 Asian, Japanese and Chinese", blank_row="none") %>%
titles("My title", "My title 2")
rpt <- create_report(fp, output_type = "DOCX",
font = "Arial", font_size = 11) %>%
set_margins(top = 1, bottom = 1) %>%
options_fixed(line_count = 32) %>%
add_content(tbl) %>%
# page_header("Help") %>%
#page_footer(left = paste("Date:", Sys.time()), right = "Page [pg] of [tpg]", blank_row="none") %>%
footnotes("Program: C:/Users/Home/AppData/Local/Temp/tdemo.R", blank_row="above")
res <- write_report(rpt)
#file.show(res$modified_path)
expect_equal(file.exists(res$modified_path), TRUE)
expect_equal(res$pages, 1)
} else
expect_equal(TRUE, TRUE)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.