更新时间:2023-02-19 14:03:44
rmarkdown程序包:
现在在github 上有一个 rmarkdown
程序包可以与Pandoc交互.
它包括一个render
函数.该文档非常清楚地说明了如何将rmarkdown转换为其他格式.这包括在rmarkdown文件中包括输出格式,或者正在为rend函数提供输出格式.例如,
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')"
因此,许多人建议使用Pandoc.请参阅以下注释,了解拥有最新版本的Pandoc的重要性.
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
markdownToHTML
函数中的选项意味着图像引用是针对文件的,而不是针对HTML文件中存储的数据的(即,我从选项列表中删除了'base64_images'
).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).正如@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.