0

I have written a function to serve as a wrapper around ggsave() to save plots as an EMF (for use with PowerPoint).

emf_save <- function(ggp) {
  filename <- glue::glue("{here()}",
                         "/",
                         "{deparse(substitute(ggp))}.emf")
  
  ggsave(
    plot = ggp,
    filename = filename,
    height = 5.2,
    width = 11.5,
    units = "in",
    device = devEMF::emf()
  )
  
  dev.off()
  print(paste0("Saved ", filename))
}

It accepts a ggplot2 object and saves an EMF file with the object's name. The problem is, whenever I call this function, it also saves a file called Rplot.emf in the project folder.

How do I prevent this file generation?

user8701090
  • 48
  • 1
  • 5
  • Hmm, so far I can reproduce the problem with a png if I use `device = png()`. But not if I use `device = "png"`. – aosmith Jun 24 '20 at 19:10
  • Oh, I think I see. Try using `device = devEMF::emf` instead of `device = devEMF::emf()`. – aosmith Jun 24 '20 at 19:16
  • @aosmith That's probably it. When I use `devEMF::emf()` the function executes. I'll give it a try and close the question if it is resolved. Thanks! – user8701090 Jun 25 '20 at 01:49
  • That didn't work; removing the parentheses broke the function. It did, however, prevent the Rplot.emf file from being generated. The error it gave was: `Error in dev(filename = filename, width = dim[1], height = dim[2], ...) : unused argument (filename = filename)` – user8701090 Jun 25 '20 at 12:27
  • Is your ggplot2 version up to date? I think there was an issue about this (file vs filename) that has been fixed. It worked for me using devEMF with a current version of ggplot2. [This](https://github.com/tidyverse/ggplot2/issues/3807) is the issue that I thought might be relevant. – aosmith Jun 25 '20 at 14:27
  • I updated, and the error persists. `#> ggplot2 * 3.3.2 2020-06-19 [1] CRAN (R 4.0.2)` – user8701090 Jun 27 '20 at 22:50
  • Switching to using `device = devEMF::emf` works for me without error on Windows 7 R 4.0.0 with ggplot2 3.3.2 and doesn't save the extra Rplot.emf plot. I hope you figure it out! – aosmith Jun 30 '20 at 14:19

0 Answers0