Title: | Extra Helpers for 'officer' |
---|---|
Description: | Helper and convenience functions using the 'officer' package to modify docx files. |
Authors: | Eli Pousson [aut, cre, cph] |
Maintainer: | Eli Pousson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1.9002 |
Built: | 2024-11-21 05:36:35 UTC |
Source: | https://github.com/elipousson/officerExtras |
rdocx
objectThis function assumes that the input docx object has a list style. Use
read_docx_ext()
with allow_null = TRUE
to create a new rdocx object with
the "List Bullet" and "List Number" styles. "List Paragraph" can be used but
the resulting formatted list will not have bullets or numbers.
add_list_to_body( docx, values, style = "List Bullet", keep_na = FALSE, before = NULL, after = "", ... )
add_list_to_body( docx, values, style = "List Bullet", keep_na = FALSE, before = NULL, after = "", ... )
docx |
A |
values |
Character vector with values for list. |
style |
Style to use for list of values, Default: 'List Bullet' |
keep_na |
If |
before |
Value to insert before the list, Default: NULL |
after |
Value to insert after the list, Default: ” |
... |
Additional parameters passed to |
A modified rdocx
object.
add_to_body()
, vec_add_to_body()
Wrappers for officer::body_add_par()
, officer::body_add_gg()
, and
officer::body_add_xml()
that use the cursor_docx()
helper function to
allow users to pass the value and keyword, id, or index value used to place a
"cursor" within the document using a single function. If pos = NULL
,
add_to_body()
calls officer::body_add()
instead of
officer::body_add_par()
. If value is a gt_tbl
object, value is passed as
the gt_object parameter for add_gt_to_body()
.
add_text_to_body()
passes value to glue::glue()
to add support for glue string interpolation.
add_gt_to_body()
converts gt tables to OOXML with gt::as_word()
.
add_gg_to_body()
adds a caption following the plots using the labels
from the plot object.
add_to_body( docx, keyword = NULL, id = NULL, index = NULL, value = NULL, str = NULL, style = NULL, pos = "after", ..., gt_object = NULL, call = caller_env() ) add_text_to_body( docx, value, style = NULL, pos = "after", .na = "NA", .null = NULL, .envir = parent.frame(), ... ) add_xml_to_body(docx, str, pos = "after", ..., call = caller_env()) add_gt_to_body( docx, gt_object, align = "center", caption_location = c("top", "bottom", "embed"), caption_align = "left", split = FALSE, keep_with_next = TRUE, pos = "after", tablecontainer = TRUE, ..., call = caller_env() ) add_gg_to_body( docx, value, caption = "title", caption_style = style, autonum = NULL, style = "Normal", pos = "after", ... ) add_value_with_keys(docx, value, ..., .f = add_text_to_body) add_str_with_keys(docx, str, ..., .f = add_xml_to_body)
add_to_body( docx, keyword = NULL, id = NULL, index = NULL, value = NULL, str = NULL, style = NULL, pos = "after", ..., gt_object = NULL, call = caller_env() ) add_text_to_body( docx, value, style = NULL, pos = "after", .na = "NA", .null = NULL, .envir = parent.frame(), ... ) add_xml_to_body(docx, str, pos = "after", ..., call = caller_env()) add_gt_to_body( docx, gt_object, align = "center", caption_location = c("top", "bottom", "embed"), caption_align = "left", split = FALSE, keep_with_next = TRUE, pos = "after", tablecontainer = TRUE, ..., call = caller_env() ) add_gg_to_body( docx, value, caption = "title", caption_style = style, autonum = NULL, style = "Normal", pos = "after", ... ) add_value_with_keys(docx, value, ..., .f = add_text_to_body) add_str_with_keys(docx, str, ..., .f = add_xml_to_body)
docx |
A rdocx object. |
keyword , id
|
A keyword string used to place cursor with
|
index |
A integer matching a doc_index value appearing in a summary of
the docx object created with |
value |
object 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. |
str |
a wml string |
style |
paragraph 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 |
pos |
where to add the new element relative to the cursor, one of "after", "before", "on". |
... |
Additional parameters passed to |
gt_object |
A gt object converted to an OOXML string with
|
call |
The execution environment of a currently
running function, e.g. |
.na |
[ |
.null |
[ |
.envir |
[ |
align |
Table alignment
An option for table alignment. Can either be |
caption_location |
Caption location
Determines where the caption should be positioned. This can either be
|
caption_align |
Caption alignment Determines the alignment of the caption. This is
either |
split |
Allow splitting of a table row across pages
A logical value that indicates whether to activate the Word option
|
keep_with_next |
Keeping rows together
A logical value that indicates whether a table should use Word option
|
tablecontainer |
If |
caption |
Name of the ggplot2 label to use as a caption if plot passed to value has a label for this value. Defaults to "title". |
caption_style |
Passed to style for |
autonum |
an object generated with function run_autonum |
.f |
Any 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 |
Using add_value_with_keys()
or add_str_with_keys()
add_value_with_keys()
supports value vectors of length 1 or longer. If
value is named, the names are assumed to be keywords indicating the cursor
position for adding each value in the vector. If value is not named, a
keyword parameter with the same length as value must be provided. When named = FALSE
, no keyword parameter is required. Add add_str_with_keys()
works
identically but uses a str parameter and .f defaults to add_xml_to_body()
.
Note, as of July 2023, both add_value_with_keys()
and add_str_with_keys()
are superseded by vec_add_to_body()
.
A rdocx object with xml, gt tables, or paragraphs of text added.
Ellis Hughes [email protected] (ORCID)
if (rlang::is_installed("gt")) { docx_example <- read_docx_ext( filename = "example.docx", path = system.file("doc_examples", package = "officer") ) tab_1 <- gt::gt( gt::exibble, rowname_col = "row", groupname_col = "group" ) add_gt_to_body( docx_example, tab_1, keyword = "Title 1" ) tab_str <- gt::as_word(tab_1) add_str_with_keys( docx_example, str = c("Title 1" = tab_str, "Title 2" = tab_str) ) }
if (rlang::is_installed("gt")) { docx_example <- read_docx_ext( filename = "example.docx", path = system.file("doc_examples", package = "officer") ) tab_1 <- gt::gt( gt::exibble, rowname_col = "row", groupname_col = "group" ) add_gt_to_body( docx_example, tab_1, keyword = "Title 1" ) tab_str <- gt::as_word(tab_1) add_str_with_keys( docx_example, str = c("Title 1" = tab_str, "Title 2" = tab_str) ) }
Check file path for a docx, pptx, or xlsx file extension
check_office_fileext( x, arg = caller_arg(x), fileext = c("docx", "pptx", "xlsx"), allow_null = FALSE, call = caller_env(), ... ) check_docx_fileext(x, arg = caller_arg(x), call = caller_env(), ...) check_pptx_fileext(x, arg = caller_arg(x), call = caller_env(), ...) check_xlsx_fileext(x, arg = caller_arg(x), call = caller_env(), ...)
check_office_fileext( x, arg = caller_arg(x), fileext = c("docx", "pptx", "xlsx"), allow_null = FALSE, call = caller_env(), ... ) check_docx_fileext(x, arg = caller_arg(x), call = caller_env(), ...) check_pptx_fileext(x, arg = caller_arg(x), call = caller_env(), ...) check_xlsx_fileext(x, arg = caller_arg(x), call = caller_env(), ...)
x |
Object to check. |
arg |
Argument name of object to check. Used to improve
|
fileext |
File extensions to allow without error. Defaults to "docx", "pptx", "xlsx". |
allow_null |
If |
call |
The execution environment of a currently
running function, e.g. |
... |
Additional parameters passed to |
Check if x is a rdocx, rpptx, or rxlsx object
check_officer( x, arg = caller_arg(x), what = c("rdocx", "rpptx", "rxlsx"), call = caller_env(), ... ) check_docx(x, arg = caller_arg(x), call = caller_env(), ...) check_pptx(x, arg = caller_arg(x), call = caller_env(), ...) check_xlsx(x, arg = caller_arg(x), call = caller_env(), ...) check_block_list( x, arg = caller_arg(x), allow_empty = FALSE, allow_null = FALSE, call = caller_env() )
check_officer( x, arg = caller_arg(x), what = c("rdocx", "rpptx", "rxlsx"), call = caller_env(), ... ) check_docx(x, arg = caller_arg(x), call = caller_env(), ...) check_pptx(x, arg = caller_arg(x), call = caller_env(), ...) check_xlsx(x, arg = caller_arg(x), call = caller_env(), ...) check_block_list( x, arg = caller_arg(x), allow_empty = FALSE, allow_null = FALSE, call = caller_env() )
x |
Object to check. |
arg |
Argument name of object to check. Used to improve
|
what |
Class names to check |
call |
The execution environment of a currently
running function, e.g. |
... |
Additional parameters passed to |
allow_empty |
If |
allow_null |
If |
Check an object and error if an object is not a data.frame with the required column names to be a summary data.frame created from a rdocx or rpptx object. Optionally check for the number of rows, a specific content_type value, or if tables are included in the document the summary was created from.
check_officer_summary( x, n = NULL, content_type = NULL, summary_type = "doc", tables = FALSE, ..., arg = caller_arg(x), call = parent.frame() )
check_officer_summary( x, n = NULL, content_type = NULL, summary_type = "doc", tables = FALSE, ..., arg = caller_arg(x), call = parent.frame() )
x |
An object to check if it is a data.frame object created with
|
n |
Required number of rows. Optional. If n is more than length 1,
checks to make sure the number of rows is within the range of |
content_type |
Required content_type, e.g. "paragraph", "table cell", or
"image". Optional. Defaults to |
summary_type |
Summary type. Options "doc", "docx", "pptx", "slide", or "layout". "doc" requires the data.frame include a "content_type" column but allows columns for either a docx or pptx summary. |
tables |
If |
... |
Additional parameters passed to |
arg |
Argument name to use in error messages. Defaults to
|
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
Other summary functions:
officer_summary()
,
officer_summary_levels()
combine_docx()
is a variant of officer::body_add_docx()
that allows any
number of input files and supports rdocx objects as well as Word file paths.
Optionally use a separator between files or objects.
Please note that when you create a new rdocx object with this function (or
officer::body_add_docx()
) the added content will not appear in a summary
data frame created with officer_summary()
and is not accessible to other
functions until the document is opened and edited with Microsoft Word. This
is part of how the OOXML AltChunk Class
works and can't be avoided.
combine_docx( ..., docx = NULL, .list = list2(...), pos = "after", sep = NULL, call = caller_env() )
combine_docx( ..., docx = NULL, .list = list2(...), pos = "after", sep = NULL, call = caller_env() )
... |
Any number of additional rdocx objects or docx file paths. |
docx |
A rdocx object or a file path with a docx file extension.
Defaults to |
.list |
Any number of additional rdocx objects or docx file paths passed
as a list. Defaults to |
pos |
where to add the new element relative to the cursor, one of "after", "before", "on". |
sep |
Separator to use docx files. A bare function that takes a rdocx
object as the only parameter, such as officer::body_add_break or another
object passed to |
call |
The execution environment of a currently
running function, e.g. |
A rdocx object.
## Not run: if (interactive()) { docx_path <- system.file("doc_examples", "example.docx", package = "officer") docx <- read_officer(docx_path) combine_docx( docx, docx_path ) } ## End(Not run)
## Not run: if (interactive()) { docx_path <- system.file("doc_examples", "example.docx", package = "officer") docx <- read_officer(docx_path) combine_docx( docx, docx_path ) } ## End(Not run)
convert_docx()
uses rmarkdown::pandoc_convert()
to convert a rodcx object
or Word file to another format with pandoc. If the "to" parameter contains a
file extension, it is assumed to be an output file name. If you want to
convert a file to a Word document, use the input parameter for the path to
the Markdown, HTML, or other file.
convert_docx( docx = NULL, to = NULL, input = NULL, output = NULL, path = NULL, options = NULL, extract_media = TRUE, overwrite = TRUE, quiet = TRUE, ... )
convert_docx( docx = NULL, to = NULL, input = NULL, output = NULL, path = NULL, options = NULL, extract_media = TRUE, overwrite = TRUE, quiet = TRUE, ... )
docx |
A rdocx object or path to a docx file. Optional if input is provided. |
to |
Format to convert to (if not specified, you must specify
|
input |
Character vector containing paths to input files (files must be UTF-8 encoded) |
output |
Output file (if not specified then determined based on format being converted to). |
path |
File path for converted file passed to wd parameter of
|
options |
Character vector of command line options to pass to pandoc. |
extract_media |
If |
overwrite |
If |
quiet |
If |
... |
Arguments passed on to
|
Supported input and output formats are described in the pandoc user guide.
The system path as well as the version of pandoc shipped with RStudio (if running under RStudio) are scanned for pandoc and the highest version available is used.
Executes a call to pandoc using rmarkdown::pandoc_convert()
to
create a file from an officer object or a docx file.
docx_example <- read_officer( system.file("doc_examples/example.docx", package = "officer") ) convert_docx( docx_example, to = "markdown" ) withr::with_tempdir({ convert_docx( docx_example, output = "docx_example.html" ) })
docx_example <- read_officer( system.file("doc_examples/example.docx", package = "officer") ) convert_docx( docx_example, to = "markdown" ) withr::with_tempdir({ convert_docx( docx_example, output = "docx_example.html" ) })
A combined function for setting cursor position with
officer::cursor_reach()
, officer::cursor_bookmark()
, or using a doc_index
value from officer::docx_summary()
. Defaults to using
officer::cursor_end()
, officer::cursor_begin()
,
officer::cursor_backward()
, or officer::cursor_forward()
if keyword, id,
and index are all NULL
.
cursor_docx( docx, keyword = NULL, id = NULL, index = NULL, default = "end", quiet = FALSE, call = caller_env() )
cursor_docx( docx, keyword = NULL, id = NULL, index = NULL, default = "end", quiet = FALSE, call = caller_env() )
docx |
A rdocx object. |
keyword , id
|
A keyword string used to place cursor with
|
index |
A integer matching a doc_index value appearing in a summary of
the docx object created with |
default |
Character string with one of the following options: |
quiet |
If |
call |
The execution environment of a currently
running function, e.g. |
officer::cursor_begin()
, officer::docx_summary()
This function extends officer::docx_dim()
by also returning the body text
dimensions within the margins, the aspect ratio of the page and body, and the
page orientation as a string ("landscape" or "portrait").
dims_docx_ext(docx)
dims_docx_ext(docx)
docx |
A rdocx object to get dimensions for. |
This function uses vctrs::vec_fill_missing()
to convert hierarchically
nested headings and text into a rectangular data.frame. It is an experimental
function that may be modified or removed. At present, it is only used by
officer_tables()
.
fill_with_pattern( x, pattern = "^heading", pattern_col = "style_name", fill_col = "text", col = "heading", direction = c("down", "up", "downup", "updown"), call = caller_env() )
fill_with_pattern( x, pattern = "^heading", pattern_col = "style_name", fill_col = "text", col = "heading", direction = c("down", "up", "downup", "updown"), call = caller_env() )
x |
A input data.frame (assumed to be from |
pattern |
Passed to |
pattern_col |
Name of column to use for pattern matching, Defaults to "style_name". |
fill_col |
Name of column to fill , Defaults to "text". |
col |
Name of new column to fill with values from fill_col, Default: Defaults to "heading" |
direction |
Direction of fill passed to |
call |
The execution environment of a currently
running function, e.g. |
A data.frame with an additional column taking the name from col and the values from the column named in fill_col.
is_officer()
and variants are basic wrappers for inherits()
to check for
object classes created by functions from the officer package. These functions
are intended for internal use but are exposed for use by other extension
developers.
is_officer(x, what = c("rdocx", "rpptx", "rxlsx")) is_rdocx(x) is_rpptx(x) is_block_list(x)
is_officer(x, what = c("rdocx", "rpptx", "rxlsx")) is_rdocx(x) is_rpptx(x) is_block_list(x)
x |
A object to test |
what |
Class or classes passed to |
make_block_list()
extends officer::block_list()
by supporting a list of
inputs and optionally combining parameters with an existing block list (using
the blocks parameter). Unlike officer::block_list()
, make_block_list()
errors if no input parameters are provided.
combine_blocks()
takes any number of block_list
objects and combined them
into a single block_list
. Both functions are not yet working as expected.
make_block_list(blocks = NULL, ..., allow_empty = FALSE, call = caller_env()) combine_blocks(...)
make_block_list(blocks = NULL, ..., allow_empty = FALSE, call = caller_env()) combine_blocks(...)
blocks |
A list of parameters to pass to |
... |
For |
allow_empty |
If |
call |
The execution environment of a currently
running function, e.g. |
Other block list functions:
officer_add_blocks()
officer_add_blocks()
supports adding a list of blocks to rdocx (using
add_blocks_to_body()
) or rpptx objects (using officer::ph_with()
).
add_blocks_to_body()
is a variant of officer::body_add_blocks()
that
allows users to set the cursor position before adding the block list using
cursor_docx()
.
officer_add_blocks( x, blocks, pos = "after", location = NULL, level_list = integer(0), ..., call = caller_env() ) add_blocks_to_body( docx, blocks, pos = "after", keyword = NULL, id = NULL, index = NULL, ... )
officer_add_blocks( x, blocks, pos = "after", location = NULL, level_list = integer(0), ..., call = caller_env() ) add_blocks_to_body( docx, blocks, pos = "after", keyword = NULL, id = NULL, index = NULL, ... )
x |
A rdocx or rpptx object. Required. |
blocks |
set of blocks to be used as footnote content returned by
function |
pos |
where to add the new element relative to the cursor, one of "after", "before", "on". |
location |
If |
level_list |
The list of levels for hierarchy structure as integer values. If used the object is formated as an unordered list. If 1 and 2, item 1 level will be 1, item 2 level will be 2. |
... |
further arguments passed to or from other methods. When
adding a |
call |
The execution environment of a currently
running function, e.g. |
docx |
A rdocx object. |
keyword , id
|
A keyword string used to place cursor with
|
index |
A integer matching a doc_index value appearing in a summary of
the docx object created with |
Other block list functions:
make_block_list()
Unzip a docx or pptx file to a temporary directory, check if the directory
contains a media folder, and copy media files to the directory set by dir. If
a rdocx or rpptx object is provided, files are copied from the temporary
package_dir associated with the object (accessible via x[["package_dir"]]
).
officer_media( filename = NULL, path = NULL, x = NULL, target = "media", list = FALSE, overwrite = TRUE )
officer_media( filename = NULL, path = NULL, x = NULL, target = "media", list = FALSE, overwrite = TRUE )
filename , path
|
File name and path for a |
x |
A rdocx or rpptx object containing one or more media file. |
target |
Folder name or path to copy media files. dir is created if no folder exists at that location. |
list |
If |
overwrite |
If |
officer_media( system.file("doc_examples/example.pptx", package = "officer"), list = TRUE )
officer_media( system.file("doc_examples/example.pptx", package = "officer"), list = TRUE )
officer_properties()
is a variant on officer::doc_properties()
that will
warn instead of error if document properties can't be found
officer_properties(x, values = list(), keep.null = FALSE, call = caller_env())
officer_properties(x, values = list(), keep.null = FALSE, call = caller_env())
x |
A rdocx or rpptx object. |
values |
A named list with new properties to replace existing document properties before they are returned as a named list. |
keep.null |
Passed to |
call |
The execution environment of a currently
running function, e.g. |
A named list of existing document properties or (if values is supplied) modified document properties.
officer_summary()
extends officer::docx_summary()
and other officer
summary functions by handling multiple input types within a single function.
The preserve parameter is supported by officer version >= 0.6.3 (currently
the development version) and it is ignored unless a minimum supported version
of officer is installed.
officer_summary( x, summary_type = "doc", index = NULL, preserve = FALSE, as_tibble = TRUE, call = caller_env() )
officer_summary( x, summary_type = "doc", index = NULL, preserve = FALSE, as_tibble = TRUE, call = caller_env() )
x |
A rdocx or rpptx object passed to |
summary_type |
Summary type. Options "doc", "docx", "pptx", "slide", or "layout". "doc" requires the data.frame include a "content_type" column but allows columns for either a docx or pptx summary. |
index |
slide index |
preserve |
If |
as_tibble |
If |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
A tibble or data frame object.
Other summary functions:
check_officer_summary()
,
officer_summary_levels()
docx_example <- read_officer( system.file("doc_examples", "example.docx", package = "officer") ) officer_summary(docx_example) pptx_example <- read_officer( "example.pptx", system.file("doc_examples", package = "officer") ) officer_summary(pptx_example) officer_summary(pptx_example, "slide", 1)
docx_example <- read_officer( system.file("doc_examples", "example.docx", package = "officer") ) officer_summary(docx_example) pptx_example <- read_officer( "example.pptx", system.file("doc_examples", package = "officer") ) officer_summary(pptx_example) officer_summary(pptx_example, "slide", 1)
This function works with fill_with_pattern()
to help convert hierarchically
organized text, e.g. text with leveled headings or other styles into a
data.frame where new columns hold the value of the preceding (or succeeding)
heading text.
officer_summary_levels( x, levels = NULL, levels_from = "style_name", exclude_levels = NULL, fill_col = "text", direction = c("down", "up", "downup", "updown"), ..., call = caller_env() )
officer_summary_levels( x, levels = NULL, levels_from = "style_name", exclude_levels = NULL, fill_col = "text", direction = c("down", "up", "downup", "updown"), ..., call = caller_env() )
x |
A input data.frame (assumed to be from |
levels |
Levels to use. If |
levels_from |
Column name to use for identifying levels. Defaults to "style_name". |
exclude_levels |
Levels to exclude from process of adding new columns. |
fill_col |
Name of column to fill , Defaults to "text". |
direction |
Direction of fill passed to |
... |
Arguments passed on to
|
call |
The execution environment of a currently
running function, e.g. |
A summary data frame with additional columns based on the supplied levels.
Other summary functions:
check_officer_summary()
,
officer_summary()
Get one or more tables from a rdocx or rpptx object. officer_tables()
returns a list of data frames and officer_table()
returns a single table as
a data frame. These functions are based on example code on extracting Word
document and PowerPoint slides in the officeverse documentation.
Some additional features including the type_convert parameter and the
addition of doc_index values as the default names for the returned list of
tables are based on this blog post by Matt Dray.
officer_tables( x, index = NULL, has_header = TRUE, col = NULL, preserve = FALSE, ..., stack = FALSE, type_convert = FALSE, nm = NULL, call = caller_env() ) officer_table( x, index = NULL, has_header = TRUE, col = NULL, ..., call = caller_env() )
officer_tables( x, index = NULL, has_header = TRUE, col = NULL, preserve = FALSE, ..., stack = FALSE, type_convert = FALSE, nm = NULL, call = caller_env() ) officer_table( x, index = NULL, has_header = TRUE, col = NULL, ..., call = caller_env() )
x |
A rdocx or rpptx object or a data frame created with
|
index |
A index value matching a doc_index value for a table in the
summary data frame, Default: |
has_header |
If |
col |
If col is supplied, |
preserve |
If |
... |
Additional parameters passed to |
stack |
If |
type_convert |
If |
nm |
Names to use for returned list of tables. If |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
A list of data frames or, if stack is TRUE
, a single data frame.
docxtractr::docx_extract_all()
docx_example <- read_docx_ext( filename = "example.docx", path = system.file("doc_examples", package = "officer") ) officer_tables(docx_example) pptx_example <- read_pptx_ext( filename = "example.pptx", path = system.file("doc_examples", package = "officer") ) officer_tables(pptx_example)[[1]]
docx_example <- read_docx_ext( filename = "example.docx", path = system.file("doc_examples", package = "officer") ) officer_tables(docx_example) pptx_example <- read_pptx_ext( filename = "example.pptx", path = system.file("doc_examples", package = "officer") ) officer_tables(pptx_example)[[1]]
Uses the Google Docs, Slides, or Sheets URL to export a file locally, read to
an officer object. If filename is NULL
, exported file is removed after
export.
read_docs_url(url, format = NULL, filename = NULL, path = NULL, quiet = TRUE)
read_docs_url(url, format = NULL, filename = NULL, path = NULL, quiet = TRUE)
url |
A URL for a Google Doc, Google Slides presentation, or Google Sheets. |
format |
File format to use for export URL (typically set
automatically). Options are |
filename |
Destination file name. Optional. If filename is |
path |
Folder path. Optional. |
quiet |
If |
read_officer()
is a variant of officer::read_docx()
,
officer::read_pptx()
, and officer::read_xlsx()
that allows users to read
different Microsoft Office file types with a single function.
read_docx_ext()
, read_pptx_ext()
, and read_xlsx_ext()
are wrappers for
read_officer()
that require the matching input file type. All versions
allow both a filename and path (the officer functions only use a path). If a
rdocx, rpptx, or rxlsx class object is provided to x, the object is checked
based on the fileext parameter and then returned as is.
read_officer( filename = NULL, path = NULL, fileext = c("docx", "pptx", "xlsx"), x = NULL, arg = caller_arg(x), allow_null = TRUE, quiet = TRUE, call = parent.frame(), ... ) read_docx_ext( filename = NULL, path = NULL, docx = NULL, allow_null = FALSE, quiet = TRUE ) read_pptx_ext( filename = NULL, path = NULL, pptx = NULL, allow_null = FALSE, quiet = TRUE ) read_xlsx_ext( filename = NULL, path = NULL, xlsx = NULL, allow_null = FALSE, quiet = TRUE )
read_officer( filename = NULL, path = NULL, fileext = c("docx", "pptx", "xlsx"), x = NULL, arg = caller_arg(x), allow_null = TRUE, quiet = TRUE, call = parent.frame(), ... ) read_docx_ext( filename = NULL, path = NULL, docx = NULL, allow_null = FALSE, quiet = TRUE ) read_pptx_ext( filename = NULL, path = NULL, pptx = NULL, allow_null = FALSE, quiet = TRUE ) read_xlsx_ext( filename = NULL, path = NULL, xlsx = NULL, allow_null = FALSE, quiet = TRUE )
filename , path
|
File name and path. Default: |
fileext |
File extensions to allow without error. Defaults to "docx", "pptx", "xlsx". |
x |
A rdocx, rpptx, or rxlsx class object If x is provided, filename and
path are ignored. Default: |
arg |
Argument name of object to check. Used to improve
|
allow_null |
If |
quiet |
If |
call |
The execution environment of a currently
running function, e.g. |
... |
Additional parameters passed to |
docx , pptx , xlsx
|
A rdocx, rpptx, or rxlsx class object passed to the x
parameter of |
A rdocx, rpptx, or rxlsx object.
reduce_officer()
is a wrapper for purrr::reduce()
.
reduce_officer( x = NULL, .f = function(x, value, ...) { vec_add_to_body(x, value = value, ...) }, value = NULL, ..., .path = NULL )
reduce_officer( x = NULL, .f = function(x, value, ...) { vec_add_to_body(x, value = value, ...) }, value = NULL, ..., .path = NULL )
x |
File path or officer object. |
.f |
Any function taking an officer object as the first parameter and a
value as the second parameter, Defaults to anonymous function:
|
value |
A vector of values that are support by the function passed to
.f, Default: |
... |
Additional parameters passed to |
.path |
If .path not |
A rdocx, rpptx, or rxlsx object.
purrr::reduce()
, vec_add_to_body()
## Not run: if (interactive()) { x <- reduce_officer(value = LETTERS) officer_summary(x) } ## End(Not run)
## Not run: if (interactive()) { x <- reduce_officer(value = LETTERS) officer_summary(x) } ## End(Not run)
use_doc_version()
is inspired by usethis::use_version()
and designed to
support semantic versioning for Microsoft Word or PowerPoint documents. The
document version is tracked as a custom file property and a component of the
document filename. If filename is supplied and the filename contains a
version number, the function ignores any version number stored as a document
property and overwrites the property with the new incremented version number.
doc_version()
is a helper function that returns the document version based
on a supplied filename or document properties.
The internals for this function are adapted from the
internal idesc_bump_version()
function authored by Csárdi Gábor for the
{desc}
package.
use_doc_version( filename = NULL, x = NULL, which = NULL, save = TRUE, sep = ".", property = "version", prefix = NULL, path = NULL, ..., call = caller_env() ) doc_version( filename = NULL, x = NULL, sep = ".", property = "version", allow_new = TRUE, .default = c(0, 1, 0), call = caller_env() )
use_doc_version( filename = NULL, x = NULL, which = NULL, save = TRUE, sep = ".", property = "version", prefix = NULL, path = NULL, ..., call = caller_env() ) doc_version( filename = NULL, x = NULL, sep = ".", property = "version", allow_new = TRUE, .default = c(0, 1, 0), call = caller_env() )
filename |
Filename for a Word document or PowerPoint presentation. Optional if x is supplied, however, it is required to locally save a file with an updated version name. |
x |
A rdocx or rpptx object. Optional if filename is supplied. |
which |
A string specifying which level to increment, one of: "major", "minor", "patch", "dev". |
save |
If |
sep |
Character separating version components. Defaults to ".". "-" is also supported. |
property |
Property name optionally containing a version number. Defaults to "version". |
prefix |
Property name to use as prefix for new filename, e.g.
"modified" to use modified date/time as the new filename prefix. If prefix
is identical to property and the filename does not already include a
version number, version is added to the filename as a prefix instead of a
postfix. Defaults to |
path |
Passed to |
... |
Additional parameters passed to |
call |
The execution environment of a currently
running function, e.g. |
allow_new |
If |
.default |
Specification for initial version. |
Invisibly return a rdocx or rpptx object with an updated version property.
add_to_body()
vec_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()
.
vec_add_to_body( docx, ..., .sep = NULL, .pos = "after", .size = NULL, .call = caller_env() )
vec_add_to_body( docx, ..., .sep = NULL, .pos = "after", .size = NULL, .call = caller_env() )
docx |
A rdocx object. |
... |
Arguments passed on to
|
.sep |
A bare function, such as officer::body_add_break or another
object passed to |
.pos |
String passed to pos parameter if |
.size |
Desired output size. |
.call |
The execution environment of a currently
running function, e.g. |
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) 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) }
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) 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) }
Write a rdocx, rpptx, or rxlsx object to a file
write_officer(x, path, overwrite = TRUE, modified_by = Sys.getenv("USER"), ...)
write_officer(x, path, overwrite = TRUE, modified_by = Sys.getenv("USER"), ...)
x |
A rdocx, rpptx, or rxlsx object to save. Document properties won't be set for rxlsx objects. |
path |
File path. |
overwrite |
If |
modified_by |
If the withr package is installed, modified_by overrides
the default value for the lastModifiedBy property assigned to the output
file by officer. Defaults to |
... |
Arguments passed on to
|
Returns the input object (invisibly) and writes the rdocx, rpptx, or rxlsx object to a file with a name and location matching the provided path.