Total Pageviews

Tuesday, 20 December 2016

How to create HTML or PDF files with R, Knitr, MiKTeX, and Pandoc?

This page will show you how to create HTML or PDF output from R. It’s complicated, but can be really useful!

Step 1: Install MiKTeX

Go to http://miktex.org/download and follow the instructions to install MiKTeX on your computer.
  • During the installation process be sure to change Preferred Paper from A4 to Letter if necessary.
  • You do not need to install this if you only want HTML output and no PDF output.

Step 2: Install Pandoc

Go to https://code.google.com/p/pandoc/downloads/list and follow the instructions to install Pandoc on your computer.
  • You do not need to install this if you only want HTML output and no PDF output.

Step 3: Install Knitr and Markdown

Open R or RStudio and install the packages Knitr and Markdown on your computer by running the following code:
# Install knitr
install.packages("knitr")
install.packages("markdown")

Step 4: Create a .Rmd File Containing Your Analysis

Open RStudio and click File then New then R Markdown. Then click File then Save As. Enter My_Analysis.Rmd as the name of the file and click Save. Note that RStudio will allow you to have spaces in the name but this will prevent later steps from working. Use an underscore instead.
When the new markdown (.Rmd) file is created it very helpfully is already populated with an example (the below code). Note the basic structure – the markdown file is both R code and Knitr code. Any code between the sets of three apostrophes is R code, any code outside of the sets of three apostrophes is Knitr. The R code tells R what to do and the Knitr code creates the HTML file.
Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plots(cars)
```
If you only want to create HTML files, not PDF files, stop right here and click Knit HTML in RStudio. This will create a HTML file based on the example code! The file will have the same name as the .Rmd file and will be save to the same location.
Your HTML file should look like this:
R HTML Output Example

Step 5: Create a .R File to Run the .Rmd File

If you want to create PDFs there is still more work to do. In RStudio click File then New then R Script to create a new .R file. This file will be used to tell MiKTeX and Pandoc to create a PDF based on your HTML file. Paste the following code into this R file. Then click File then Save As. Enter any name you want and click Save. I saved this file as Build_Report.R
  • Be sure to change the below code to specify the correct working directory and filenames.
  • Don’t use space in your file names or it won’t work.
# Set working directory
setwd("C:/Documents and Settings/name")

# Load packages
require(knitr)
require(markdown)

# Create .md, .html, and .pdf files
knit("My_Analysis.Rmd")
markdownToHTML('My_Analysis.md', 'My_Analysis.html', options=c("use_xhml"))
system("pandoc -s My_Analysis.html -o My_Analysis.pdf")

Step 6: Produce HTML and PDF Output Files with R

In RStudio, run all of the code in your Build_Report.R file. This will read the My_Analysis.Rmd file, use that to create a My_Analysis.md file, use the .md to create a My_Analysis.html file, and finally use the .html to create a My_Analysis.pdf file. Awesome!
Your PDF should look like this:


I’ll cover how to format HTML and PDF files on a separate page and link to it here once I do.
There’s a lot more on Knitr here: http://yihui.name/knitr/ and here http://cran.r-project.org/web/packages/knitr/index.html.
There’s a lot more on Pandoc here: http://johnmacfarlane.net/pandoc/.
There’s a lot more on Markdown here: http://daringfireball.net/projects/markdown/.
There’s a lot more on MikTex here: http://miktex.org/.
Here’s a great video introductory video from Yihui, the creator of Knitr. Note that this video only explains how to use R and Knitr to make HTML files and does not cover PDFs.

from http://rprogramming.net/create-html-or-pdf-files-with-r-knitr-miktex-and-pandoc/
----------

Convert Markdown to other formats via Pandoc

from http://yihui.name/knitr/demo/pandoc/
----------

R Markdown supports dozens of static and dynamic output formats including HTML, PDF, MS Word, Beamer, HTML5 slides, Tufte-style handouts, books, dashboards, shiny applications, scientific articles, websites, and more.

from http://rmarkdown.rstudio.com/
http://rmarkdown.rstudio.com/lesson-1.html