| lwrite | R Documentation |
Writes nested lists containing plots, tables, and data frames to files with intelligent handling of list structure, automatic naming, and optional subdirectory organization. This is particularly useful for organizing multiple related outputs from a single analysis or model run.
lwrite(
list,
model,
dir = ".",
path.format,
lists.as.subdirs = FALSE,
name.all.pdf = "main_all.pdf",
structure,
...
)
list |
A list (potentially nested) containing output objects: |
model |
Character string or model object specifying the model name/identifier.
Used for organizing output directories and stamping. If a list (model object)
is provided, the |
dir |
Character string specifying the base output directory. Default is
|
path.format |
Character string or function defining the output path format.
Passed to |
lists.as.subdirs |
Logical. If
|
name.all.pdf |
Character string specifying the filename to use when writing
all plots to a single PDF file. Only used when the file name is empty and
plots are being combined into one PDF. Default is |
structure |
Deprecated. Use |
... |
Additional arguments passed to
|
The function processes lists recursively with the following behavior:
Separates plot objects from non-plot objects
Combines plots into a single multi-page or multi-file output (see ggwrite)
Writes each non-plot object individually
Handles nested lists according to lists.as.subdirs
Invisibly returns a list of results from the writer calls.
The structure mirrors the input list structure.
The function intelligently flattens nested lists while preserving meaningful names:
Named list elements use their names in output file names
Plots in the same list are combined (behavior depends on ggwrite settings)
Non-plot objects (tables, data frames) are written individually
Nesting deeper than 2 levels will cause an error
Output file names are constructed from:
The name at each level of the list hierarchy
The model identifier (if provided and if path.format uses it)
Automatic numbering for unnamed elements
For example, with lists.as.subdirs = FALSE:
list(
section1 = list(
plot1 = p1,
table1 = ft1
)
)
produces: section1_plot1.png, section1_table1.png
With lists.as.subdirs = TRUE, the same structure produces:
section1/plot1.png, section1/table1.png
All ggplot objects within the same list level are collected and written
together using ggwrite. This allows creating multi-page PDFs or
sequentially numbered PNG files from a list of plots.
writer for the underlying writing function,
ggwrite for plot-specific options,
ftwrite for table-specific options,
datwrite for data frame writing,
pathFormat for path format options,
is.listnotplot for list detection logic
## Not run:
library(ggplot2)
library(flextable)
# Create some outputs
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(hp, mpg)) + geom_point()
ft1 <- flextable(head(mtcars))
df1 <- mtcars
# Simple list
outputs <- list(
scatter_wt = p1,
scatter_hp = p2,
summary_table = ft1,
data = df1
)
lwrite(outputs, model = "run001", dir = "outputs",script="myscript")
# Creates in outputs/run001/:
# scatter_wt_run001.png
# scatter_hp_run001.png
# summary_table_run001.png
# data_run001.rds
# Nested list with subdirectories
nested_outputs <- list(
diagnostics = list(
gof1 = p1,
gof2 = p2
),
tables = list(
summary = ft1
)
)
lwrite(nested_outputs,
model = "run001",
dir = "outputs",
lists.as.subdirs = TRUE)
# Creates:
# outputs/run001/diagnostics/gof1_run001.png
# outputs/run001/diagnostics/gof2_run001.png
# outputs/run001/tables/summary_run001.png
# With stamping
lwrite(outputs,
model = "run001",
dir = "outputs",
script = "analysis.R",
time = Sys.time())
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.