Welcome! This tutorial will walk you through some of the basics of programming and R so that you all are ready when class starts up. This was written initially for ENV710 at Duke University, but anyone can benefit from this tutorial. If you’re using this as an ENV710 student, be sure to email your TA with questions.

The focus of this module is to introduce all of you to R and RStudio, so you can get a feel for what R is and how to use it. If you already have RStudio downloaded and ready to go, you can skip on down to Part 2: All Those Windows.

Programming terms introduced: argument, assign, console, CRAN, echo, environment, extension, function, GUI, open-source, package, programming language, R, RStudio, return, script, value, variable.

Shortcuts introduced: SHIFT+CTRL+N [create new R script]; CTRL+ENTER [run selected lines]; UP arrow [move back through previous commands]. For Mac users, substitute CMD for CTRL.

Follow the prompts labeled Try This to start exploring R on your own.

PART 1: Downloading R and RStudio

This section will get you started with R and RStudio. By the end, you should have both downloaded and able to run on the computer you’re using for this class.

One of the most frequent questions I get for this section is “What’s the difference between R and RStudio? Do I have to have both?” Yes, you do have to have both. R is a programming language, which means that it tells your computer what to do. R is like the words in a book: the information stored inside is what tells your brain what’s going on. RStudio is an interface, often called a GUI for “Graphical User Interface” (said like “gooey”). RStudio is just a nice wrapper for R so that you can program with less stress. Think of RStudio as the cover, pages, pictures, and glossary in a book; you don’t need these things to read a novel, but it’s definitely a lot easier than reading one long line of text.

So, for this class, we need both the novel (R) and all the fixins that make it easy to read (RStudio).

The Novel: Steps to download and install R

  1. Click this link http://archive.linux.duke.edu/cran/ to access the R download page. This is hosted on a website called CRAN (The Comprehensive R Archive Network), and you’ll be using it a lot with R.
  2. Click the appropriate link for your operating system in the “Download and Install R” section.
  3. Next, click the link labeled something like “R-x.x.x.pkg (notarized and signed)”. This will start the download of the latest version of R. At the time of this writing, the latest should be “R-4.0.3.pkg”, but it’s updated regularly.
  4. Once your download is finished, find it in your “Downloads” folder and double-click to open. Follow the instructions on the installer (accepting all the defaults).
  5. You have now installed R! Congrats!

The Fixins: Steps to download and install RStudio

  1. Click this link https://rstudio.com/products/rstudio/download/#download to access the RStudio download page.
  2. Click the appropriate link for your operating system in the “Download and Install R” section. You can skip Step 1 because you already have R installed.
  3. Once your download is finished, find it in your “Downloads” folder and double-click to open. Follow the instructions on the installer (accepting all the defaults).
  4. You have now installed RStudio! Congrats!

PART 2: All Those Windows

This section will orient you to All Those Windows that you see when you first open RStudio. There are a lot of things going on, but I promise that it’ll get less overwhelming once we go over their uses and functions!

Before we get started, find the place where you installed RStudio (likely in your Applications folder) and double-click to open it. If you have any errors or problems so far, STOP and email me () with your question and a copy-paste or screenshot of the issue.

When you open RStudio for the first time, you should see four window panes inside the application. If you have only three, don’t worry, we’ll address that later on down the page. You can choose to change the orientation of the panes (and things like the background and font size/color) from the Preferences menu, but for now, we’ll deal with the panes as they are “out of the box”.

Pane 1: Files, Plots, Packages, Help, Viewer

This pane is going to be your best friend. Here you can see any plots you’ve made, view where your files and packages are, and most importantly, use the Help menu.

Files: This is just like your Finder (Mac) or File Explorer (Windows). You can open files from here.

Plots: This will be empty right now, but this’s where your graphs and figures will show up.

Packages: R is called an open-source language, which means that users can create free content called packages for anyone else to use. We will cover packages in the next module.

Help: Use the Help menu when you don’t know what something does. Type the function you’re curious about into the search bar to pull up the documentation. If you need an example, just scroll down to the bottom of the page.

Viewer: This lets you look at web content; you won’t need it right now.

Try This: Type sum into the Help search bar. A document should show up describing the sum() function in R. Does its description make sense to you?

Try This: Click on the check boxes in the Packages menu. What happens in the console?

Pane 2: The Console

All right, now we’re at the console! You might also see tabs labeled Terminal, R Markdown, and Jobs, but just ignore those for now.

The console is your main interface to R. Anything you type in there, the console will tell R to do it. The most basic thing we can do with R is to use it as a calculator. Type 5 + 6 into the console, then press enter. There will be two numbers that R returns to you: [1] and 11. If 11 is our answer, then what is the [1] for? This is R’s way of telling us that 11 is the first thing it’s returning. Since there’s only one thing, it’s kind of redundant for now; later on, though, we will deal with things like lists and data frames that might deal with more than one item.

Remember that sum() function we looked up earler? Type sum(5, 6) into the console. Press enter to see the results—is it what you expected? The description says that sum “returns the sum of all the values present in its arguments”. sum is a function that takes arguments. An argument is something that you give to the function for it to do something with. In this case, we gave sum, the function, arguments of 5 and 6. Then, sum will return, or give us back, the sum of those two arguments.

Try This: There are a lot of other functions you’ll be familiar with from an introductory statistics course; explore concepts like mean() and mode() with different arguments. If you don’t remember what these terms mean, you can use the Help menu or type ?mean into the console.

Pane 3: Environment and History

Environment: Your environment is everything that you’ve defined in this session of R. Some people use this tab a lot, others not so much. It’s really up to you. I like it because I can see everything that I’ve saved so far, in the state that I saved it.

You probably don’t have anything in here yet, so head back to the console and type a <- 3. This is called assigning a variable; you’re telling R that you want the variable a to hold the value 3, by linking them with the little left-arrow <-. The arrow is unique to R, and most other programming languages just use a = instead. It’s fine if you want to do it this way. One of the fun things about R is that there are many ways to accomplish the same task, and there is rarely one ‘right’ way to do things. You can come up with your own personal style.

You should see something change in the Environment now—a little chart that tells you that a equals 3. We’ll talk in the next section about this in more detail, but you can see if you’ve got a lot of variables floating around, how nice it is to have a list of all of them.

History: Exactly what it sounds like. It holds everything you’ve typed in so far this session.

Try This: Type 3 <- a into the console. What happens?

Try This: Go to the console and press the UP arrow key on your keyboard. What happens? And when you press enter? Try scrolling up even farther.

Pane 4: Scripts

By this point you may have realized that you don’t have a 4th pane. If not, no worries; the last pane is to hold your scripts, which are like a list of instructions for R to carry out. Instead of typing in things like 5 + 6 and a <- 3 every time you open up RStudio, you can save your work. To create a new script, go to the very top left corner of your screen and click the white box with a green plus on it, then select “R Script”. Or, try SHIFT+CTRL+N (SHIFT+CMD+N for Mac users) for a shortcut. Either way, a new script should appear in the fourth pane. You can also go to the 01_Orientation folder and double-click the file with a ‘.R’ extension at the end of its name; this will open a blank script you can use for this tutorial.

Scripts work just the same as your console, but (1) you can save your work, and (2) you can run many lines at once. Try typing this into your script: a <- 8, then press enter, then type a + 3 in the next line. Each command in a script in R must be on a separate line.

Now, there are a couple of ways to get what you just typed over to the console. You could copy and paste it over, but that would be silly. Instead, highlight the two lines you just typed, then click the green right-arrow that says “Run” on the top of your scripts section. You should see both lines echoed over in the console now, with the answer as well.

Anything you highlight will run in order. You can also run the selected code by pressing CTRL+ENTER (CMD+ENTER for Mac users).

Try This: If you don’t highlight anything and press Run, what happens?

Try This: Create some variables in your new script then save and close RStudio. Reopen the file. What happened to the environment? Highlight the entire script and run it. What does the environment look like now?


And now we’re done! You should now be familiar with all the windows and panes in RStudio. Try going to your settings to customize how you want your RStudio to look. You’ll be using this a lot, so it’s worth your time. For me, I have my scripts in top left, console top right, environment bottom left, and plots/help bottom right.

When you’re ready, move on to the next module: Just the Basics.