3 Getting to know RStudio
RStudio is an IDE (Integrated development environment) for the R programming language made by a company called Posit. You can install RStudio by visiting this link1. Posit, also develops Shiny, a package to create full-fledged web-apps. I am not going to cover Shiny in this book, since there’s already a lot2 of material that you can learn from.
Once you have installed RStudio, launch it and let’s go through the interface together.
3.1 Panes
RStudio is divided into different panes. Each pane has a specific function. The image below shows some of these panes:
Take some time to look around what each pane shows you. Some panes are empty; for example the Plots pane or the Viewer pane. Plots shows you the plots you make. You can browse the plots and save them. We will see this in more detail in a later chapter. Viewer shows you previews of documents that you generate with R. More on this later. You can also minimize and maximize the panes by clicking these two buttons:
3.2 Console
The Console pane is where you can execute R code. Write the following in the console:
2 + 3
and you’ll get the answer, 5
. However, do not write a lot of lines in the console. It is better write your code inside a script. Output is also shown inside the console.
3.3 Scripts
Instead of writing code in the console, it is better to write code in a so-called script. Scripts are simple text files that can be written and executed by RStudio. To write a new script, click on the top-right icon and select “R script”:
In Figure 3.4, we see the user creating a new R script. If you have a background in the social sciences you might be familiar with STATA: STATA also uses scripts, colloquially called .do
files. The C programming language uses .c
files. R scripts have the .r
or .R
extension. But .R
files are not the only type of files that you can edit with RStudio. We will explore other formats later in the book.
3.3.1 The help pane
The Help pane allows you to consult documentation for R, its packages etc:
You can also read the help file of a specific function by writing ?function
in the console, where function
is the function you want to know about.
I highly recommend you take some time to check out the “CRAN Task Views”. These views provide a very nice summary of the different packages available for different scientific fields. For example, if you’re doing econometrics, you should read the CRAN Task View: Econometrics3.
Take some time to browse the different CRAN Task Views here4.
3.3.2 The Environment pane
The Environment pane shows every object created in the current section. It is especially useful if you have defined lists or have loaded data into R as it makes it easy to explore these more complex objects. As you will write code and create objects throughout a session, the environment pane will get more and more populated.
3.4 Projects
One of the best features of RStudio are projects. Creating a project is simple; simply click on the top right corner of RStudio and then “New Project”:
Projects make a lot of things easier, such as managing paths (more on this in the chapter about reading data). Another useful feature of projects is that the scripts you open in project A will stay open even if you switch to another project B, and then switch back to the project A again.
You can also use version control (with Git) inside a project. Version control is very useful, but I won’t discuss it in this book.
3.5 History
The History pane saves all the previous lines you executed. You can then select these lines and send them back to the console or the script.
3.6 Plots
All the plots you make during a session are visible in the Plots pane. From there, you can export them in different formats.
There are several ways to create plots: later in the book I will teach you some basics of the {ggplot2}
package.
3.7 Addins
Some packages install addins, which are accessible through the addins button:
These addins make it easier to use some functions and you can read more about them here5.
There are other panes that I will not discuss here, but you will naturally discover their use as you go. For example, we will discuss the Build pane in Chapter 11.
3.8 Packages
You can think of packages as addons that extend R’s core functionality. You can browse all available packages on CRAN6. To make it easier to find what you might be interested in, you can also browse the CRAN Task Views as mentioned previously. Each package has a landing page that summarises its dependencies, version number etc. For example, for the {dplyr}
package: https://CRAN.R-project.org/package=dplyr. To install a package, you can either type the following command in the console:
install.packages("dplyr")
or you can go to the Packages pane and click on the Install button. Before installing a package, you can consult its manual and vignettes online. For example, if you go back to {dplyr}
’s landing page, you can take a look at the Reference Manual and Vignettes:
Vignettes are valuable documents; inside vignettes, the purpose of the package is explained in plain English, usually with accompanying examples. The reference manuals list the available functions inside the packages. You can also find vignettes from within RStudio (but you need to install the package first):
Once you installed a package, you have to load it before you can use it. To load packages you use the library()
function:
library(dplyr)
library(janitor)
# and so on...
If you only need to use one single function once, you don’t need to load an entire package. You can write the following:
::full_join(A, B) dplyr
using the ::
operator, you can access functions from packages without having to load the whole package beforehand.
It is possible and easy to create your own packages. This is useful if you have to write a lot of functions that you use daily. We will lean about that, in Chapter 10.
3.9 Exercises
Exercise 1
Change the look and feel of RStudio to suit your tastes! I personally like to move the console to the right and use a dark theme. Take some 5 minutes to customize by clicking on Tools and then Global options. Browse through all the options.