【问题标题】:Using knit2pdf with Rmd files将 knit2pdf 与 Rmd 文件一起使用
【发布时间】:2015-02-19 10:45:30
【问题描述】:

是否可以直接将 knitr 函数 knit2pdf() 与 R Markdown (Rmd) 文件一起使用?我看过各种教程/课堂笔记似乎表明它可以,例如herehere (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 

非常感谢您的帮助。

【问题讨论】:

  • 检查 ?knit2pdf,我看到 knit2pdf() 需要一个 Rnw 或 Rrst 文档,这就是为什么“带有 Rnw 文件的 knit2pdf() 可以按预期工作”而不是 Rmd 的原因。我认为“单击“Knit PDF”按钮总是成功生成 pdf”,因为在初步步骤中生成了 Rnw 或 rst 版本,然后 knit2pdf() 调用结果以生成 pdf(或类似的东西)。所以你需要弄清楚如何从你的 Rmd 生成第一个版本,然后使用 rst2pdf()

标签: r knitr r-markdown


【解决方案1】:

您的输入文件是rmarkdown 格式。

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

试试:

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

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 2010-09-19
    • 2017-10-04
    • 2013-02-19
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多