2016-09-28 96 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

我做了一些基本的文本處理,以刪除所有的'\',它的工作。感謝指針。 –