Skip to contents

Converts user-supplied width, height, units, and dpi into:

  • integer pixel sizes (for raster devices, e.g., PNG/JPEG/TIFF), and

  • floating-point inch sizes (for vector devices, e.g., PDF/SVG).

Opens a base graphics device (png/jpeg/tiff/bmp/pdf/svg), prints the ggplot, and closes the device. Raster devices use pixels + res; vector devices use inches.

Uses ragg::agg_png(), ragg::agg_jpeg(), or ragg::agg_tiff() for high-quality raster output. Requires ragg in Suggests. This helper checks availability with rlang::is_installed() and never attaches ragg.

Uses svglite::svglite() for clean SVG output. Requires svglite in Suggests. Checks availability with rlang::is_installed() and never attaches svglite.

If there is exactly one unique non-NA date, return it as YYYY-MM-DD. If there are multiple, return maxDate_minDate (both formatted YYYY-MM-DD). Errors if there are no non-NA dates or if date_vec is not Date/POSIXt.

Produces a conservative, filesystem-friendly slug:

  • Lowercases the string.

  • Replaces any whitespace (one or more) with underscores.

  • Replaces any character not in [a-z0-9._-] with underscores.

  • Collapses multiple underscores to a single underscore.

  • Trims leading/trailing underscores and dots.

Usage

econ_viz_dims(width, height, units, dpi)

econ_viz_save_grdevices(plt, outpath, ext, dims, bg, dpi)

econ_viz_save_ragg(plt, outpath, ext, dims, bg, dpi)

econ_viz_save_svglite(plt, outpath, ext, dims)

make_file_name_date(date_vec)

make_metadata_vec(df_text, na_token = "na", warn_all_na = TRUE)

sanitize_filename(x)

Arguments

width, height

Numeric scalars: requested size.

units

Character scalar: one of "px", "in", "cm", "mm".

dpi

Resolution (passed as res).

plt

A ggplot object to print.

outpath

File path (including extension) to write to.

ext

Lowercased extension; must be "svg".

dims

List from econ_viz_dims(): width_in, height_in (inches).

bg

Background color (e.g., "white", "transparent").

date_vec

A Date vector (POSIXt allowed; coerced to Date).

df_text

Data frame of *_text columns.

na_token

Token to use when a column is all NA.

warn_all_na

If TRUE, warn when a column is all NA.

x

A character vector (will be coerced via as.character()).

Value

A list with:

  • width_in, height_in (numeric, inches)

  • width_px, height_px (integer, pixels)

A character stem like "2025-05-01" or "2025-05-01_2021-05-01".

Character vector of tokens in column order.

A character vector of sanitized filename stems, same length as x.

Details

This is used by the various econanalyzr visualization savers to normalize sizing regardless of which graphics backend is chosen.

This does not add an extension; you can append e.g. ".csv" afterward. NA inputs are returned as NA.