Function should add logo to plot with magick; prints empty tibble instead

I'm trying to write a function that takes a ggplot and draws an image of said plot with a logo composite in the bottom right corner.

add_logo <- function(plot, width = 600, height = 400, logo_scale = 7){
  frame <- image_graph(width = width, height = height)
  plot + theme(plot.margin = unit(c(0.5, 0.5, 1.5, 0.5), "lines"))
  logo <- image_read(filepath)
  plot_height <- magick::image_info(frame)$height
  plot_width <- magick::image_info(frame)$width
  logo <- image_scale(logo, as.character(plot_width/logo_scale))
  logo_height <- magick::image_info(logo)$height
  logo_width <- magick::image_info(logo)$width

  x_pos <- plot_width - logo_width - 0.01 * plot_width
  y_pos <- plot_height - logo_height - 0.01 * plot_height
  comp <- image_composite(frame, logo, offset = paste0("+", x_pos, "+", y_pos))

When I call add_logo(plot) it prints an empty tibble to the console. However, the intended image is printed when I explicitly run the code in the function (not as a function call).

Maybe it's because I've been reading more about environments recently, but that difference in behavior makes me think there could be a disconnect in environments and the magick package when calling the function?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.