
Add a vector of objects to a rdocx object using add_to_body()
Source: R/vec_add_to_body.R
vec_add_to_body.Rdvec_add_to_body() is a vectorized variant add_to_body() that allows users
to supply a vector of values or str inputs to add multiple blocks of text,
images, plots, or tables to a document. Alternatively, the function also
supports adding a single object at multiple locations or using multiple
different styles. All parameters are recycled using
vctrs::vec_recycle_common() so inputs must be length 1 or match the length
of the longest input vector. Optionally, the function can apply a separator
between each element by passing a value to add_to_body() or passing docx to
a function, such as officer::body_add_break().
Arguments
- docx
A rdocx object.
- ...
Arguments passed on to
add_to_bodygt_objectA gt object converted to an OOXML string with
gt::as_word()then passed toadd_xml_to_body()as str parameter. Required foradd_gt_to_body().tablecontainerIf
TRUE(default), add tables inside of a tablecontainer tag that automatically adds a table number and converts the gt title into a table caption. This feature is based on code from the gto package by Ellis Hughes to transform the gt_object to OOXML and insert the XML into the docx object.captionName of the ggplot2 label to use as a caption if plot passed to value has a label for this value. Defaults to "title".
caption_stylePassed to style for
officer::body_add_caption(). Defaults to same value as style..fAny function that takes a docx and value parameter and returns a rdocx object. A keyword parameter must also be supported if named is TRUE. Defaults to
add_text_to_body().keyword,idA keyword string used to place cursor with
officer::cursor_reach()or bookmark id withofficer::cursor_bookmark(). Defaults toNULL. If keyword or id are not provided, the gt object is inserted at the front of the document.indexA integer matching a doc_index value appearing in a summary of the docx object created with
officer::docx_summary(). If index is for a paragraph value, the text of the pargraph is used as a keyword.callThe execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofabort()for more information.valueobject to add in the document. Supported objects are vectors, data.frame, graphics, block of formatted paragraphs, unordered list of formatted paragraphs, pretty tables with package flextable, 'Microsoft' charts with package mschart.
styleparagraph style name. These names are available with function styles_info and are the names of the Word styles defined in the base document (see argument
pathfrom read_docx).stra wml string
poswhere to add the new element relative to the cursor, one of "after", "before", "on".
.envir[
environment:parent.frame()]
Environment to evaluate each expression in. Expressions are evaluated from left to right. If.xis an environment, the expressions are evaluated in that environment and.enviris ignored. IfNULLis passed, it is equivalent toemptyenv()..na[
character(1): ‘NA’]
Value to replaceNAvalues with. IfNULLmissing values are propagated, that is anNAresult will causeNAoutput. Otherwise the value is replaced by the value of.na..null[
character(1): ‘character()’]
Value to replace NULL values with. Ifcharacter()whole output ischaracter(). IfNULLall NULL values are dropped (as inpaste0()). Otherwise the value is replaced by the value of.null.alignTable alignment
scalar<character>// default:"center"An option for table alignment. Can either be
"center","left", or"right".caption_locationCaption location
singl-kw:[top|bottom|embed]// default:"top"Determines where the caption should be positioned. This can either be
"top","bottom", or"embed".caption_alignCaption alignment
Determines the alignment of the caption. This is either
"left"(the default),"center", or"right". This option is only used whencaption_locationis not set as"embed".splitAllow splitting of a table row across pages
scalar<logical>// default:FALSEA logical value that indicates whether to activate the Word option
Allow row to break across pages.keep_with_nextKeeping rows together
scalar<logical>// default:TRUEA logical value that indicates whether a table should use Word option
Keep rows together.autonumAutomatic Table Numbering
scalar<logical>// default:TRUEA logical value that indicates whether a table should use Words built-in auto table numbering option in the caption.
Automatic Table Numbering.
- .sep
A bare function, such as officer::body_add_break or another object passed to
add_to_body()as the value parameter.- .pos
String passed to pos parameter if
add_to_body()with .sep if .sep is not a function. Defaults to "after".- .size
Desired output size.
- .call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofabort()for more information.
Examples
docx_example <- read_officer()
docx_example <- vec_add_to_body(
docx_example,
value = c("Sample text 1", "Sample text 2", "Sample text 3"),
style = c("heading 1", "heading 2", "Normal")
)
docx_example <- vec_add_to_body(
docx_example,
value = rep("Text", 5),
style = "Normal",
.sep = officer::body_add_break
)
officer_summary(docx_example)
#> # A tibble: 12 × 11
#> doc_index content_type style_name text table_index row_id cell_id is_header
#> <int> <chr> <chr> <chr> <int> <int> <int> <lgl>
#> 1 2 paragraph heading 1 "Samp… NA NA NA NA
#> 2 3 paragraph heading 2 "Samp… NA NA NA NA
#> 3 4 paragraph Normal "Samp… NA NA NA NA
#> 4 5 paragraph Normal "Text" NA NA NA NA
#> 5 6 paragraph NA "\n" NA NA NA NA
#> 6 7 paragraph Normal "Text" NA NA NA NA
#> 7 8 paragraph NA "\n" NA NA NA NA
#> 8 9 paragraph Normal "Text" NA NA NA NA
#> 9 10 paragraph NA "\n" NA NA NA NA
#> 10 11 paragraph Normal "Text" NA NA NA NA
#> 11 12 paragraph NA "\n" NA NA NA NA
#> 12 13 paragraph Normal "Text" NA NA NA NA
#> # ℹ 3 more variables: row_span <int>, col_span <chr>, table_stylename <chr>
if (rlang::is_installed("gt")) {
gt_tbl <- gt::gt(gt::gtcars[1:2, 1:2])
# list inputs such as gt tables must be passed within a list to avoid
# issues
docx_example <- vec_add_to_body(
docx_example,
gt_object = list(gt_tbl, gt_tbl),
keyword = c("Sample text 1", "Sample text 2")
)
officer_summary(docx_example)
}
#> # A tibble: 24 × 11
#> doc_index content_type style_name text table_index row_id cell_id is_header
#> <int> <chr> <chr> <chr> <int> <int> <int> <lgl>
#> 1 2 paragraph heading 1 "Samp… NA NA NA NA
#> 2 9 paragraph heading 2 "Samp… NA NA NA NA
#> 3 16 paragraph Normal "Samp… NA NA NA NA
#> 4 17 paragraph Normal "Text" NA NA NA NA
#> 5 18 paragraph NA "\n" NA NA NA NA
#> 6 19 paragraph Normal "Text" NA NA NA NA
#> 7 20 paragraph NA "\n" NA NA NA NA
#> 8 21 paragraph Normal "Text" NA NA NA NA
#> 9 22 paragraph NA "\n" NA NA NA NA
#> 10 23 paragraph Normal "Text" NA NA NA NA
#> # ℹ 14 more rows
#> # ℹ 3 more variables: row_span <int>, col_span <chr>, table_stylename <chr>