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 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 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

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.

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:


You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
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

# Create .md, .html, and .pdf files
markdownToHTML('', '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 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: and here
There’s a lot more on Pandoc here:
There’s a lot more on Markdown here:
There’s a lot more on MikTex here:
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.


Convert Markdown to other formats via 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.