export_forest_plots: Export MR Forest Plots to Disk

export_forest_plotsR Documentation

Export MR Forest Plots to Disk

Description

Saves plots stored in a GWASForestPlots or MRForestPlots object to a directory using ggplot2::ggsave(). You can export all plots or filter to a specific outcome, a specific exposure, or a specific outcome-exposure pair.

Usage

export_forest_plots(
  object,
  save_dir = tempdir(),
  file_type = "png",
  width = 8,
  height = 6,
  dpi = 300,
  outcome = NULL,
  exposure = NULL
)

Arguments

object

A GWASForestPlots or MRForestPlots object returned by GWAS_forest() or MR_forest().

save_dir

Character string; directory to write files into. Must already exist. Defaults to tempdir().

file_type

Character string; output format passed to ggplot2::ggsave(). One of "pdf", "png", "jpeg", or "tiff". Default is "png".

width

Numeric; plot width in inches. Default is 8.

height

Numeric; plot height in inches. Default is 6.

dpi

Integer; resolution in dots per inch (ignored for PDF). Default is 300.

outcome

Optional character string; if supplied, only plots whose outcome matches this value are exported.

exposure

Optional character string; if supplied, only plots whose exposure matches this value are exported.

Details

Filtering logic:

  • Both outcome and exposure supplied — exports the single plot for that exact pair.

  • outcome only — exports all exposures for that outcome.

  • exposure only — exports all outcomes for that exposure.

  • Neither supplied — exports every plot in the object.

File names are prefixed with Instrument_forest_ for GWASForestPlots objects and Method_forest_ for MRForestPlots objects, followed by <Outcome>_<Exposure>.<file_type> with spaces replaced by underscores.

Value

Invisibly returns object so calls can be chained.

Examples


data("fi_49item")
input1   <- harmonize_mr_data(df = fi_49item)$input_df
outcome1 <- run_mr_analysis(MR_input_data = input1)

gwas_plots <- GWAS_forest(MR_input_data = input1, report_form = "Beta")
mr_plots   <- MR_forest(summary_df = outcome1, effect = "Beta")

# Inspect what is stored; for fi_49item both objects print:
#   [1] fi_49item :: Zn

# Retrieve the exact outcome/exposure labels stored in the object
gwas_out <- gwas_plots@outcomes[1]   # "fi_49item"
gwas_exp <- gwas_plots@exposures[1]  # "Zn"
mr_out   <- mr_plots@outcomes[1]     # "fi_49item"
mr_exp   <- mr_plots@exposures[1]    # "Zn"

# Export all instrument-level plots as PDF (commented — writes to disk)
export_forest_plots(gwas_plots, save_dir = tempdir())

# Export one outcome only
export_forest_plots(gwas_plots, save_dir = tempdir(), outcome = gwas_out)

# Export one exposure only
export_forest_plots(mr_plots, save_dir = tempdir(), exposure = mr_exp)

# Export one specific pair
export_forest_plots(mr_plots, save_dir = tempdir(), outcome = mr_out, exposure = mr_exp)


autoMR documentation built on June 12, 2026, 9:07 a.m.