To quit your R session, type the following code in the console, after the command prompt (>
):
> quit()
R asks you a question to make sure that you meant to quit, as shown in Figure 2-3. Click No, because you have nothing to save. This action closes your R session (as well as RGui, if you’ve been using RGui as your code editor). In fact, saving a workspace image rarely is useful.
Figure 2-3: R asks you a simple question.
RStudio is a code editor and development environment with some very nice features that make code development in R easy and fun:
✔ Code highlighting that gives different colors to keywords and variables, making it easier to read
✔ Automatic bracket and parenthesis matching
✔ Code completion, so you don’t have to type out all commands in full
✔ Easy access to R Help, with some nice features for exploring functions and parameters of functions
✔ Easy exploration of variables and values
Because RStudio is available free of charge for Linux, Windows, and Apple OS X, we think it’s a good option to use with R. In fact, we like RStudio so much that we use it to illustrate the examples in this book. Throughout the book, you find some tips and tricks on how things can be done in RStudio. If you decide to use a different code editor, you can still use all the code examples and you’ll get identical results.
To open RStudio, click the RStudio icon in your menu system or on your desktop. (You can find installation instructions in this book’s appendix.)
Once RStudio starts, choose File⇒New⇒R Script to open a new script file.
Your screen should look like Figure 2-4. You have four work areas (also called panes):
✔ Source: The top-left corner of the screen contains a text editor that lets you work with source script files. Here, you can enter multiple lines of code, save your script file to disk, and perform other tasks on your script. This code editor works a bit like every other text editor you’ve ever seen, but it’s smart. It recognizes and highlights various elements of your code, for example (using different colors for different elements), and it also helps you find matching brackets in your scripts.
✔ Console: In the bottom-left corner, you find the console. The console in RStudio can be used in the same way as the console in RGui (refer to “Seeing the naked R console,” earlier in this chapter). This is where you do all the interactive work with R.
✔ Environment and History: The top-right corner is a handy overview of your environment, where you can inspect the variables you created in your session, as well as their values. (We discuss the environment in more detail later in this chapter.) This is also the area where you can see a history of the commands you’ve issued in R.
✔ Files, plots, package, help, and viewer: In the bottom-right corner, you have access to several tools:
● Files: This is where you can browse the folders and files on your computer.
● Plots: This is where R displays your plots (charts or graphs). We discuss plots in Part V.
● Packages: You can view a list of all installed packages.
A package is a self-contained set of code that adds functionality to R, similar to the way that add-ins add functionality to Microsoft Excel.
● Help: This is where you can browse R's built-in Help system.
● Viewer: This is where RStudio displays previews of some advanced features, such as dynamic web pages and presentations that you can create with R and add-on packages.
Figure 2-4: RStudio’s four work areas (panes).
Starting Your First R Session
By now, you probably are itching to get started on some real code. In this section, you get to do exactly that. Get ready to get your hands dirty!
Programming books typically start with a very simple program. Often, this first program creates the message "Hello world!"
. In R, hello world program consists of one line of code.
Start a new R session, type the following in your console, and press Enter:
> print("Hello world!")
R responds immediately with this output:
[1] "Hello world!"
As we explain in the introduction to this book, we collapse input and output into a single block of code, like this:
> print("Hello world!")
[1] "Hello world!"
Type the following in your console to calculate the sum of five numbers:
> 1 + 2 + 3 + 4 + 5
[1] 15
The answer is 15
, which you can easily verify for yourself. You may think that there’s an easier way to calculate this value, though – and you’d be right. We explain how in the following section.
A vector is the simplest type of data structure in R. The R manual defines a vector as “a single entity consisting of a collection of things”. A collection of numbers, for example, is a numeric vector – the first five integer numbers form a numeric vector of length 5.
To construct a vector, type into the console:
> c(1, 2, 3, 4, 5)
[1] 1 2 3 4 5
In constructing your vector, you have successfully used a function in R. In programming language, a function is a piece of code that takes some inputs and does something specific with them. In constructing a vector, you tell the c()
function to construct a vector with the first five integers. The entries inside the parentheses are referred to as arguments.
You also can construct a vector by using operators. An operator is a symbol you stick between two values to make a calculation. The symbols +
, -
, *
, and / are all operators, and they have the same meaning they do in mathematics. Thus, 1+2
in R returns the value 3
, just as you’d expect.
One very handy operator is called sequence, and it looks like a colon (:
). Type the following in your console:
> 1:5
[1] 1 2 3 4 5
That’s more like it. With three keystrokes, you’ve generated a vector with the values 1
through 5
. To calculate the sum of this vector, type into your console:
> sum(1:5)
[1] 15
While quite basic, this example shows