Create PDF reports using R, R Markdown, LaTeX and knitr (on Windows 10)

TLDR: This tutorial teaches you how to install LaTeX, R and R Markdown on Windows 10. It also guides you through creating your first R Markdown file and shows how to compile it into a PDF file using knitr.

Update - March 2018: If you’re on a Mac, see my post on how to create PDF reports on Mac OS High Sierra.

Introduction

If you’re using R to statistically explore data sets, and you need to write reports detailing your findings, you can benefit from using R Markdown. An R Markdown file is basically a standard Markdown file with embedded R code chunks. This tutorial teaches you how to install everything you need on a Windows 10 computer as well as how to create R Markdown files and compile them into PDF files. It doesn’t teach you the syntax of R Markdown. Howevever, if you are familiar with R and Markdown, that should not be a problem once you have seen an example R Markdown file (which you will as part of his tutorial). If you are familiar with R and not with Markdown, follow me here on Medium or on Twitter to get notified when I post a tutorial on that. (If you’re not familar with R, I’m not quite sure why you’re reading, but of course, you’re very welcome to read along!)

Requirements

This tutorial assumes you are running Windows 10. If you are running an older version of Windows you should still be able to follow along, provided you are running at least Windows Vista.

When writing this tutorial I used a new and 100% clean installation of Windows 10 — no software was installed in advance and no changes had been made to the settings of the computer (except changing the color of the desktop). In other words, everything you need is explained in this tutorial.

I ran this tutorial as the default user that was created during installation of Windows 10. This user is an administrator. If you are running as a standard user, you may have varying degrees of success.

Having installed R and RStudio before installing MikTeX?

This guide assumes you have neither R, RStudio nor MikTeX installed before beginning the tutorial. In case you are already experienced with R but are not using LaTeX, chances are you already have R and RStudio installed but do not have MikTeX. If that’s the case for you, don’t fret. You can just skip the parts explaining how to install R and RStudio. I have tested that this works exactly the same by first installing R and RStudio and then finally installing MikTeX on a clean installation of Windows.

1. Determine whether you’re running 32-bit or 64-bit Windows

Before installing anything, you should determine the architecture of your system. That is, whether you’re running a 32-bit or 64-bit Windows installation. If you’re already sure which one you’re running, you can skip this step.

If you’re comfortable with the Command Prompt, the easiest way to determine the Windows architecture is by opening a command prompt (cmd.exe) and copy-paste the following (and hit return):

echo %PROCESSOR_ARCHITECTURE%

If you’re running a 64-bit Windows, it will print something like AMD64. If it prints x86, it means you’re running 32-bit Windows (no, it’s not 86-bit :)).

Alternatively, you can open the Control Panel, go to System and Security and then System. This will tell you whether you’re on a 32-bit or 64-bit system. Note that what matters is the operating system, not the actual processor. See screenshot below.

2. Download and Install MikTeX

You are now ready to download and install MikTeX (pronounced mick-tech). MikTeX is a so-called LaTeX distribution (pronounced lah-tech or lay-tech). It is required for knitr to create professionally (or at least, academically) looking reports. If you haven’t heard of LaTeX before (or tried it but failed to make it work for you), don’t fret. You can write your report using RMarkdown and knitr will automatically convert your report to a LaTeX file which it will use to create a PDF file for you. You will never have to look at the LaTeX file itself.

  • Go to http://miktex.org/download in your favorite web browser.
  • Pick a download matching the system architecture you determined in the previous step (32-bit or 64-bit). You will notice that for each architecture, there are several kinds of downloads, for example a net installer, a portable installer and more. I strongly recommend you pick the one just called installer. In any case you should pick either that one (installer) or the one called net installer. The net installer lets you install all LaTeX packages immediately while installer only installs the most common ones and gives you the opportunity of automatically download need packages on-the-fly. However, the process of using the net installer is quite a bit more complicated so unless you know what you are doing, use the one just called installer.
    If you follow my advice and use installer, a handful of needed packages will not be downloaded immediately. But no reason to worry — RStudio and the MikTeX package manager will make sure packages are downloaded when needed. For this tutorial, I used the installer. If you pick the net installer, you are on the own for this part of the ride. No matter which type of installer you pick, make sure it’s one matching your system architecture (32-bit or 64-bit). I’m on a 64-bit Windows so I picked the one called Basic MikTex 2.9.5721 64-bit Installer. The version number will most likely have changed when you read this, don’t worry about that.
  • When your download is complete, run the downloaded installer. Windows may ask you if you want to “allow this app from an unknown publisher to make changes to your PC”. If it does, make sure to click Yes.
  • The installation procedure is quite straightforward. You are given a few options, for example you can choose the installation location. I recommend you stick with the default settings, so after clicking Next on the first screen, and checking the box indicating you accept the “MikTeX copying conditions” on the second screen, you can basically just continue clicking Next on each screen. On the final screen of the wizard, click Start, and go make a cup of coffee while the installation completes.
  • When the program is done installing, click Next one final time and then Close to complete the installation.

3. Download and Install R

Before installing RStudio we will need to download and install R itself.

  • Go to https://cran.rstudio.com/ in your web browser.
  • Click the Download R for Windows link.
  • Click the install R for the first time link, or alternatively the base link (they point to the same page).
  • Then click the the Download R 3.2.2 for Windows link to download R. By the time you read this, the version number will have most likely changed. Don’t worry about that.
  • When the download is complete, run the file you just downloaded. Again, you may be asked whether you want to “allow this app from an unknown publisher to make changes to your PC”. Make sure to click Yes.
  • Before installation of R begins, you have to choose the language to use during installation. I suggest you choose English.
  • As when installing MikTeX, you are a given a few options to choose from during installation of R, but again I recommend you stick to the defaults so you can just click Next at each screen. This will install both a 32-bit and a 64-bit version of R. If you want to save space you can choose to only install the version which is relevant for your system (as determined in the first step) but I wouldn’t worry about it.
  • When installation is done, click Finish to close the installer.

4. Download and Install RStudio

Now is the time to install RStudio.

  • Go to https://www.rstudio.com/products/rstudio/download/ in your web browser.
  • From this page, you can download various installers for RStudio. You have to choose the one for Windows. At the time of this writing, it’s called RStudio 0.99.486 — Windows Vista/7/8/10, but as usual, the version number will likely have changed when you read this. You don’t have to choose between a 32-bit and 64-bit version.
  • When the download is complete, run the file you just downloaded. As usual, Windows may ask if want to “allow this app from an unknown publisher to make changes to your PC”. And as usual, make sure to click Yes.
  • When installation is complete, click Finish to close the installer.

You are now ready to open RStudio! You can find it the Start/Windows menu. Note that R and RStudio are different programs, so make sure you open RStudio and not R. (It should look like the screenshot below).

5. Preparing for Your First R Markdown Document

You are now almost ready to create your first R Markdown document. Before you can actually work with R Markdown, you will need to install a few packages, one of which is knitr itself. Luckily, RStudio will automatically detect which packages are need if you just create new R Markdown file. All you need is to follow the steps below.

  • Click File in the menu, then New File, and then R Markdown…
  • RStudio will now detect that you miss some packages and ask if you want to install them now. Click Yes. RStudio will now install the required packages.

6. Your First R Markdown Document

When RStudio is done installing packages it will show the dialog window for creating a new R Markdown file. In this dialog, you can enter the title of the document, as well as the name of the author (you, I would suppose) and which type of output document you want(HTML, PDF or Word). You can change any of these later but you might as well set it up correctly now. The main goal of this post ist to show you how you can create PDF files (using R Markdown, knitr and LaTeX) so you should select PDF, and you might as well enter your name and a title for the document. You can choose other templates in the left sidebar, for example Presentation or Shiny but we are not going to use them for this tutorial. Click OK and RStudio will generate and open your first R Markdown file as shown below.

Before actually processing the R Markdown to generate a PDF, you should make sure to save the R Markdown file. When you do this for the first time, RStudio will ask what encoding you want to use when saving the file. The file encoding determines how the characters which make up your R Markdown file are stored on disk. The topic of file encodings is way too involved for the scope of this article so I will not go into any sorts of details but I should say that I absolutely recommend that you choose UTF-8 as seen below. (Feel free to check the “Set as default encoding for source files” if you want. That way you will not have to choose encoding each time you create a new R Markdown file.)

Having saved your R Markdown file, it’s time to process it and generate a PDF file. You can do this by clicking the Knit PDF button above the text. When you do this for the first time, RStudio will find that some LaTeX packages are missing. Luckily, it can install them for you. Just click the Install button shown in the screenshot below. (And once again you may be asked if you will “allow this app from an unknown publisher to make changes to your PC” and as always click Yes.) Don’t fret if an almost identical dialog comes up again asking to download a missing LaTeX package just when the first one completes installing. In my case, 7 packages were missing, so I had to click Install several times. Fortunately, you only have to go through this entire process once. Optionally you can remove the checkmark from “Always show this dialog before installing packages” to have RStudio download missing packages automatically without asking first.

When installation of all missing LaTeX packages is complete, your PDF file will be generated and opened as shown below. You’re now ready to start writing and processing your own R Markdown files and generate awesome PDF-files using knitr and LaTeX!

For more tutorials, feel free to follow me here on Medium or on Twitter.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.