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

时间:2016-09-28 13:47:13

标签: r pdf r-markdown pandoc

我正在开发一个使用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.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 

我做错了什么?

有人可以帮忙吗?

问候。

1 个答案:

答案 0 :(得分:1)

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

相关问题