| writer | R Documentation |
A unified interface for ggwrite, ftwrite, and
NMwriteData that automatically detects object type and
applies the appropriate writing function with consistent file naming and
stamping conventions.
writer(
x,
file,
formats.ft,
formats.gg,
formats.data,
path.format = NULL,
subdir = NULL,
stamp.format = NULL,
stamp.funs = NULL,
stamp.gg.style = NULL,
stamp.ft.style = NULL,
fun.path = NULL,
...
)
x |
Object to write. Can be a |
file |
Character string specifying the base file name (without extension).
The actual extension will be determined by the |
formats.ft |
Character vector of file formats for flextable objects.
Default is |
formats.gg |
Character vector of file formats for ggplot objects.
Default is |
formats.data |
Character vector of file formats for data frame objects.
Default is |
path.format |
Character string or function defining the output path.
A template using |
subdir |
Character string specifying a subdirectory within the path structure.
This is passed to the path function generated from |
stamp.format |
A template for the stamp text using the
placeholders |
stamp.funs |
Optional named list of functions (elements |
stamp.gg.style |
A list of formatting options for the stamp text,
passed to |
stamp.ft.style |
A list of formatting options for the stamp,
passed to |
fun.path |
Deprecated. Use |
... |
Additional arguments passed to the specific writer functions
(
|
The function dispatches to appropriate writer functions based on object class:
flextable objects → ftwrite
data.frame objects → datwrite (which wraps
NMwriteData)
ggplot/gtable objects or lists → ggwrite
For lists of objects, each element is written separately with appropriate naming. The function intelligently handles nested lists by flattening them and constructing meaningful file names from the list structure.
File paths are constructed using path.format, which can organize
outputs by model, file name, or custom patterns. This is particularly useful in
modeling workflows where outputs need to be organized by model number or name.
Invisibly returns the result from the called writer function. The specific return value depends on which writer function was dispatched to.
The function determines object type in the following order:
Check if flextable
Check if data.frame
Check if ggplot or gtable (using is.gg)
Check if list (and not a plot object)
If none of the above, throw an error
If the object x has traceit attributes (added via traceit),
those attributes will be automatically extracted and used by the writer function,
unless explicitly overridden by arguments passed to writer.
ggwrite for writing ggplot objects,
ftwrite for writing flextable objects,
datwrite for writing data frames,
lwrite for writing lists with subdirectory organization,
pathFormat for path format options,
traceit for adding traceit attributes to objects
## Not run:
library(ggplot2)
library(flextable)
# Write a ggplot
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
writer(p1, file = "myplot", formats.gg = c("png", "pdf"))
# Write a flextable
ft1 <- flextable(head(mtcars))
writer(ft1, file = "mytable", formats.ft = c("png", "docx"))
# Write a data frame
writer(mtcars, file = "mydata", formats.data = "csv")
# Use with model organization
writer(p1, file = "gof", model = "run001", path.format = "%model/%name_%model")
# Writes to: run001/gof_run001.png
# Write with stamping
writer(p1, file = "myplot",
script = "analysis.R",
time = Sys.time(),
model = "run001")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.