Monday, 13 October 2014

Publishing HTML or PDF from R - 'knitr' and 'rmarkdown'

This shows how html and pdf documents could be produced from R console, outside of RStudio. While RStudio offers simpler and more convenient interface to produce these documents, using R console offer options to schedule these documents to be generated through batch processing which would come in handy for daily reports etc.



With 'knitr' package, you can publish html documents out of rmd files.

library(knitr)


knit2html("C:/your/file//location/Input.Rmd","C:/your/file//location/Output.html")



With 'rmarkdown' package, you can convert rmd to pdf.

At the time of writing this blog, in Windows 8 or higher, pandoc installation program does not register its path to exe file automatically, hence you need to set the path manually. One option is to use Sys.setenv() function within R to point to where pandoc application is.

library(rmarkdown)


Sys.setenv(PATH=paste(Sys.getenv("PATH"),"C:\\your\\file\\location\\Pandoc",sep=";"))

render("C:/your/file//location/Input.Rmd","pdf_document",
output_file="C:/your/file//location/Output.pdf",
output_dir=getwd())





No comments:

Post a Comment