Skip to content

Use devEMF::emf instead of grDevices::win.metafile for .wmf/.emf #4541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bwiernik opened this issue Jul 3, 2021 · 8 comments
Closed

Use devEMF::emf instead of grDevices::win.metafile for .wmf/.emf #4541

bwiernik opened this issue Jul 3, 2021 · 8 comments

Comments

@bwiernik
Copy link
Contributor

bwiernik commented Jul 3, 2021

Currently, ggsave() uses the grDevices::win.metafil() device for output to .wmf/.emf. This device doesn't seem to work very well—when I use it, the actual plot is rendered at half the size of the canvas in the upper left corner. Like this:

image

The devEMF::emf() device seems to work much better:

image

I think it would be better to render wmf/emf files using the devEMF device. It would add a dependency (perhaps in Suggests with a warning or error to prompt installation?), but would be worth it for much better emf generation. If that would be acceptable, I can make a PR.

Example emf files for the two devices are attached.
emf_output.zip

Reprex:

library(ggplot2)
g <- ggplot(cars, aes(x = speed, y = dist)) +
  geom_point()
ggsave("grDevices.emf", g, height = 7, width = 7)
ggsave("devEMF.emf",    g, height = 7, width = 7,
       device = {\(filename, ...) devEMF::emf(file = filename, ...)})

Created on 2021-07-03 by the reprex package (v2.0.0)

@yutannihilation
Copy link
Member

We already use ragg for high-quality image output, so the idea of using another external package for the same purpose can be acceptable itself. However, I think the question is whether ggplot2 supports .emf/.wmf format that eagerly. Honestly, while I used R on Windows for 5 years, I've never heard of the format until #4521. I'm not totally against your idea, but, considering no maintainer uses Windows actively, I'm afraid it's virtually impossible to maintain the feature reliably (as you already notice .emf/.wmf-related features are broken twice recently: #4521 and #4539).

@thomasp85 @clauswilke
Any thoughts?

@bwiernik
Copy link
Contributor Author

bwiernik commented Jul 5, 2021

EMF is basically the only vector format that works reliably in Microsoft Word on either Windows or Mac--SVG and PDF support are very buggy

@clauswilke
Copy link
Member

@yutannihilation: I think we should fix any bugs that prevent people from using the graphics device of their choice, but otherwise I'd be wary of adding dependencies to relatively obscure external packages. Also note that users would likely not have devEMF installed unless they already know about it, so out of the box ggplot2 would basically never use this functionality in practice.

@bwiernik Please be aware that another way to fix this issue is for devEMF::emf() to add the ... argument. You could file a bug with the maintainer. There's no public bug tracker, as far as I can tell, so you'd have to rely on sending an email...

@bwiernik
Copy link
Contributor Author

bwiernik commented Jul 5, 2021

My point with this issue is that the default behavior with ggsave when the file extension is wmf or emf is pretty unusable

@yutannihilation
Copy link
Member

I think we should fix any bugs that prevent people from using the graphics device of their choice, but otherwise I'd be wary of adding dependencies to relatively obscure external packages.

I agree with you. I basically believe it should be fixed on grDevices's side if it's grDevices's problem.

@thomasp85
Copy link
Member

thomasp85 commented Oct 28, 2021

This is really an issue that should be fixed in grDevices... however I'm pretty sure it never will be unless @pmur002 has a very strong urge to dive into that arcane code base :-)

I am very much against adding a dependency to a compiled packages that is not developed in public by a developer we have no knowledge of - even as a weak dependency. So any fix will need to find a third route

@thomasp85
Copy link
Member

I think the best fix would be to ask Microsoft to finally add proper support for SVG in Word 😄

@pmur002
Copy link
Contributor

pmur002 commented Nov 21, 2021

I have committed a change to r-devel (r81221) so that the user can specify xpinch and ypinch to win.metafile(). This will still require some trial-and-error to get the right result (e.g., 96 seemed to work for me on this example), but once you have found the right number it should continue to work (for that machine/display). Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants