【问题标题】:Converting text to pdf in R using rmarkdown使用 rmarkdown 在 R 中将文本转换为 pdf
【发布时间】:2017-02-06 13:07:09
【问题描述】:

我正在研究使用 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 

我做错了什么?

有人可以帮忙吗?

问候。

【问题讨论】:

    标签: r pdf r-markdown pandoc


    【解决方案1】:

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

    【讨论】:

    • 但是当我不把它作为一个函数使用时,它可以完美运行。
    • 我做了一些基本的文本处理来删除所有的`\`并且它起作用了。谢谢指点。
    猜你喜欢
    • 2021-06-11
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2011-01-04
    相关资源
    最近更新 更多