Inserts an image into a new worksheet, optionally with title, source, and metadata. Images must be provided either as a ggplot object or as a path to the image file.

insert_worksheet_image(
  wb,
  sheetname,
  image,
  title = NULL,
  source = NULL,
  metadata = NULL,
  width = NULL,
  height = NULL,
  startrow = 3,
  startcol = 3,
  units = "in",
  dpi = 300
)

Arguments

wb

workbook object to write new worksheet in

sheetname

Name of the sheet where the image should be inserted

image

Image, either a ggplot object or the path to an existing image.

title

Title of the image. Can be NULL

source

Source associated with the image. Can be NULL.

metadata

Metadata associated with the image. Can be NULL.

width

width of figure

height

height of figure

startrow

row coordinate of upper left corner of figure

startcol

column coordinate of upper left corner of figure

units

unit of measurement (default: in)

dpi

image resolution (default: 300)

Note

The function does not write the result into a .xlsx file. A separate call to openxlsx::saveWorkbook() is required. A temporary file is created for inputs of type gg, ggplot or histogram object at path given by `tempfile()`.

Examples

figure <- ggplot2::ggplot(mtcars, ggplot2::aes(x = disp)) +
  ggplot2::geom_histogram()

wb <- openxlsx::createWorkbook()
insert_worksheet_image(wb, sheetname = "ggplot image",
                       image = figure, width = 3.5, height = 5.5)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.