使用 rmarkdown 在 R 中将文本转换为 pdf

Converting text to pdf in R using rmarkdown

我正在研究使用 R 将文本文件转换为 pdf 的功能。

我正在使用 rmarkdown。

SO上已经提供了脚本来实现,如下:

require(rmarkdown)
my_text <- readLines("YOUR PATH") 
cat(my_text, sep="  \n", file = "my_text.Rmd")
render("my_text.Rmd", pdf_document())
file.remove("my_text.Rmd") #cleanup

这很完美

我想把它写成一个函数。

我的函数如下:

convertTOtext<-function(.txt){
  require(rmarkdown)
  my_text <- readLines(.txt) 
  cat(my_text, sep="  \n", file = "Report.Rmd")
  render("Report.Rmd", pdf_document())
  #file.remove("my_text.Rmd") #cleanup
}

但是,当我 运行 将函数作为 convertTOtext(test.txt) [我与文本文件位于同一目录] 时,出现以下错误:

processing file: Report.Rmd
output file: Report.knit.md

! Undefined control sequence.
l.144 ``C:\Users

pandoc.exe: Error producing PDF
 Show Traceback

 Rerun with Debug
 Error: pandoc document conversion failed with error 43 In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS Report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output Report.pdf --template "C:\Users\user\Documents\R\win-library.3\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 

我做错了什么?

有人可以帮忙吗?

此致。

您需要检查文件的第 144 行。 \ 提供转义序列来控制各种事物,例如制表符 \t 或换行符 \n https://en.wikipedia.org/wiki/Control_sequence。 您可以将转义反斜杠转换为转义反斜杠 \ 的文字,也可以将其替换为正斜杠 /