This function is used to insert 'Word' computed field into flextable.
It is used to add it to the content of a cell of the
flextable with the functions compose()
, append_chunks()
or prepend_chunks()
.
This has only effect on 'Word' output. If you want to
condition its execution only for Word output, you can
use it in the post processing step (see
set_flextable_defaults(post_process_docx = ...)
)
Do not forget to update the computed field in Word.
Fields are defined but are not computed, this computing is an
operation that has to be made by 'Microsoft Word'
(select all text and hit F9
when on mac os).
as_word_field(x, props = NULL, width = 0.1, height = 0.15, unit = "in")
computed field strings
text properties (see fp_text_default()
or officer::fp_text()
)
object to be used to format the text. If not specified, it will use
the default text properties of the cell(s).
size computed field
unit for width and height, one of "in", "cm", "mm".
Other chunk elements for paragraph:
as_bracket()
,
as_b()
,
as_chunk()
,
as_equation()
,
as_highlight()
,
as_image()
,
as_i()
,
as_sub()
,
as_sup()
,
colorize()
,
gg_chunk()
,
hyperlink_text()
,
linerange()
,
lollipop()
,
minibar()
,
plot_chunk()
library(flextable)
# define some default values ----
set_flextable_defaults(font.size = 22, border.color = "gray")
# an example with append_chunks ----
pp_docx = function(x) {
x <- add_header_lines(x, "Page ")
x <- append_chunks(
x = x, i = 1, part = "header", j = 1,
as_word_field(x = "Page")
)
align(x, part = "header", align = "left")
}
ft_1 <- flextable(cars)
ft_1 <- autofit(ft_1)
ft_1 <- pp_docx(ft_1)
## or:
# set_flextable_defaults(post_process_docx = pp_docx)
## to prevent this line addition when output is not docx
# print(ft_1, preview = "docx")
# an example with compose ----
library(officer)
ft_2 <- flextable(head(cars))
ft_2 <- add_footer_lines(ft_2, "temp text")
ft_2 <- compose(
x = ft_2, part = "footer", i = 1, j = 1,
as_paragraph("p. ",
as_word_field(x = "Page", width = .05),
" on ", as_word_field(x = "NumPages", width = .05))
)
ft_2 <- autofit(ft_2, part = c("header", "body"))
doc <- read_docx()
doc <- body_add_flextable(doc, ft_2)
doc <- body_add_break(doc)
doc <- body_add_flextable(doc, ft_2)
outfile <- print(doc, target = tempfile(fileext = ".docx"))
# reset default values ----
init_flextable_defaults()