Automatically detects the appropriate output method based on the rendering context and input type. Simplifies workflows by providing a single function that works for both Quarto/knitr rendering (HTML tabsets/tables) and officer-based DOCX generation.
Usage
crowd_output(
plot_list,
path = "crowd_output.docx",
force_format = c("auto", "html", "docx"),
...
)Arguments
- plot_list
Either:
A named list of ggplot2 objects (for HTML plots)
A named list of mschart objects (for DOCX plots)
A
saros_officer_plotsobject (fromcrowd_plots_as_officer())A data.frame or list of data.frames (for tables)
- path
Character. File path for DOCX output (e.g.,
"output.docx"). Only used when not in a knitr/Quarto rendering context. Default:"crowd_output.docx".- force_format
Character. Force a specific output format:
"auto"(default): Auto-detect based on context"html": Force HTML tabset output viacrowd_plots_as_tabset()"docx": Force DOCX file output viacrowd_plots_as_docx()
- ...
Additional arguments passed to
crowd_plots_as_tabset()orcrowd_plots_as_docx()depending on the detected/forced format.
Value
In knitr/Quarto context: Invisibly returns
NULL(side effect: prints tabset markdown)Outside knitr context: Invisibly returns the DOCX file path
Details
Context Detection:
The function uses getOption("knitr.in.progress") to detect if code is
running within a knitr/Quarto rendering context:
In knitr/Quarto → Generates HTML tabset via
crowd_plots_as_tabset()Outside knitr → Writes DOCX file via
crowd_plots_as_docx()
This allows the same code to work in multiple contexts:
Quarto → HTML rendering
Quarto → DOCX rendering (still uses HTML output in document)
R script → Officer-based DOCX generation
Input Type Detection: The function automatically detects and handles different input types:
ggplot2 objects → Uses
crowd_plots_as_tabset()orcrowd_plots_as_docx()mschart objects → Uses
crowd_plots_as_docx()saros_officer_plots→ Usescrowd_plots_as_docx()data.frame/tables → Uses
crowd_tables_as_tabset()or writes to DOCX
Typical Workflow:
# In a Quarto document - works for both HTML and DOCX output formats
plots <- makeme(
data = ex_survey,
dep = b_1:b_3,
crowd = c("target", "others"),
mesos_var = "f_uni",
mesos_group = "Uni of A",
type = if (is_rendering()) "cat_plot_html" else "cat_plot_docx"
)
crowd_output(plots) # Auto-detects context and renders appropriatelySee also
crowd_plots_as_tabset()for HTML tabset generationcrowd_plots_as_docx()for DOCX file creationis_rendering()for context detection helpermakeme()for creating plots with crowd parameter
Examples
if (FALSE) { # \dontrun{
# Example 1: In a Quarto document (auto-detects HTML context)
plots <- makeme(
data = ex_survey,
dep = b_1:b_3,
crowd = c("target", "others"),
mesos_var = "f_uni",
mesos_group = "Uni of A",
type = if (is_rendering()) "cat_plot_html" else "cat_plot_docx"
)
crowd_output(plots)
# Example 2: In an R script (auto-detects non-knitr context, writes DOCX)
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"
)
crowd_output(plots, path = "my_report.docx")
# Example 3: Force specific format
crowd_output(plots, force_format = "docx", path = "forced_output.docx")
} # }