Lesson 0: Introduction and Set-up

Introduction

The pre-first lesson focuses on setting you up with RStudio and Git. As a reminder, there are some steps you should have done before starting this lesson:

  1. Install R. If you already have R installed, be sure it is the newest version.
  2. Install RStudio.
  3. Make sure tex (e.g. LaTeX) is installed.
  4. Set up Git on your local computer.
  5. Make a Bitbucket account.

By the end of this lesson you should have:

  • Git configured on your computer.
  • Git linked to RStudio.
  • An SSH RSA key linked to RStudio.
  • The SSH RSA key uploaded to Bitbucket.
  • Packages installed in RStudio.

The video below provides an overview of the course and explains the initial set-up steps. Feel free to pause the video as needed and read the more detailed instructions below. A PDF of the slides can be downloaded here.

Configuring Git

Unix-like Instructions

If you are on Unix-like machine (Mac or Linux) follow the instructions here, otherwise skip down to the section on how to configure Git for Windows. Open the Terminal and type

git --version 

as is displayed in the example image below.

terminal_git_version

You should see a version of Git available (e.g. here “git version 2.5.4 (Apple Git-61)”). If you do not see a version listed it means you do not have Git installed. There are several useful sources on the internet for how to install Git for Unix-like systems.

Assuming you see a version listed, we can configure your Git to include your information. To check first if you’ve already configured Git type

git config --list 

if nothing comes up it means nothing is configured. The only two things you really need to set are your name and email address. These can be set with the following Terminal commands, note replace YOUR NAME and YOUR EMAIL with the correct information:

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"
git config --list 

Now you should see your name and email as in the example image below.

terminal_git_config

Congratulations! Git is now installed and configured on your computer.

Windows Instructions

You should have downloaded Git and have it installed in a folder. Navigate to wherever you installed Git, it will probably be in the “Program Files” or somewhere similar. Open the executable “git-bash” and type

git --version

as is displayed in the example image below.

terminal_git_version_windows

Assuming you see a version listed, we can configure your Git to include your information. To check first if you’ve already configured Git type

git config --list

if your name is not in the “user.name”  line it means Git is not configured. The only two things you really need to set are your name and email address. These can be set with the following Terminal commands, note replace YOUR NAME and YOUR EMAIL with the correct information:

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"
git config --list

Now you should see your name and email as in the example image below (note, the email address here as been redacted but you should see one in your list).

terminal_git_config_windows.png

Congratulations! Git is now installed and configured on your computer.

Linking Git to RStudio

The next step is to be sure RStudio is recognizing that you have Git installed. Open RStudio and come to the screen in the image below with the following steps in the menu:

Unix-like: RStudio → Preferences → Git/SVN
Windows: Tools → Global Options… → Git/SVN

You should see roughly the following (this screen shot is from a Mac version of RStudio, yours may be slightly different).

rstudio_git

There two things you should double check here: 1) the box next to “Enable version control Interface for RStudio projects” should be checked, it may say something slightly different and be in a slightly different place if you’re not on a Mac, 2) make sure a path is present in the box for “Git executable”, for example mine is “/usr/bin/git”.

If the path is not set click the “Browse…” button and navigate to where you have Git installed on your computer. If you are a Windows user this will be almost the same folder as where you found the “git-bash” executable. It should be in the “bin” folder and called “git.exe”. If you are a Unix-like user it is probably in your user bin folder like mine above.

Great! You can now commit to Git right from within RStudio.

Getting an SSH RSA Key in RStudio

Also in the previous image you’ll notice that there is an area “SSH RSA Key”. This is how RStudio can communicate with online Git websites like Bitbucket and GitHub. If there is no file path set (unlike in the image above). Click “Create RSA Key…”. You will be given the option to create a passphrase for extra security, this is optional. You should now see a path similar to the one in the image above.

If using the “Create RSA Key…” button did not work for you you’ll need to create your RSA key directly in the Terminal. This is likely the case if are working on a Windows machine. If you are on a Unix-like machine go to the Terminal, if you are an Windows machine go back to the “git-bash” window that you have open from earlier when you configured Git. In the command line type:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 

You will be asked where to save the file, just press enter. You will then be prompted to create a passphrase, this is optional. It should look something like the image below (courtesy of https://help.github.com/articles/generating-a-new-ssh-key/).

terminal_ssh.png

Go back to RStudio. If you still have the “Options” menu open close it by clicking “OK”. If you navigate back to the “Git/SVN” tab in the “Options” menu you should now see a path for your SSH RSA key. Now we need to actually use this key to talk to Bitbucket!

Getting Your SSH RSA Key on Bitbucket

The (almost) final step for this set-up is to move your newly made RSA key to Bitbucket. This will allow your computer and Bitbucket to directly talk to each other through RStudio without having to enter any usernames or passwords in the future. Let’s start by coming back to our familiar RStudio Git/SVN preferences, as shown below.

rstudio_git

Click on “View public key”. A box should pop-up with a long string of letters and numbers. Copy all of the text in the box. It should look like the image below.

rstudio_rsa_key.png

Now we’ll logon to Bitbucket. On the homepage click on the icon of you, or of a faceless person, in the top right hand corner. From the drop down menu choose “Bitbucket settings”. On the left hand side look for the menu section “SECURITY” and click on “SSH keys”. Finally, click on “Add key”. A window will pop up for you to fill out. For “Label” you can put anything you want, e.g. “home”, “work”, “MacBook Pro”, etc. In the box for “Key” paste the RSA key that you copied earlier. The webpage with the window popped up should look like the screenshot below.

bitbucket

You’ve now linked RStudio and Bitbucket so you’re ready to start pushing your code up to the internet!

Installing Packages in RStudio

The final part of the set-up is be sure you can install packages in R, and more specifically RStudio. There’s two packages in particular that we’ll be using right off the bat, dplyr and ggplot. Go back to RStudio and in the Console type the following:

install.packages("dplyr") 

If you have never installed a package you will be asked to choose a mirror. Any is fine, but it’s best to pick one near you. R should then begin the installation process. You’ll know that it’s done when the “>” symbol reappears as the only thing on a line in the Console. When it is done installing, type the following in the Console to be sure it installed properly and load the package:

library(dplyr) 

If you get no messages, or just a message about the package’s configuration, you’re in the clear. Your console should look something like the image below.

rstudio_installdplyr.png

If it says the package does not exist you have a problem. There are a couple common problems people have when installing packages. One, some packages are not supported by older versions of R. Try updating to the newest version of R and restarting RStudio (RStudio will detect automatically that you’ve updated). Then try and install and load the package again. If you’re still getting an error it may have to do with the permissions on your computer for writing packages to a given folder. This is a problem I’ve seen a few times on Windows machines. To fix this you’ll need to change some permission settings. Google your specific error message and you should find a solution.

Once you have dplyr installed and loaded, do the same for ggplot2:

install.packages("ggplot2")
library(ggplot2) 

Your console should now look like the following image.

rstudio_installggplot2.png

And that’s it! You’re finally done!

Conclusion and Next Steps

This first pre-lesson lesson focused on getting you up and running with Git in RStudio and Bitbucket and made sure you are able to install and load packages in RStudio. In the next session we’ll start doing some actual R coding including, reading in and manipulating data, making figures, committing to Git, and creating an R Markdown document to summarize our work.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: