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

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

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.


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

  1. K.O. Oloede

    This is really an eye opener and I like to be more updated because I am very interested in reproducible research with R. I am teaching R at undergraduate level and I shall be most pleased to introduce reproducible research to my students. Please update me more on how I can be more learned about this. Buying the book on reproducible research on Amazon from Nigeria has been difficult due to my address. Can you help on how to buy the books and other related ones? Yihui Xie’s e-mail is important to me. Glad if I can get it.

    1. Post author

      Sorry, I don’t think I can help with your requests but good luck in pursuing reproducible research!

  2. Gilad

    This is a GREAT post – helped me a lot!! thanks
    unfortunately i’m getting an error while trying to create the PDF –
    running command ‘pandoc -s Category-Price-Analysis.html -o Category-Price-Analysis.pdf’ had status 127
    i’d love to know how to fix it

    thanks in advance!

  3. Gilad

    when producing the PDF all the font and headlines are default and not as i defined in the rmd file – do you how it should be done?


Leave a Reply

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