knitr:如何根据输出类型设置包装选项 [英] knitr: how to set package options depending on output type
问题描述
我开始使用包knitr
作为工作流中的组件,以从Markdown
输入文件生成HTML
和PDF
报告.
我想设置一些专门针对输出文件格式的knitr
软件包选项.
目前,我手动在以下两行之间来回切换:
I am starting to use package knitr
as a component in the workflow to produce HTML
and PDF
reports from a Markdown
input file.
I would like to set some knitr
package options specifically tailored to the format of the output file.
Currently I manually switch back and forth the following two lines:
<!--roptions dev='png', fig.width=300px, fig.height=200px" -->
<!--roptions dev='pdf', fig.width=5, fig.height=4 -->
是否可以根据输出类型自动让knitr知道要使用的选项集?
Is it possible to let knitr know which set of options to use based on output type, automatically?
谢谢.
推荐答案
@Ramnath评论建议使用解决方案通过将Makefile
中的knitr
设置为特定选项,从唯一的Markdown
文件生成pdf
和html
输出:
@Ramnath comment suggests a solution to producing pdf
and html
output from a unique Markdown
file by setting specific options to knitr
in the Makefile
:
$(PDF): $(SRC) Makefile
Rscript \
-e "library(knitr)" \
-e "opts_chunk[['set']](dev = 'pdf')" \
-e "pat_gfm()" \
-e "knit('$<', 'temp.md')"
$(PANDOC) temp.md -o $@
rm temp.md
此处图像的格式设置为pdf
.请注意,pat_gfm()
函数已添加到 GitHub 仅5天前,尚未发布为稳定版本.
Here the format of images is set to pdf
. Note that the pat_gfm()
function has been added in the master branch on GitHub just 5 days ago and has not been released as a stable version yet.
通过详细说明以完全回答问题,可以通过在Makefile中添加几行来轻松设置图像尺寸:
Elaborating a bit to fully answer the question, the image dimensions can be easily setted by adding a couple of lines to the Makefile:
-e "opts_chunk[['set']](fig.width = 5)"\
-e "opts_chunk[['set']](fig.height = 5)"\
这篇关于knitr:如何根据输出类型设置包装选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!