2016-09-28 34 views
0

我工作的一个功能到一个文本文件转换为使用R.使用rmarkdown

我使用rmarkdown PDF中的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 

我在做什么错?

有人可以帮忙吗?

问候。

回答

1

您需要检查文件的第144行。 \提供转义序列来控制各种东西,如标签\t或换行\nhttps://en.wikipedia.org/wiki/Control_sequence。 您可以将您的转义反斜杠转换为文字转义字符\\或者您可以将其替换为正斜线/

+0

但是,当我不使用它作为功能时,它完美地工作。 –

+0

我做了一些基本的文本处理,以删除所有的'\',它的工作。感谢指针。 –