3 Project Management with R

3.1 TL;DR

  • Use R Projects (.Rproj files) to keep track of your work
  • Combine your code directly with your results to create great reports using RMarkdown documents
  • Use the here() command to refer to your files and folders in a project
  • Before you start working on a longer and more intensive project, use GitHub to keep track of all your changes.

3.2 Working with Projects

As discussed in our Project Management chapter 3, we click on “Create a Project” and e.g. name it “QEH QM.” This will put an .RProj file into the folder we selected. This is the basis for all our exercises and Problem Sets going forward.

To open the project, we either double click on it in our file browser, or we open it from RStudio.

Creating a new R Project - once created, the working directory (above console) and the project (top right) is perfectly configured.

Figure 3.1: Creating a new R Project - once created, the working directory (above console) and the project (top right) is perfectly configured.

3.3 RMarkdown Documents

One of the greatest assets of R, is that you can use RMarkdown to marry your coding and reporting - no more need to write up your results e.g. in a Word Document while having your code in a separate script.

This book, for example, is also written with RMarkdown. We can create a RMarkdown document very easily, as you can see in Figure 3.2 below.

You can create a new document as an R Notebook. In this case, we modfiy the header to change the title and include an author. We also change from "html_notebook" to "pdf_document" because I want to create a PDF document. Once we have saved the file, we can click on "knit" and our document will be created.

Figure 3.2: You can create a new document as an R Notebook. In this case, we modfiy the header to change the title and include an author. We also change from “html_notebook” to “pdf_document” because I want to create a PDF document. Once we have saved the file, we can click on “knit” and our document will be created.

See the video below for a further introduction.

RStudio comes ready to use RMarkdown files and

What is R Markdown? from RStudio, Inc. on Vimeo.

A great resource on RMarkdown documents can be found here.

3.4 The here() function

Use the here() function to navigate within your project. The here() function finds the path to the top-level of my current project to find a file, so it does not matter whether I started out in a sub-folder or not.

The way that the here() command works, is that you first load the package and then use the here as a wrapper for each time you are importing a datafile:

library(here)
here("data", "subfolder", "file_i_want.csv")

See this link for more details.

Basically, using the here() command will save you a huge amount of hassle as you are going forward.

The here command in an alternative interpretation.

Figure 3.3: The here command in an alternative interpretation.

3.5 Further tips:

If you start with your research project, I highly recommend to start to use GitHub as your Version control. See this great Book on working with GitHub in RStudio.