Create PDF reports using R, R Markdown, LaTeX and knitr (on macOS 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 Mac running macOS High Sierra, 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 macOS High Sierra. If you are running an older version of macOS you should still be able to follow along, provided you are running at least OS X El Capitan.

Having installed R and RStudio before installing MacTeX?

This guide assumes you have neither R, RStudio nor MacTeX 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 MacTeX. 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 MacTeX on a clean installation of macOS.

1. Download and Install MacTeX

You should start by installing MacTeX (pronounced mac-tech). MacTeX 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.

  • When your download is complete, run the downloaded installer.
  • The installation procedure is quite straightforward. You are given a few options, for example you can choose not to install some comonents. I recommend you stick with the default settings, so having accepted the license agreement, you can basically just keep clicking Continue on each screen. On the final screen of the wizard, click Install. It may ask for an administrator password. Enter the password,click Install Software and go make a cup of coffee while the installation completes.
  • When the program is done installing, click Close to complete the installation.
  • If asked, it’s safe to choose to move the installer package to Trash.

2. 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 (Mac) OS X link.
  • Find the “Files:” section on the page. The left column contains links to installers. Clink the topmost link to download R. When I wrote this, it said R-3.4.3.pkg. 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.
  • Click Continue a couple of times and agree to the license.
  • As when installing MacTeX, 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 Continue on each screen.
  • Again, the installer may ask for an administrator password. Enter the password and click Install Software.
  • When installation is done, click Close to close the installer. If you’re asked whether you want to remove the installer to Trash, feel free to do so.

3. Download and Install RStudio

Now is the time to install RStudio.

  • Go to https://www.rstudio.com/products/rstudio/download/ in your web browser.
  • The upper part of this page contains a product feature comparison chart. Just scroll down to the bottom of the page. From here, you can download various installers for RStudio. You have to choose one for macOS. At the time of this writing, it’s called RStudio 1.1.442 — Mac OS X 10.6+ (64-bit), but as usual, the version number will likely have changed when you read this.
  • The RStudio installer comes in a .dmg file, a disk image. To install RStudio, double click the .dmg file to “mount” it. This should open a window showing the contents of the disk image. In this window, simply drag the blue RStudio icon to the Applications shortcut icon next to it. This will copy RStudio to your Applications folder. You should now “unmount” the disk image.

4. 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 needed 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.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Søren L Kristiansen

Søren L Kristiansen

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