RMarkdown图以101%的宽度和高度插入到Word中 [英] RMarkdown plot inserted into Word with 101% width and height

查看:175
本文介绍了RMarkdown图以101%的宽度和高度插入到Word中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像在标题中一样:无论我从RMarkdown插入Word中的图形多么精美或简单,我都得到的图像的高度和宽度为101%.这很烦人,因为这会使图变得模糊.

Just like in Title: no matter how fancy or simple a plot I insert into Word from RMarkdown, I get an image with 101% height and width. This is annoying because this makes plots look blurred.

示例

这个简短的.Rmd文件:

This short .Rmd file:

---
output:
  word_document

---

```{r, echo=F}
plot(1:100)
```

编织成这样:

然后我右键单击它,选择 Properties ,然后看到它插入时的原始大小为101%(下面的奇怪语言是波兰语;))

Then I right-click on it, select Properties and see it was inserted with 101% of original size (strange language below is polish ;) )

当我将其重置为100%时,它看起来好多了:

When I reset it to 100%, it looks much nicer:

问题 如何强制RMarkdown插入宽度和高度为100%的图?

Question How to force RMarkdown to insert plots with 100% width and height?

我尝试过的事情

(这些工作都没有)

  • 更改绘图大小(fig.widthfig.height块选项)
  • 将Word中的默认单位从cm更改为in
  • 用于绘图的更改设备及其重新布置(devres块选项)
  • change plot size (fig.width and fig.height chunk options)
  • change default unit in Word from cm to in
  • change device used for plots and it's resoultion (dev and res chunk options)

推荐答案

首先,请注意,当可以使用pandoc 链接属性解决此问题.如pandoc文档中所述,没有链接属性"后备是查看图像分辨率和图像文件中嵌入的dpi元数据"(我认为麻烦来自此后备方法)

This problem can be solved using pandoc link attributes. As explained in the pandoc documentation, without link attributes "the fallback is to look at the image resolution and the dpi metadata embedded in the image file" (I think the trouble comes from this fallback method).

链接属性是pandoc 1.16中引入的最新pandoc功能. knitr尚不支持此功能,但根据上述功能要求将很快提供该功能.

Link attributes is a recent pandoc feature, introduced in pandoc 1.16. It is not yet supported by knitr, but will be soon according to the above-mentioned feature request.

只要knitr不支持pandoc链接属性,这是一个解决该问题的技巧性建议.它使用knitr 绘图钩.

As long as knitr does not support pandoc link attributes, here is a hacky proposal to solve this problem. It uses a knitr plot hook.

---
output: word_document
---

```{r setup, echo=FALSE}
default_plot_hook <- knitr::knit_hooks$get('plot')

knitr::knit_hooks$set(plot = function(x, options) {
  default_md <- default_plot_hook(x, options)
  link_attr <- sprintf("{width=%sin height=%sin}", options$fig.width, options$fig.height)
  sub("(!\\[.*]\\(.*\\))", paste0("\\1", link_attr), default_md)
})
```

```{r, echo=F}
plot(1:100)
```

说明
RMarkdown文件(.Rmd)渲染过程的第一步是生成普通的markdown文件(.md). 这是knitr的工作.

Explanations
The first step of the rendering process of a RMarkdown file (.Rmd) is to produce a plain markdown file (.md). This is the job of knitr.

该过程的第二步是从该markdown文件生成word文件(.docx). 这是pandoc的工作.

The second step of the process is to produce the word file (.docx) from this markdown file. This is the job of pandoc.

请注意,您可以使用YAML标头中的keep_md选项检查此中间markdown文件:

Note that you can inspect this intermediate markdown file using the keep_md option in the YAML header:

---
output: 
  word_document:
    keep_md: true
---

在可用的knitr版本中,图像的链接由knitr呈现,如下所示:

With the available versions of knitr, a link to an image is rendered by knitr like this:

![](myreport_files/figure-docx/unnamed-chunk-1-1.png)<!-- -->

没有链接属性,因此pandoc使用后备方法确定图像的尺寸.

There is no link attributes, so pandoc determines the dimensions of the image using its fallback method.

建议的knitr图钩将链接的width和height属性添加到链接. 它返回以下markdown:

The proposed knitr plot hook add width and height attributes to the link.
It returns the following markdown:

![](myreport_files/figure-docx/unnamed-chunk-1-1.png){width=5in height=4in}<!-- -->

因此,pandoc从这些属性而不是回退方法确定图像的尺寸.

Therefore, pandoc determines the dimensions of the image from these attributes and not from the fallback method.

很明显,要使用此建议,您的pandoc版本必须为> = 1.16.

Obviously, to use this proposal, your pandoc version has to be >= 1.16.

这篇关于RMarkdown图以101%的宽度和高度插入到Word中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆