export_scatter_plots: Export MR Scatter Plots to Disk

export_scatter_plotsR Documentation

Export MR Scatter Plots to Disk

Description

Saves plots stored in an MRScatterPlots object to a directory. You can export all plots or filter to a specific outcome, a specific exposure, or a specific outcome-exposure pair.

Usage

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

Arguments

object

An MRScatterPlots object returned by plot_mr_scatter().

save_dir

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

file_type

Character string; output format passed to the corresponding grDevices device. 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.

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 follow the pattern Scatter_<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)
plots    <- plot_mr_scatter(MR_input_data = input1, summary_df = outcome1)

# Inspect what is stored; for fi_49item this prints:
#   [1] fi_49item :: Zn

# Retrieve the exact outcome/exposure labels stored in the object
out_name <- plots@outcomes[1]   # "fi_49item"
exp_name <- plots@exposures[1]  # "Zn"

# Export all plots to tempdir() (commented — writes to disk)
export_scatter_plots(plots, save_dir = tempdir())

# Export one outcome only
export_scatter_plots(plots, save_dir = tempdir(), outcome = out_name)

# Export one exposure only
export_scatter_plots(plots, save_dir = tempdir(), exposure = exp_name)

# Export one specific pair
export_scatter_plots(plots, save_dir = tempdir(), outcome = out_name, exposure = exp_name)


autoMR documentation built on July 15, 2026, 1:07 a.m.