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

Requirements

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?

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

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

  • 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

  • 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

  • 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

  • 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

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.

Developer, data scientist and Philosopher. Co-founder of Guts & Glory, creator of Readery and 🤘Lemmy.

Developer, data scientist and Philosopher. Co-founder of Guts & Glory, creator of Readery and 🤘Lemmy.