knitr:如何根据输出类型设置包装选项 [英] knitr: how to set package options depending on output type

查看:60
本文介绍了knitr:如何根据输出类型设置包装选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用包knitr作为工作流中的组件,以从Markdown输入文件生成HTMLPDF报告.
我想设置一些专门针对输出文件格式的knitr软件包选项. 目前,我手动在以下两行之间来回切换:

I am starting to use package knitr as a component in the workflow to produce HTMLand 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文件生成pdfhtml输出:

@Ramnath comment suggests a solution to producing pdf and html output from a unique Markdown file by setting specific options to knitrin 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屋!

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