rpdfr-markdownpandoc

Converting text to pdf in R using rmarkdown


I am working on a function to convert a text file to pdf using R.

I am using rmarkdown.

There is already a script provided on SO to do it, which is as follows: Text to Pdf in R

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

This works perfectly.

I am trying to write it as a function.

My function is as follows:

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
}

However, when I run the function as convertTOtext(test.txt) [I am in the same directory as the text file], I get the following error:

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 

What am I doing wrong?

Can someone help?

Regards.


Solution

  • You need to check line 144 of your file. \ provides escape sequence to control various things suchs as tabs \t or newlines \n https://en.wikipedia.org/wiki/Control_sequence. You can either convert your escape backslash to a literal one escaping it \\ or you can replace it with forwardslash /