Now, let’s aggregate the filtered dataset and
count observations. The created dataset will
have three dimensions, cut, color and clarity
that will be used as rows or columns in the final
flextable.
Now all datasets are ready to be used, let’s call
function tabulator() that will prepare an object
ready to be sent to as_flextable().
ftd <- fp_text_default(color = "#f24f26")
# create tabulator object to be used with `as_flextable()` ----
tab <- tabulator(
x = summary_dat,
rows = c("cut", "color"),
columns = "clarity",
hidden_data = cut_counts,
row_compose = list(
cut = as_paragraph(cut, as_chunk(x = paste0("\nn = ", n_cut), props = ftd))
),
# defines the only cells to show in the result
`y stats` = as_paragraph(y_mean, " (\u00B1 ", y_sd, ")")
)
## get colkeys corresponding to multiple "y stats" ----
colkeys <- tabulator_colnames(tab, columns = "y stats")
colkeys
## [1] "I1@y stats" "SI1@y stats" "VS2@y stats"
The flextable
A flextable is produced with the following code:
ft <- as_flextable(tab, separate_with = "cut")
ft
cut
color
I1
SI1
VS2
Good n = 2634
D
3.5 (± 2.2)
3.0 (± 2.8)
3.6 (± 3.4)
E
4.4 (± 2.5)
3.2 (± 3.1)
3.8 (± 3.3)
F
2.6 (± 1.9)
3.3 (± 3.0)
3.8 (± 3.2)
G
3.2 (± 2.0)
4.1 (± 3.9)
4.1 (± 3.3)
H
3.8 (± 2.2)
4.2 (± 3.8)
4.4 (± 4.2)
I
4.2 (± 2.9)
4.7 (± 4.5)
6.0 (± 5.1)
J
3.8 (± 2.1)
4.6 (± 3.9)
4.8 (± 3.6)
Very Good n = 5915
D
2.6 (± 0.8)
3.2 (± 2.9)
3.1 (± 3.3)
E
3.4 (± 2.2)
3.2 (± 3.1)
3.3 (± 3.5)
F
4.3 (± 2.6)
3.6 (± 3.2)
4.0 (± 3.8)
G
3.2 (± 2.1)
3.5 (± 3.6)
4.4 (± 4.0)
H
5.3 (± 2.5)
4.9 (± 4.4)
4.6 (± 4.0)
I
6.0 (± 4.9)
5.2 (± 4.5)
5.8 (± 5.1)
J
4.5 (± 2.7)
5.0 (± 4.1)
5.3 (± 4.5)
Customise the table
We need to add details in the columns headers.
ft <- append_chunks(ft,
j = colkeys, i = 1, part = "header",
as_paragraph(
"\n",
as_chunk(x = paste0("(n = ", clarity_counts$n_clarity, ")"), props = ftd)
)
)
ft
cut
color
I1 (n = 180)
SI1 (n = 4800)
VS2 (n = 3569)
Good n = 2634
D
3.5 (± 2.2)
3.0 (± 2.8)
3.6 (± 3.4)
E
4.4 (± 2.5)
3.2 (± 3.1)
3.8 (± 3.3)
F
2.6 (± 1.9)
3.3 (± 3.0)
3.8 (± 3.2)
G
3.2 (± 2.0)
4.1 (± 3.9)
4.1 (± 3.3)
H
3.8 (± 2.2)
4.2 (± 3.8)
4.4 (± 4.2)
I
4.2 (± 2.9)
4.7 (± 4.5)
6.0 (± 5.1)
J
3.8 (± 2.1)
4.6 (± 3.9)
4.8 (± 3.6)
Very Good n = 5915
D
2.6 (± 0.8)
3.2 (± 2.9)
3.1 (± 3.3)
E
3.4 (± 2.2)
3.2 (± 3.1)
3.3 (± 3.5)
F
4.3 (± 2.6)
3.6 (± 3.2)
4.0 (± 3.8)
G
3.2 (± 2.1)
3.5 (± 3.6)
4.4 (± 4.0)
H
5.3 (± 2.5)
4.9 (± 4.4)
4.6 (± 4.0)
I
6.0 (± 4.9)
5.2 (± 4.5)
5.8 (± 5.1)
J
4.5 (± 2.7)
5.0 (± 4.1)
5.3 (± 4.5)
Add a title in the header part:
ft <- add_header_lines(ft, "Subset of original dataset")
ft
Subset of original dataset
cut
color
I1 (n = 180)
SI1 (n = 4800)
VS2 (n = 3569)
Good n = 2634
D
3.5 (± 2.2)
3.0 (± 2.8)
3.6 (± 3.4)
E
4.4 (± 2.5)
3.2 (± 3.1)
3.8 (± 3.3)
F
2.6 (± 1.9)
3.3 (± 3.0)
3.8 (± 3.2)
G
3.2 (± 2.0)
4.1 (± 3.9)
4.1 (± 3.3)
H
3.8 (± 2.2)
4.2 (± 3.8)
4.4 (± 4.2)
I
4.2 (± 2.9)
4.7 (± 4.5)
6.0 (± 5.1)
J
3.8 (± 2.1)
4.6 (± 3.9)
4.8 (± 3.6)
Very Good n = 5915
D
2.6 (± 0.8)
3.2 (± 2.9)
3.1 (± 3.3)
E
3.4 (± 2.2)
3.2 (± 3.1)
3.3 (± 3.5)
F
4.3 (± 2.6)
3.6 (± 3.2)
4.0 (± 3.8)
G
3.2 (± 2.1)
3.5 (± 3.6)
4.4 (± 4.0)
H
5.3 (± 2.5)
4.9 (± 4.4)
4.6 (± 4.0)
I
6.0 (± 4.9)
5.2 (± 4.5)
5.8 (± 5.1)
J
4.5 (± 2.7)
5.0 (± 4.1)
5.3 (± 4.5)
And add a special line so that when rendered in line the top of
the flextable will indicate the page number corresponding to
its position in the document.
ft <- add_header_lines(ft, "Page N°") |>
append_chunks(i = 1, part = "header", j = 1,
as_word_field(x = "Page")) |>
align(part = "header", align = "right", i = 1) |>
set_caption(caption = "Prices of over 50 000 round cut diamonds")
ft
Table 1: Prices of over 50 000 round cut diamonds
Page N°NA
Subset of original dataset
cut
color
I1 (n = 180)
SI1 (n = 4800)
VS2 (n = 3569)
Good n = 2634
D
3.5 (± 2.2)
3.0 (± 2.8)
3.6 (± 3.4)
E
4.4 (± 2.5)
3.2 (± 3.1)
3.8 (± 3.3)
F
2.6 (± 1.9)
3.3 (± 3.0)
3.8 (± 3.2)
G
3.2 (± 2.0)
4.1 (± 3.9)
4.1 (± 3.3)
H
3.8 (± 2.2)
4.2 (± 3.8)
4.4 (± 4.2)
I
4.2 (± 2.9)
4.7 (± 4.5)
6.0 (± 5.1)
J
3.8 (± 2.1)
4.6 (± 3.9)
4.8 (± 3.6)
Very Good n = 5915
D
2.6 (± 0.8)
3.2 (± 2.9)
3.1 (± 3.3)
E
3.4 (± 2.2)
3.2 (± 3.1)
3.3 (± 3.5)
F
4.3 (± 2.6)
3.6 (± 3.2)
4.0 (± 3.8)
G
3.2 (± 2.1)
3.5 (± 3.6)
4.4 (± 4.0)
H
5.3 (± 2.5)
4.9 (± 4.4)
4.6 (± 4.0)
I
6.0 (± 4.9)
5.2 (± 4.5)
5.8 (± 5.1)
J
4.5 (± 2.7)
5.0 (± 4.1)
5.3 (± 4.5)
See how it renders in Word
The following code is producing a Word document
with package ‘officer’, the content is a fake content.
The purpose is to show how the flextable is rendered
in a Word document.
psum_txt <- "Lorem ipsum dolor sit amet, purus ut nullam nisl vehicula non ligula sem non. Egestas nascetur, eu sed nec mattis semper arcu auctor sagittis id consequat non? Facilisi vestibulum ac nec primis. Posuere sociis ligula tempor, mattis sed sed dapibus. Taciti nulla mattis aliquet dictumst, nisi aenean, pulvinar! Hendrerit porttitor quis praesent mi nisl lorem mauris ut nulla. Tincidunt in sit sit quisque id molestie. Eros, orci ligula phasellus sed erat vel vivamus penatibus aliquam, scelerisque turpis sociis erat."
landscape_two_columns <- block_section(
prop_section(
type = "continuous",
section_columns = section_columns(widths = c(3, 3))
)
)
read_docx(path = "template.docx") |>
body_add_par(value = "Lorem ipsum", style = "heading 1") |>
body_add_par(psum_txt) |>
body_add_par(value = "Tempor velit sed", style = "heading 2") |>
body_end_block_section(value = block_section(property = prop_section(type = "continuous"))) |>
body_add_par(psum_txt) |>
body_add_par(psum_txt) |>
body_end_block_section(value = landscape_two_columns) |>
body_add_par(psum_txt) |>
body_add_par(psum_txt) |>
body_add_break() |>
body_add_par(value = "Mattis potenti metus", style = "heading 2") |>
body_add_par(value = "") |>
body_add_flextable(value = ft, topcaption = FALSE, keepnext = FALSE) |>
body_add_par(psum_txt) |>
print(target = "illustration.docx")