R for Publication by Page Piccinini: Lesson 0 – Introduction and Set-up

[This article was first published on DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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

There is a video in end of this post which 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

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.

Unix-like Instructions

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
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 "[email protected]"

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 Generating a new ssh key).
terminal_ssh

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

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

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

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.

Related Post

  1. How to export Regression results from R to MS Word
  2. Learn R by Intensive Practice
  3. Learn R from the Ground Up
  4. Table 1 and the Characteristics of Study Population
  5. Learn R From Scratch – Part 3

To leave a comment for the author, please follow the link and comment on their blog: DataScience+.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)