knitr .Rmd-> Word文档:数字控件的详细信息 [英] knitr .Rmd -> Word document: control details of figures

查看:78
本文介绍了knitr .Rmd-> Word文档:数字控件的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有以下YAML标头的.Rmd文件为一本书编写解决方案手册:

I'm producing a solutions manual for a book, using .Rmd files with the following YAML header:

---
title: "DDAR: Solutions and Hints for Exercises"
date: "`r Sys.Date()`"
output: 
  word_document:
    reference_docx: solutions-setup.docx
---

我用reference_docx控制文档的总体布局,以获取输出Word文档.

where I control the general layout of the document with the reference_docx to get an output Word document.

会有很多数字,我想设置一些全局图形参数以提供相对紧凑的边框和合理的字体大小 在图中进行调整,而不必根据我在PDF文档中看到的内容进行调整.

There will be many figures, and I'd like to set some global graphics parameters to give relatively tight bounding boxes and reasonable font sizes in the figures without having to tweak each one from what I see in a PDF document.

我尝试了以下操作,但是par()设置似乎没有任何作用:

I tried the following, but the par() setting doesn't seem to have any effect:

{r setup, echo=FALSE} options(digits=4) par(mar=c(5,4,1,1)+.1)

{r setup, echo=FALSE} options(digits=4) par(mar=c(5,4,1,1)+.1)

相反,我在文档中得到的图像如下所示,其边框比我想要的要大,并且字体大小比我想要的要大得多.

Instead I get images like the following in my document with larger bounding boxes than I would like and with much larger font sizes than I would like.

我知道如何控制LaTeX生成的.Rnw文件中的所有内容,但是我 在.Rmd-> Word中找不到方法.有没有我可以的钩子 使用?我认为没有out.width块选项可以重新缩放 像LaTeX一样的数字.

I know how to control all this in .Rnw files produced with LaTeX, but I can't find how to do it in .Rmd -> Word. Is there a chunk hook I could use? I don't think that there is an out.width chunk option that re-scales a figure as in LaTeX.

推荐答案

@scoa的答案显示了如何使用钩子在每个块的开头设置一些图形参数.这是必要的,因为默认情况下,knitr会打开一个新的图形设备来记录曲线并在评估代码后将其关闭,因此par()设置将被丢弃",即无法在早期设置中设置用于后续块的图形参数-chunk,但需要分别为每个块设置.

@scoa's answer shows how to use a hook to set some graphical parameters at the beginning of each chunk. This is necessary because "by default, knitr opens a new graphics device to record plots and close it after evaluating the code, so par() settings will be discarded", i.e. graphical parameters for later chunks cannot be set in an early setup-chunk but need to be set for each chunk separately.

如果不需要此行为,则可以使用打包选项 global.par = TRUE:

If this behavior is not wanted, the package option global.par = TRUE can be used:

opts_knit$set(global.par = TRUE)

找到正确的边距值有时会很痛苦.在这些情况下, hook_pdfcrop 可以提供帮助.在所有使用选项crop = TRUE的块中,白色边距将被删除.要将其应用于所有块,请使用

Finding the correct values for the margins is sometimes quite painful. In these cases, hook_pdfcrop can help. In all chunks where the option crop = TRUE, white margins will be removed. To apply this to all chunks, use

library(knitr)
knit_hooks$set(crop = hook_pdfcrop)
opts_chunk$set(crop = TRUE)

这也适用于docx输出,因为当绘图格式不是PDF(例如PNG)时,ImageMagick中的convert程序将用于修剪白色边距"(来自?hook_pdfcrop).

This works for docx output as well because "when the plot format is not PDF (e.g. PNG), the program convert in ImageMagick is used to trim the white margins" (from ?hook_pdfcrop).

请注意,在某些情况下,裁剪地块会产生有时明显不同的地块缩放"因子的副作用:这种情况发生在以下情况下:当我们在两个地块上以相同大小的元素开始,但其中一个地块周围的白色边距较大时.如果然后在裁切后将两者都调整为固定的输出宽度,则图上具有较大边距的元素看起来会更大.但是,这与docx输出无关,因为在这种情况下不能使用out.width/out.height.

Note that under some circumstances, cropping plots has the side effect of sometimes apparently different "zoom" factors of plots: This happens in cases where we start with identical sized elements on two plots but larger white margins around one of the plots. If then both are resized to a fixed output width after cropping, elements on the plot with larger margins look larger. However, this is not relevant for docx output because out.width/out.height cannot be used in that case.

这篇关于knitr .Rmd-> Word文档:数字控件的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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