Meeting Re-cap

To follow up our first meeting I would like for you to complete all the lessons in the R swirl tutorial before our next meeting. At the end of this doc I linked 2 readings for the next two weeks as well. I will also send these in an email.

Reminder: We established Tuesday and Thursday meetings after lunch. I will follow up with an email indicating the final times.

Introduction

In our first meeting we discussed what R and GitHub are, why they are useful, and how we will use them. These will be the tools we will use to complete analysis for our research.

The following tutorial is meant to give you a brief introduction to the R programming language. It is low stakes and interactive in your R environment.

To get you started on your R journey, walk through the following:

  • How to set-up a directory and GitHub repository at the same time - there are many ways to do this
  • How to set-up your R project file.
  • How to install and start your swirl tutorial.
  • How to commit file changes to save all of your work to GitHub.

Setting up a Repository

As previously mentioned there is no one right way to do this. You will find that one of the best parts about working in R is that there are many different ways to accomplish the same goal. This is the workflow I prefer but you should feel free to explore follow your own ways of working and organizing data and files if you discover a method that feels more intuitive to you.

Build repo using GitHub Desktop

To begin, open GitHub Desktop, navigate to the repository dropdown in the upper left corner of your GitHub Desktop session, and click Add:

Then click Create New Repository:

Give your new repository a title:

Provide a project description:

Select a local path to save your repository to. This is where your local copy of the repository will be saved:

Make note of where your local repository will be added, and click Create Repository:

Publish that repository to your online GitHub account:

If you log in to your GitHub account you should now see your new repository online.

Creating an R Project

An R Project is like a dedicated workspace for one project. Similar to how you keep different notebooks for different classes. It keeps your scripts, data, figures, results, etc. organized in one folder. When you open the R Project, RStudio knows where all your project files are, which makes it easier to load, save, and move between files without getting lost on your computer.

Steps for setting up your RProject

Creating the RProject will be fairly simple since we did the work of building a repository already.

First open an RStudio session and navigate to the Project drop down in the upper-right corner and select New Project…:

Select Existing Directory in the set-up wizard:

Note: You can also set-up the directory locally before GitHub, by creating the project first.

Find the existing directory. This is the same directory that we created for the GitHub repo. So the file path will be the same:

Finally, create project. If you would like for a new R window to open with the new project you can select Open in new session.

Once you have the project set up you can add folders and even other files to it if you need to.

  1. You can add files to the old fashion way, by opening the directory on you computer and adding a new folder.
  2. You can add a new folder from your Files panel at the right of your R window using the +Folder icon.
  3. Or you can write code to make R build a new directory for you:
dir.create("new_directory")

This will add a new folder to the directory you are in.

Some of the folders I recommend adding to any directory you create are:

  • scripts – where you will save your code scripts.
  • figures – any figures you create can be saved there.
  • data – sometimes you will pull in data or save data you create in R. You should have a place to save it.

Before you begin the tutorial, practice opening a new script. Add two comments at the top to put your name and date, and a title if you would like. As a reminder you can use the # to comment out your code. R will not recognize any text after # as code that needs to be run.

Example:

# Name: Joe Brown
# Date: 05/21/2026

Save that script in your scripts directory.

Installing and loading swirl

Swirl is one of the better R tutorials because it is interactive inside your R environment by providing prompts in the R console (the lower right panel).

Many packages that contain the important functions to run code in R are pre-installed. But most of them are not. For those that are not pre-installed, we have to install them manually.

We do this using the install.packages() command. This finds the package we are looking for inside a massive directory of R sponsored packages and installs the package for you.

You only ever need to install packages once. Therefore, it is good practice to run the command in the console:

install.packages("swirl")

You should notice a few syntax rules. First, the arguments for the function called install.packages is in () this will be the case for all functions in R. Second, the name of the package we want to install is in " " this will also ALWAYS be the case when we are installing packages, or using strings of letters that we want to be interpreted by R as text.

Once you have installed swirl in the console you can load the swirl library. This gives R a reference point. It gives R a space to look for the functions you are going to use. Or, in this case it drives the tutorial prompts.

We can load libraries like this:

# Load swirl package
library(swirl)

You should notice that we no longer NEED " " but we can use them if we want.

Once you run the library(swirl) command you will be able to choose the lesson to work through and R will use a prompt-response model as it tracks your work through the lesson.

Please start with the R Programming Basic Building Blocks.

Note: Any code that you are asked to write in swirl tutorial can be written in the console or in a script. Scripts can be saved and read more easily than console outputs. For now, just move through the tutorial. swirl will eventually prompt you to move from the console to a script. SO, just follow the lesson and we will fill in the gaps later.

I will not add instructions about committing changes to GitHub here. We will have practice doing this in the coming weeks.

Readings for the next couple of weeks:

  1. NASA article on the carbon cycle
  2. Hartin et al. 2015 - The Origin Story for the Hector simple climate model.