Convert List of Plots to officer-Compatible Format
Source:R/crowd_plots_as_officer.R
crowd_plots_as_officer.RdPrepares a named list of mschart objects for insertion into a Word document
using officer. Typically used with plots generated by makeme() with
crowd parameter and type = "cat_plot_docx".
Arguments
- plot_list
A named list of mschart objects. Names become section labels. Typically created with:
makeme(crowd = c("target", "others"), type = "cat_plot_docx", docx_return_object = TRUE).- extract_metadata
Logical. If
TRUE(default), extracts and includes metadata (likedep_label_prefix) from each plot object.
Value
A list with class "saros_officer_plots" containing:
plots: Named list of mschart objects ready for officer insertionmetadata: List of metadata for each plot (ifextract_metadata = TRUE), including plot names, dep_label_prefix, and other attributes;NULLifextract_metadata = FALSEn_plots: Integer count of valid mschart objects after filtering
Details
This function validates and structures plot objects for Word document assembly.
Unlike crowd_plots_as_tabset() which generates Quarto markdown directly,
this function returns a structured R object that can be used programmatically
with officer to build Word documents.
Typical Workflow:
Generate mschart objects with
makeme(..., type = "cat_plot_docx", docx_return_object = TRUE, crowd = ...)Pass the list to
crowd_plots_as_officer()for validation and structuringUse the returned object to insert plots into a Word document via officer
Metadata Extraction:
When extract_metadata = TRUE, the function extracts:
dep_label_prefix: Main question text (fromget_dep_label_prefix())name: Plot name from the listAdditional attributes attached to the plot object
See also
crowd_plots_as_tabset()for the Quarto/HTML equivalentmakeme()for creating plots with crowd parameterget_dep_label_prefix()for retrieving main question metadatamschart::body_add_chart()for inserting charts into Word documents
Examples
if (FALSE) { # \dontrun{
# Generate mschart objects for Word
plots <- makeme(
data = ex_survey,
dep = b_1:b_3,
crowd = c("target", "others"),
mesos_var = "f_uni",
mesos_group = "Uni of A",
type = "cat_plot_docx",
docx_return_object = TRUE
)
# Prepare for officer assembly
officer_plots <- crowd_plots_as_officer(plots)
# Use in officer workflow
library(officer)
doc <- read_docx()
for (plot_name in names(officer_plots$plots)) {
doc <- doc |>
body_add_par(plot_name, style = "heading 2") |>
body_add_par(officer_plots$metadata[[plot_name]]$dep_label_prefix) |>
mschart::body_add_chart(officer_plots$plots[[plot_name]])
}
print(doc, target = "output.docx")
} # }