将 knit2pdf 与 Rmd 文件一起使用

Using knit2pdf with Rmd files

是否可以直接对 R Markdown (Rmd) 文件使用 knitr 函数 knit2pdf()?我看过各种 tutorials/class 似乎暗示它可以的注释,例如here and here(Ctrl+F "knit2pdf" 任意一个)。

但是当我获取一个简单的 rmd 文件时(另存为 "test.rmd")

---
title: "knit2pdf test"
author: "A Aaronson"
date: "Thursday, February 19, 2015"
output: pdf_document
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

并尝试

library(knitr)
knit2pdf("test.Rmd")

我收到以下错误

结果:

output file: test.md

Error: running 'texi2dvi' on 'test.md' failed

LaTeX errors:
! Emergency stop
*** (job aborted, no legal \end found)

!  ==> Fatal error occurred, no output PDF file produced!
!  ==> Fatal error occurred, no output PDF file produced!
In addition: Warning message:
running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf "test.md" --max-iterations=20 -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/bibtex/bst"' had status 1 

点击 "Knit PDF" 按钮总能成功生成 pdf。那么我是否缺少中间步骤?

我应该补充一点,带有 Rnw 文件的 knit2pdf() 对我来说按预期工作,但我仍然收到警告

running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf "rnwtest.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-31~1.2/share/texmf/bibtex/bst"' had status 1 

非常感谢帮助。

您的输入文件是 rmarkdown 格式。

您应该使用 rmarkdown 包中的 render() 函数来编译您的文档。

尝试:

library("rmarkdown")
render("temp.rmd")