Chapter 1. The RStudio IDE – an Overview

The number of users adopting the R programming language has been increasing faster and faster in the last few years. It is not just used for smaller analyses, but also for bigger projects, and often, several people collaborating on the same project. The functions of the R console are limited when it comes to managing a lot of files, or when we want to work with version control systems. This is the reason, in combination with the increasing adoption rate, why a need for a better development environment arose. To serve this need, a team of R fans began to develop an integrated development environment (IDE) to make it easier to work on bigger projects and to collaborate with others. This IDE has the name, RStudio. We will introduce you to this fantastic software and show you how to take your R programming to the next level. Mastering the use of RStudio will help you solve real-world problems faster and more effectively.

In this chapter, we will introduce you to the RStudio interface and build the foundation for more advanced topics in the following chapters.

This chapter covers the following topics:

Before installing RStudio, you should install R on your computer. RStudio will then automatically search for your R installation.

RStudio is based on the R framework and it requires, at least, R version 2.11.1, but we highly recommend that you install the latest version. The latest version of R is 3.2.2, as of September 2015.

We assume that most readers are using Windows or Mac OS systems. The installation of R is pretty simple. Just go to http://cran.rstudio.com, download the proper version of R for your system, and install it using the default setting.

We would like to leave more space to talk about installing R on different Linux distributions. As there are a huge number of different Linux distributions out there, we will focus, in this book, on the most used one: Ubuntu.

RStudio's source editor was developed in a fully functional R editor over the last few years. It has a powerful syntax highlighter that works with not only every format connected to R development, such as R Scripts, R Markdown, or R documentation files, but also C++, JavaScript, HTML, and many more.

We've already created a new R script file and can now demonstrate some of the code editor's functions. You can also open an existing R document by clicking on File | Open File, or by using the shortcut, Ctrl + O.

The code editor works with tabs, which gives you the possibility of opening several files at the same time, as you can see in the following screenshot. If there are unsaved changes in a file, their names will be highlighted in red and marked with an asterisk.

The Source editor pane

If you have several files opened, you will see a double arrow in the menu of the source code editor. This will open a small menu showing you an overview of all the opened files. You can also search for a specific file.

The Source editor pane

Under the tabs with the opened files, you can see a toolbox with tools for the code editor. For example, you have the Source on Save checkbox. This is a really handy tool especially when you are working on a reusable function. If activated, the function is automatically sourced to the global environment and we do not have to source it manually again after editing the code.

Another function you can find in the toolbox is the search and replace tool. This is known from a lot of text editors and helps you find existing code and replace it. RStudio also offers different options for your search, such as In selection, to just search in the code you selected in the editor or Match case, to make the search case-sensitive. This is demonstrated in the following screenshot:

The Source editor pane

RStudio highlights parts of your code according to the R language definition. This makes your code much easier to read. The default settings are:

With the default settings, this pane consists of the tabs, Environment and History. You can use the shortcut, Ctrl + 8, to switch to the Environment browser, and Ctrl + 4 to switch to the History window:

The Environment and History panes

The Environment pane is one of the biggest advantages of RStudio. It gives you an overview over all objects currently available in an environment. So, you can see a list of all data, values, and functions.

The Environment browser shows you the number of observations and the number of variables in the second column. If you want to get a better overview of a dataset, you can click on the table symbol at the end of the row.

The Environment and History panes

When you click on the blue and white arrow next to the name of an object, you will see its structure. This is basically the output of the str() function, but in a more structured way.

The Import Dataset button offers you an easy way to import data. It basically uses the read.csv() function but offers you a graphical interface to set the parameters for the import. You can either import the dataset from a local file, or you can choose an import from a URL.

The Environment and History panes

Furthermore, the Environment pane gives you the possibility of clearing the environment, which will delete all defined variables and also all sourced functions.

This pane is, like the name says, divided into five sub panes: Files, Plots, Packages, Help, and Viewer.

The fastest way to use RStudio is by using it with keyboard shortcuts. In the previous text, we already mentioned some of them. But we put the most important ones together in a table, which is as follows:

Description

Windows and Linux

Mac

Move the focus to the Source editor

Ctrl + 1

Ctrl + 1

Move the focus to console

Ctrl + 2

Ctrl + 2

Move the focus to Help

Ctrl + 3

Ctrl + 3

Show the History pane

Ctrl + 4

Ctrl +4

Show the Files pane

Ctrl + 5

Ctrl +5

Show the Plots pane

Ctrl + 6

Ctrl + 6

Show the Packages pane

Ctrl + 7

Ctrl + 7

Show the Environment pane

Ctrl + 8

Ctrl + 8

Open the document

Ctrl + O

Command + O

Run the current line/section

Ctrl + Enter

Command + Enter

Clear the console

Ctrl + L

Command + L

Extract the function from the selection

Ctrl + Alt + X

Command + Option + X

Source the current document

Ctrl + Shift + Enter

Command + Shift + Enter

Toggle the breakpoint

Shift + F9

Shift + F9