Overview

The goal of this lab is to introduce you to R and RStudio, which you’ll be using throughout the course both to learn the statistical concepts discussed in the course and also to analyze real data and come to informed conclusions.

To clarify which is which:

  • R is the name of the programming language itself
  • RStudio is what’s called an Interactive Development Environment (IDE); an interface through which we can use the R language

Before we start, let’s get RStudio running.

Getting RStudio Running

There are two ways you can run R and RStudio:

  • log in to the Oberlin RStudio server from a web browser, or
  • install the software on your own computer.

Why use the server?

  • you can get to your account, and your files, from anywhere,
  • you don’t have to install anything.
  • we will use it to turn in assignments

Why might you want to install the software on your computer?

  • you have to upload and download files to the server if you create them on your computer and want to use them in RStudio, or if you want to do something on your computer with a file that you created in RStudio
  • there may be occasional server outages or slow downs when a lot of people are using it at once.

That said, I don’t recommend trying to do your own R installation yet unless you have experience with it already

Logging in to the server

For today, even if you have R/RStudio installed on your machine, you should log in to the server so you know how to do so.

Here’s how to do it

  • In your web browser, visit rstudio.oberlin.edu.
  • If you filled out the background survey you should have an account
    • Your username should be your Obie ID as in your email (e.g., mine is cdawson)
    • the initial password is the same as your username (the lab account is not actually linked to your Oberlin ID)
    • Even if you had an account before, your password has been reset
  • Before moving on, change your password
    • Go to the Tools dropdown menu and select Shell…
    • Type passwd at the terminal
    • Enter your old password, followed by a new password twice (the cursor will not move or show anything, but the computer is registering your keystrokes)
    • Close the terminal window
  • Keep track of what you set your password to. Your browser will likely keep you logged in by default so you will not necessarily need to log in every time, but if you should have to clear your browser cache you’ll need to know what it is. If it needs to be reset at some point you’ll have to ask me to do it; there’s no “Forgot My Password” email option or anything.

RStudio IDE

This orientation will cover fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.

Environment pane

The panel in the upper right has tabs for your

  • Environment (variables and datasets currently loaded in memory for your session) as well
  • History: commands that you’ve previously entered

Files, Plots, Packages, Help, Viewer

The pane in the lower right has tabs for

  • Files: The files stored in your user account (by default shows your home directory; you can create folders within here to organize your stuff). You can also upload files from your computer to here (“Upload” button), or download files from here to your computer (“More” > “Export”).
  • Plots: Shows plots you generate in the console or in a script
  • Packages: Shows R “packages” (code modules that add functionality) currently installed and loaded into your session. You won’t need to interact with this; the packages you need will be installed for you, and loading packages into your session will happen in your documents.

The Console

The panel on the left is called the console. When you first launch RStudio, it shows what version of R that you’re running, etc.

Below that information is the prompt. As its name suggests, this prompt is really a request, namely a request for a command: a line of R code that tells the computer to do something.

Enter some simple arithmetic expressions like the following at the console and verify that the output is as expected. (Note that the asterisk represents multiplicationn and the slash is for division.)

Useful tip: When your cursor is at the console, you can use the up and down arrows to cycle through recently typed commands.

Creating a Project

Each time you begin a new lab or assignment, you should create a new project so you can keep your work distinct from one assignment to the next.

  • Find the dropdown menu in the upper right that says “Project (None)”, click on it and select “New Project”.
  • In the window that pops up choose “New Directory”, then “New Project”.
  • I have created a folder on your user account called stat209. Click “Browse” and choose that directory as the home of your new project.
  • Call your project R-orientation. Type this name in the “Directory Name”. Avoid using spaces or special characters (such as punctuation) in directory names. For multiword names you can use CamelCase, or separate words with-hyphens or with_underscores. Pick a convention and stick with it.

Whenever you begin a new assignment, it is a good idea to create a new project. This will help keep things you did in a previous session from interfering with what you are trying to do in this session.

R Packages

R is an open-source programming language, meaning that users can contribute packages that make our lives easier, and we can use them for free. We will use a handful of different packages that make doing modeling and the tasks related to modeling simpler.

Note that you can check to see which packages (and which versions) are installed by inspecting the Packages tab in the lower right panel of RStudio. Don’t install packages yourself on the server: This could result in two different versions being installed at once, causing you to see different behavior than others in the class

R Markdown

We will use a document format called RMarkdown that helps us keep our work better organized. It is basically like a Word document with some stripped down formatting capability, but with an important difference: you can embed R commands into your document in “code chunks”, which will be run and produce output when you “compile” your document.

This instruction document was created using RMarkdown.

  • Open the file markdown-tutorial.Rmd in your stat213/originals/R-orientation folder. This will cause the left-hand part of R Studio to split in half, the console moving to the bottom, and a new pane appearing in the upper left that has a tab showing the contents of the file.
  • Save a copy of this file as markdown-tutorial.Rmd in your project directory (you will not be able to modify the original or any of the files in the originals directory, which is read only)
  • The next part of the instructions for this orientation are in that file.