rmarkdown vector graphics for knit word
Asked Answered
H

1

10

When using rmarkdown with knitr in Rstudio to knit a Microsoft Word document, the graphics generally look crappy because the usual vector graphics formats, such as PDF are not supported in Microsoft Word.

Fortunately, the devEMF package in R generates the microsoft vector format EMF, and I almost have this working for my rmarkdown application. The problem is that my image is showing up way too small. How can I control the image size?

I've tried the standard things below:

---
title: "Plot size trouble demo"
author: "for stackoverflow"
output: word_document
---

Here I include graphics in a word document using the `emf` printing device.

```{r dev = 'emf', fig.ext = 'emf'}
require(devEMF)
plot(1:10, 1:10)

```

The plot is small, so I try to make it bigger with the `fig.height` and fig.width` options:

```{r dev='emf', fig.ext='emf', fig.height=10, fig.width=10}
require(devEMF)
plot(1:10, 1:10)

```

The plot region stayed the same size, and now I have super-small labels and points!

Finally, I try to use the `out.width` and `out.height` options to rescale the figure to a different size, but this produces nothing at all:

```{r dev='emf', fig.ext='emf', fig.height=10, fig.width=10, out.width = '5in', out.height= '5in'}
require(devEMF)
plot(1:10, 1:10)

```

Update: I noticed here that rmarkdown supports win.metafile, but the following also produces nothing:

```{r dev = 'win.metafile', out.width = '5in', out.height= '5in'}
plot(1:10, 1:10)
```
Haney answered 30/7, 2014 at 15:30 Comment(3)
(Stylistic note: not literally every word remotely related to code needs a code tag. "PDF" and "EMF" are file extensions; by "Word" you presumably mean Microsoft Word.)Crystallo
I guess you should not use out.width/out.height. Sorry I do not have time to explain the technical details here.Iyar
Have you tried using a template file, as is suggested here?Phipps
B
0

Currently (Aug. 2024), word deals well with PDFs.

You can use:

```{r}
knitr::include_graphics("demo.pdf")
```
Bufordbug answered 11/8 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.