怎么把R Markdown转换成PDF? [英] How to convert R Markdown to PDF?
问题描述
我以前曾问过用于将R Markdown转换为HTML的命令.
将R Markdown文件转换为PDF文档的好方法是什么?
一个好的解决方案将保留尽可能多的内容(例如图像,方程式,html表等).该解决方案需要能够从命令行运行.一个好的解决方案也应该是跨平台,并且最好将依赖关系降到最低,从而更容易共享makefile等.
A good solution would preserve as much as possible of the content (e.g., images, equations, html tables, etc.). The solution needs to be able to be run from the command-line. A good solution would also be cross-platform, and ideally minimise dependencies to make it easier to share makefiles and so forth.
具体来说,有很多选择:
Specifically, there are a lot of options:
- 是否将RMD转换为MD转换为HTML转换为PDF;或从RMD到MD到PDF;或RMD转换为PDF
- 如果在R中使用
markdown
软件包,则应指定哪些选项 - 是否使用
pandoc
,R内置的软件包或其他内容
- Whether to convert RMD to MD to HTML to PDF; or RMD to MD to PDF; or RMD to PDF
- If using the
markdown
package in R, which options to specify - Whether to use
pandoc
, a package built into R, or something else
这是一个示例 rmd文件,该文件大概可以对任何建议的解决方案进行合理的测试.它用作此博客的基础发布.
Here's an example rmd file that presumably provides a reasonable test of any proposed solution. It was used as the basis for this blog post.
推荐答案
更新后的答案(2013年2月10日)
rmarkdown程序包:
现在在github 上有一个 rmarkdown
程序包可以与Pandoc交互.
它包括一个render
函数.该文档非常清楚地说明了如何将rmarkdown转换为其他格式.这包括在rmarkdown文件中包括输出格式,或者正在为rend函数提供输出格式.例如,
Updated Answer (10 Feb 2013)
rmarkdown package:
There is now an rmarkdown
package available on github that interfaces with Pandoc.
It includes a render
function. The documentation makes it pretty clear how to convert rmarkdown to pdf among a range of other formats. This includes including output formats in the rmarkdown file or running supplying an output format to the rend function. E.g.,
render("input.Rmd", "pdf_document")
命令行:
当我从命令行运行render
时(例如使用Makefile),有时会遇到找不到pandoc的问题.大概它不在搜索路径上.
以下答案说明了如何将pandoc添加到R环境中.
Command-line:
When I run render
from the command-line (e.g., using a makefile), I sometimes have issues with pandoc not being found. Presumably, it is not on the search path.
The following answer explains how to add pandoc to the R environment.
例如,在运行OSX的计算机上,我通过RStudio获得了pandoc的副本,我可以使用以下代码:
So for example, on my computer running OSX, where I have a copy of pandoc through RStudio, I can use the following:
Rscript -e "Sys.setenv(RSTUDIO_PANDOC='/Applications/RStudio.app/Contents/MacOS/pandoc');library(rmarkdown); library(utils); render('input.Rmd', 'pdf_document')"
旧答案(大约在2012年)
因此,许多人建议使用Pandoc.请参阅以下注释,了解拥有最新版本的Pandoc的重要性.
Old Answer (circa 2012)
So, a number of people have suggested that Pandoc is the way to go. See notes below about the importance of having an up-to-date version of Pandoc.
我使用以下命令将R Markdown转换为HTML(即此Makefile的变体),其中RMDFILE
是不带.rmd
组件的R Markdown文件的名称(它也假定扩展名是.rmd
而不是.Rmd
).
I used the following command to convert R Markdown to HTML (i.e., a variant of this makefile), where RMDFILE
is the name of the R Markdown file without the .rmd
component (it also assumes that the extension is .rmd
and not .Rmd
).
RMDFILE=example-r-markdown
Rscript -e "require(knitr); require(markdown); knit('$RMDFILE.rmd', '$RMDFILE.md'); markdownToHTML('$RMDFILE.md', '$RMDFILE.html', options=c('use_xhml'))"
然后使用此命令将其转换为pdf
and then this command to convert to pdf
Pandoc -s example-r-markdown.html -o example-r-markdown.pdf
关于此的一些注意事项:
A few notes about this:
- 我删除了示例文件中的引用,该示例文件将图导出到imgur中以托管图像.
- 我删除了对imgur上托管的图像的引用.数字似乎需要本地化.
-
markdownToHTML
函数中的选项意味着图像引用是针对文件的,而不是针对HTML文件中存储的数据的(即,我从选项列表中删除了'base64_images'
). - 结果输出看起来像此 .与从浏览器将HTML文件打印为pdf时所得到的相比,它显然制作了非常LaTeX风格的文档.
- I removed the reference in the example file which exports plots to imgur to host images.
- I removed a reference to an image that was hosted on imgur. Figures appear to need to be local.
- The options in the
markdownToHTML
function meant that image references are to files and not to data stored in the HTML file (i.e., I removed'base64_images'
from the option list). - The resulting output looked like this. It has clearly made a very LaTeX style document in contrast to what I get if I print the HTML file to pdf from a browser.
正如@daroczig所提到的,拥有最新版本的Pandoc以输出pdf至关重要.从2012年6月15日开始,在Ubuntu上,我在程序包管理器中停留在Pandoc的1.8.1版本中,但是从
As mentioned by @daroczig, it's important to have an up-to-date version of Pandoc in order to output pdfs. On Ubuntu as of 15th June 2012, I was stuck with version 1.8.1 of Pandoc in the package manager, but it seems from the change log that for pdf support you need at least version 1.9+ of Pandoc.
因此,我安装了caball-install
.
然后运行:
Thus, I installed caball-install
.
And then ran:
cabal update
cabal install pandoc
Pandoc已安装在~/.cabal/bin/pandoc
中
因此,当我运行pandoc
时,它仍然看到旧版本.
请参见此处添加到路径.
Pandoc was installed in ~/.cabal/bin/pandoc
Thus, when I ran pandoc
it was still seeing the old version.
See here for adding to the path.
这篇关于怎么把R Markdown转换成PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!