首页 > 解决方案 > R:将 Rmarkdown 转换为 PDF

问题描述

我正在使用 R 编程语言。我正在学习如何使用 Rstudio 将“rmarkdown”转换为 pdf。我在这里关注本教程:https ://rmarkdown.rstudio.com/articles_intro.html

我首先创建了一个新的 Rmarkdown 文件:

在此处输入图像描述

然后,我在 Rmarkdown 编辑器中输入了以下文本:

---
output: html_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 .

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.

在此处输入图像描述

然后,我点击“编织成 PDF”:

在此处输入图像描述

但这不起作用并返回以下消息:

processing file: testfile.Rmd
  |..........                                                            |  14%
  ordinary text without R code

  |....................                                                  |  29%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |..............................                                        |  43%
  ordinary text without R code

  |........................................                              |  57%
label: cars
  |..................................................                    |  71%
  ordinary text without R code

  |............................................................          |  86%
label: pressure (with options) 
List of 1
 $ echo: logi FALSE

  |......................................................................| 100%
  ordinary text without R code


output file: testfile.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS testfile.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output testfile.tex --lua-filter "C:\Users\me\OneDrive\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\me\OneDrive\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 
Error: LaTeX failed to compile testfile.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
In addition: Warning messages:
1: In has_crop_tools() : 
Tool(s) not installed or not in PATH: pdfcrop, ghostcript
-> As a result, figure cropping will be disabled.
2: In system2(..., stdout = if (use_file_stdout()) f1 else FALSE, stderr = f2) :
  '"pdflatex"' not found
Execution halted

No LaTeX installation detected (LaTeX is required to create PDF output). You should install a LaTeX distribution for your platform: https://www.latex-project.org/get/

  If you are not sure, you may install TinyTeX in R: tinytex::install_tinytex()

  Otherwise consider MiKTeX on Windows - http://miktex.org

  MacTeX on macOS - https://tug.org/mactex/
  (NOTE: Download with Safari rather than Chrome _strongly_ recommended)

  Linux: Use system package manager

有人可以告诉我我做错了什么吗?是否有将 Rmarkdown 转换为 PDF 的简单方法?

谢谢

注意:会话信息

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.0.3    htmltools_0.5.1.1 tools_4.0.3       yaml_2.2.1       
 [5] tinytex_0.30      rmarkdown_2.7     knitr_1.31        digest_0.6.27    
 [9] xfun_0.21         rlang_0.4.10      evaluate_0.14  

标签: rpdfr-markdown

解决方案


推荐阅读