Skip to contents

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.

Usage

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,
  ...
)

Arguments

x

Object to write. Can be a ggplot, flextable, data.frame, or list of such objects. Lists can be arbitrarily nested and will be processed according to object type.

file

Character string specifying the base file name (without extension). The actual extension will be determined by the formats.* arguments.

formats.ft

Character vector of file formats for flextable objects. Default is "png". Supported formats include: "png", "docx", "pptx", "html". See ftwrite for details.

formats.gg

Character vector of file formats for ggplot objects. Default is "png". Supported formats: "png", "pdf". See ggwrite for details.

formats.data

Character vector of file formats for data frame objects. Default is "rds". This is passed to NMwriteData as the format.write argument. Common formats include: "rds", "csv", "fst".

path.format

Character string or function defining the output path. A template using %model, %script, and %name placeholders, or a function as returned by pathFormat. Passed to pathFormat. Default (NULL) uses file as is.

subdir

Character string specifying a subdirectory within the path structure. This is passed to the path function generated from path.format.

stamp.format

A template for the stamp text using the placeholders %script, %time, %model, and %file, with "\n" for line breaks. Default is "%file\n%script\n%model\n%time". See createStamp.

stamp.funs

Optional named list of functions (elements script, time, model, file) used to preprocess the values before they are inserted into stamp.format. See createStamp.

stamp.gg.style

A list of formatting options for the stamp text, passed to ggstamp. For ggplot objects, elements accepted by ggplot2::element_text() (e.g. size, colour, hjust).

stamp.ft.style

A list of formatting options for the stamp, passed to ftstamp. Valid elements: size, align, space, bg, color.

fun.path

Deprecated. Use path.format.

...

Additional arguments passed to the specific writer functions (ggwrite, ftwrite, or NMwriteData). Common arguments include:

  • script: Path to the script file (for stamping)

  • time: Time stamp (for stamping)

  • model: Model identifier (for stamping and path construction)

  • save: Logical, whether to save the output (default TRUE)

  • show: Logical, whether to show/print the output (default !save)

  • canvas: Canvas size for plots (see canvasSize)

  • quiet: Logical, suppress messages (default FALSE)

Value

Invisibly returns the result from the called writer function. The specific return value depends on which writer function was dispatched to.

Details

The function dispatches to appropriate writer functions based on object class:

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.

Object Type Detection

The function determines object type in the following order:

  1. Check if flextable

  2. Check if data.frame

  3. Check if ggplot or gtable (using is.gg)

  4. Check if list (and not a plot object)

  5. If none of the above, throw an error

Trace Attributes

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.

See also

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

Examples

if (FALSE) { # \dontrun{
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")
} # }