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

问题描述 投票:0回答:1

我正在开发一个使用 R 将文本文件转换为 pdf 的函数。

我正在使用 rmarkdown。

SO 上已经提供了一个脚本来执行此操作,如下所示:R 中的文本到 Pdf

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 

我做错了什么?

有人可以帮忙吗?

问候。

r pdf r-markdown pandoc
1个回答
1
投票

您需要检查文件的第 144 行。

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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.