Create HTML or PDF Files with R, Knitr, MiKTeX, and Pandoc

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

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:

R PDF Output Example_Page_1

R PDF Output Example_Page_2

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.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

3 thoughts on “Create HTML or PDF Files with R, Knitr, MiKTeX, and Pandoc

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>