$ show tomwenseleers/export
The export package is an R library designed for streamlined export of R graphs and statistical output to various formats including bitmap (PNG/JPG/TIF), vector (SVG/PDF/EPS), Microsoft Office/LibreOffice (PPTX/DOCX/XLSX), CSV, and HTML/LaTeX. It provides a consistent interface where users can export plots by passing plot objects directly, using the currently active graph, or supplying a function to generate the plot. The architecture follows standard R package conventions with modular design: separate files handle different export types (graph2*.R for graphs, table2*.R for tables), while shared utility functions in utils.R manage common tasks like dimension calculation, plot resolution, and template selection. The package integrates with downstream libraries such as officer, rvg, flextable, xtable, broom, and stargazer to perform the actual export operations, adding sensible defaults and unified parameter handling on top of these tools.
The LLM identified 3 files worth highlighting for reuse:
The plot_dimensions function calculates width, height, and aspect ratio for graphs based on flexible input combinations (width, height, aspect ratio, scaling). It handles edge cases with proper defaults and validation, demonstrating clean parameter handling and mathematical precision for graphics layout. This self-contained utility solves a common problem in R graphics programming and could be extracted for use in any plotting-related project with zero modification.
The resolve_plot function intelligently determines what graphical object to plot by checking explicit inputs (object/function), then falling back to the last plotted object if it's a plottable type (ggplot, lattice, etc.). It solves the common problem of flexible plot specification in R graphics APIs with graceful fallback behavior. Along with its dependencies (is_plot_object, captureplot), this utility represents a reusable pattern for handling multiple input methods in graphics APIs.
The besttemplate function selects the most appropriate PowerPoint or Word template based on graph dimensions and margins. It implements non-trivial logic to iterate through standard paper sizes (A5-A1) to find the best fit, considering both landscape and portrait orientations. This utility demonstrates thoughtful algorithmic design for document layout automation and could be adapted for template selection in any document generation system.