print.rdocx() is the essential output function for creating Word files
with officer. It takes an rdocx object (created with read_docx() and
populated with content) and writes it to disk as a .docx file.
This function performs all necessary post-processing operations before writing the file.
The function is typically called at the end of your document creation
workflow, after all content has been added with body_add_*() functions.
Usage
# S3 method for class 'rdocx'
print(
x,
target = NULL,
copy_header_refs = FALSE,
copy_footer_refs = FALSE,
preview = FALSE,
...
)Arguments
- x
an
rdocxobject created withread_docx()- target
path to the
.docxfile to write. The file will be created or overwritten if it already exists. IfNULLandpreview = FALSE, the function returnsNULLwithout writing a file.logical, default is FALSE. If TRUE, copy the references to the header and footer in each section of the body of the document. This parameter is experimental and may change in a future version.
- preview
Save
xto a temporary file and open it (defaultFALSE). WhenTRUE, the document is saved to a temporary location and opened with the system's default application for.docxfiles, useful for quick previewing during development.- ...
unused
Value
The full path to the created .docx file (invisibly). This allows
chaining operations or capturing the output path for further use.
See also
Create a 'Word' document object with read_docx(), add content with
functions body_add_par(), body_add_plot(),
body_add_table(), change settings with docx_set_settings(), set
properties with set_doc_properties(), read 'Word' styles with
styles_info().
Examples
library(officer)
# This example demonstrates how to create
# an small document -----
## Create a new Word document
doc <- read_docx()
doc <- body_add_par(doc, "hello world")
## Save the document
output_file <- print(doc, target = tempfile(fileext = ".docx"))
# preview mode: save to temp file and open locally ----
if (FALSE) { # \dontrun{
print(doc, preview = TRUE)
} # }
