3 Navigating the RStudio Interface
If you followed along with the previous chapters, you have R and RStudio installed on your computer and you have some idea of what R and RStudio are. At this point, it can be common for people to open RStudio and get totally overwhelmed. “What am I looking at?” ”What do I click first?” “Where do I even start?” Don’t worry if these, or similar, thoughts have crossed your mind. You are in good company and we will start to clear some of them up in this chapter.
When we load RStudio, we should see a screen that looks very similar to Figure 3.1 below. There, we see three panes, and each pane has multiple tabs.
3.1 The console pane
The first pane we are going to talk about is the console/terminal/background jobs pane.
It’s called the “console/terminal/background jobs” pane because it has three tabs we can click on by default: “console”, “terminal”, and “background jobs”. However, we will refer to this pane as the “console pane” and will mostly ignore the terminal and background jobs tabs for now. We aren’t ignoring them because they aren’t useful; instead, we are ignoring them because using them isn’t essential for anything we will discuss in this chapter, and we want to keep things as simple as possible for now.
The console is the most basic way to interact with R. We can type a command to R into the console prompt (the prompt looks like “>”) and R will respond to what we type. For example, below we typed “1 + 1,” pressed the return/enter key, and the R console returned the sum of the numbers 1 and 1.
The number 1 we see in brackets before the 2 (i.e., [1]) is telling us that this line of results starts with the first result. That fact is obvious here because there is only one result. So, let’s look at a result that spans multiple lines to make this idea clearer.
In Figure 3.4 we see examples of a couple of new concepts that are worth discussing.
First, as promised, we have more than one line of results (or output). The first line of results starts with a 1 in brackets (i.e., [1]), which indicates that this line of results starts with the first result. In this case, the first result is the number 2. The second line of results starts with a 29 in brackets (i.e., [29]), which indicates that this line of results starts with the twenty-ninth result. In this case, the twenty-ninth result is the number 58. If we count the numbers in the first line, there should be 28 – results 1 through 28. We also want to make it clear that “1” and “29” are NOT results themselves. They are just helping us count the number of results per line.
The second new thing that you may have noticed in Figure 3.4 is our use of a function. Functions are a BIG DEAL in R. So much so that R is called a functional language. We don’t really need to know all the details of what that means; however, we should know that, in general, everything we do in R we will do with a function. By contrast, everything we create in R will be an object. If we wanted to make an analogy between the R language and the English language, we could think of functions as verbs – they do things – and objects as nouns – they are things. This distinction likely seems abstract and confusing at the moment, but we will make it more concrete soon.
Most functions in R begin with the function name followed by parentheses. For example, seq()
, sum()
, and mean()
.
Question: What is the name of the function we used in the example above?
Answer: We used the seq()
function – short for sequence - in the example above.
You may notice that there are three pairs of words, equal symbols, and numbers that are separated by commas inside the seq()
function. They are, from = 2
, to = 100
, and by = 2
. The words from
, to
, and by
are all arguments to the seq()
function. We will learn more about functions and arguments later. For now, just know that arguments give functions the information they need to give us the result we want.
In this case, the seq()
function returns a sequence of numbers. But first, we had to give it information about where that sequence should start, where it should end, and how many steps should be in the middle. Above, the sequence began with the value we passed to the from
argument (i.e., 2), it ended with the value we passed to the to
argument (i.e., 100), and it increased at each step by the number we passed to the by
argument (i.e., 2). So, 2, 4, 6, 8 … 100.
Whether you realize it or not, we’ve covered some important programming terms while discussing the seq()
function above. Before we move on to discussing RStudio’s other panes, let’s quickly review and reinforce a few of terms we will use repeatedly in this book.
Arguments: Arguments always live inside the parentheses of R functions and receive information the function needs to generate the result we want.
Pass: In programming lingo, we pass a value to a function argument. For example, in the function call
seq(from = 2, to = 100, by = 2)
we could say that we passed a value of 2 to thefrom
argument, we passed a value of 100 to theto
argument, and we passed a value of 2 to theby
argument.Return: Instead of saying, “the
seq()
function gives us a sequence of numbers…” we say, “theseq()
function returns a sequence of numbers…” In programming lingo, functions return one or more results.
🗒Side Note: The seq()
function isn’t particularly important or noteworthy. We essentially chose it at random to illustrate some key points. However, arguments, passing values, and return values are extremely important concepts and we will return to them many times.
3.2 The environment pane
The second pane we are going to talk about is the environment/history/connections pane in Figure 3.5. However, we will mostly refer to it as the environment pane and we will mostly ignore the history and connections tab. We aren’t ignoring them because they aren’t useful; rather, we are ignoring them because using them isn’t essential for anything we will discuss anytime soon, and we want to keep things as simple as possible.
The Environment pane shows you all the objects that R can currently use for data management or analysis. In this picture, Figure 3.5 our environment is empty. Let’s create an object and add it to our environment.
Here we see that we created a new object called x
, which now appears in our Global Environment. Figure 3.6 This gives us another great opportunity to discuss some new concepts.
First, we created the x
object in the console by assigning the value 2 to the letter x. We did this by typing “x” followed by a less than symbol (<), a dash symbol (-), and the number 2. R is kind of unique in this way. we have never seen another programming language (although I’m sure they are out there) that uses <-
to assign values to variables. By the way, <-
is called the assignment operator (or assignment arrow), and ”assign” here means “make x contain 2” or “put 2 inside x.”
In many other languages you would write that as x = 2
. But, for whatever reason, in R it is <-
. Unfortunately, <-
is more awkward to type than =
. Fortunately, RStudio gives us a keyboard shortcut to make it easier. To type the assignment operator in RStudio, just hold down Option + - (dash key) on a Mac or Alt + - (dash key) on a PC and RStudio will insert <-
complete with spaces on either side of the arrow. This may still seem awkward at first, but you will get used to it.
🗒Side Note: A note about using the letter “x”: By convention, the letter “x” is a widely used variable name. You will see it used a lot in example documents and online. However, there is nothing special about the letter x. We could have just as easily used any other letter (a <- 2
), word (variable <- 2
), or descriptive name (my_favorite_number <- 2
) that is allowed by R.
Second, you can see that our Global Environment now includes the object x
, which has a value of 2. In this case, we would say that x
is a numeric vector of length 1 (i.e., it has one value stored in it). We will talk more about vectors and vector types soon. For now, just notice that objects that you can manipulate or analyze in R will appear in your Global Environment.
R is a case sensitive language. That means that uppercase x (X) and lowercase x (x) are different things to R. So, if you assign 2 to lower case x (x <- 2
). And then later ask R to tell what number you stored in uppercase X, you will get an error (Error: object 'X' not found
).
3.3 The files pane
Next, let’s talk about the Files/Plots/Packages/Help/Viewer pane (that’s a mouthful). Figure 3.7
Again, some of these tabs are more applicable for us than others. For us, the files tab and the help tab will probably be the most useful. You can think of the files tab as a mini Finder window (for Mac) or a mini File Explorer window (for PC). The help tab is also extremely useful once you get acclimated to it.
For example, in the screenshot above Figure 3.8 we typed the seq
into the search bar. The help pane then shows us a page of documentation for the seq()
function. The documentation includes a brief description of what the function does, outlines all the arguments the seq()
function recognizes, and, if you scroll down, gives examples of using the seq()
function. Admittedly, this help documentation can seem a little like reading Greek (assuming you don’t speak Greek) at first. But, you will get more comfortable using it with practice. We hated the help documentation when we were learning R. Now, we use it all the time.
3.4 The source pane
There is actually a fourth pane available in RStudio. If you click on the icon shown below you will get the following dropdown box with a list of files you can create. Figure 3.9
If you click any of these options, a new pane will appear. We will arbitrarily pick the first option – R Script.
When we do, a new pane appears. It’s called the source pane. In this case, the source pane contains an untitled R Script. We won’t get into the details now because we don’t want to overwhelm you, but soon you will do the majority of your R programming in the source pane.
3.5 RStudio preferences
Finally, We’re going to recommend that you change a few settings in RStudio before we move on. Start by clicking Tools
, and then Global Options
in RStudio’s menu bar, which probably runs horizontally across the top of your computer’s screen.
In the General
tab, we recommend turning off the Restore .Rdata into workspace at startup
option. We also recommend setting the Save workspace .Rdata on exit
dropdown to Never
. Finally, we recommend turning off the Always save history (even when not saving .Rdata)
option.
We change our editor theme to Twilight in the Appearance
tab. We aren’t necessarily recommending that you change your theme – this is entirely personal preference – we’re just letting you know why our screenshots will look different from here on out.
It’s likely that you still have lots of questions at this point. That’s totally natural. However, we hope you now feel like you have some idea of what you are looking at when you open RStudio. Most of you will naturally get more comfortable with RStudio as we move through the book. For those of you who want more resources now, here are some suggestions.