R.devices icon indicating copy to clipboard operation
R.devices copied to clipboard

EMF support for WORD

Open jsibley2 opened this issue 6 years ago • 1 comments

I have the following code:

library(officer)
library(devEMF)
library(R.devices)
mydoc <- read_docx()
toEMF("example", aspectRatio=0.88, print(plot(1:10)))
mydoc %>%
  body_add_img(src = "figures/example.emf", width = 7, height = 8) %>%  
  print(target = "Output/emf_test.docx")

It gives me this output:

image

Why is the chart so much smaller than the image and what can I do to make it bigger within the frame? I have tried different values of scale and it doesn't seem to help (or I haven't found the right value)?

Also, toEMF exists, but devEval doesn't seem to accept "emf" as an option. Is this by design?

Thanks for any help with this.

jsibley2 avatar Nov 28 '18 16:11 jsibley2

Why is the chart so much smaller than the image and what can I do to make it bigger within the frame? ...

I just tried the following in R 3.5.1 on Windows 8:

> file <- R.devices::toEMF("example", aspectRatio=0.88, print(plot(1:10)))

and when I open the generated 'figures/example.emf' file (in MS Windows Paint), it shows proper margins, i.e. there are no wide margins around the figure.

The toEMF() function calls devEval(type = "win.metafile", ...) which in turn calls grDevices::win.metafile(...) to produce the EMF file. In your/above example code, it ends up using win.metafile("figures/example.emf", width = 7, height = 6.16). You can try playing around with win.metafile() and dev.off() directly to see if you can achieve what you need.

Could it be that there is an issue with the 'officer' packages?

Also, there should be no need to do library(devEMF) - that package is not used by R.devices.

Also, toEMF exists, but devEval doesn't seem to accept "emf" as an option. Is this by design?

Yes, by design. devEval(type, ...) supports all type:s where there exists corresponding device function with the same name as type (there are some exceptions and bells'n'whistles, but lets ignore that here). For instance, devEval("png", ...) looks for a png() function, devEval("eps", ...) for eps(), ...

Now, there is now emf() device function. There is a win.metafile() to create EMF files. I could have named the corresponding toNNN() function toWinMetafile() but I went with the shorter toWMF(). Now, win.metafile() (see its help) does actually produce 'Windows Enhanced Metafile (EMF)' files and not just the simpler 'Windows Metafile (WMF)' format. Because of that, I added toEMF() so that the filename extension will be *.emf to better reflect the format. Note that toWMF() and toEMF() are identical except from the filename extensions they use. So, one could argue that there should be no toWMF() and only a toEMF() - but I kept both because some people/software want the *.wmf extension.

HenrikBengtsson avatar Mar 29 '19 18:03 HenrikBengtsson