Using knit2pdf with Rmd files

梦想的初衷 提交于 2020-01-03 08:18:29

问题


Is it possible to use the knitr function knit2pdf() directly with R Markdown (Rmd) files? I've seen various tutorials/class notes that seem to suggest it can e.g. here and here (Ctrl+F "knit2pdf" in either).

But when I take a simple rmd file (saved as "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.

and try

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

I get the following error

results in:

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 

Clicking the "Knit PDF" button always successfully generates a pdf. So am I missing an intermediate step?

I should add that knit2pdf() with Rnw files is working as expected for me, though I do still get the warning

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 

Help greatly appreciated.


回答1:


Your input file is in rmarkdown format.

You should use the render() function in the rmarkdown package to compile your document.

Try:

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



来源:https://stackoverflow.com/questions/28604304/using-knit2pdf-with-rmd-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!