且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

怎么把R Markdown转换成PDF?

更新时间:2023-02-19 14:03:44

更新后的答案(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.