This chapter introduces RStudio, a Graphical User Interface (GUI) that makes it easier to use powerful features in R and manage large projects.
New vocabulary:
Recall that R is both a language and an environment. RStudio is an Integrated Development Environment, or IDE, which is an enhanced, feature-rich programming environment with an easy-to-use graphical user interface, or GUI. While the base R environment is mostly text, RStudio has intuitive icons (hence, “graphical”) for point-and-click, automated operations.
RStudio’s layout is comprised of a menu, console, and a series of panes, or windows in the RStudio interface. Most panes are feature-rich and all panes serve a key purpose, but we’ll only focus on the most critical panes for getting started in RStudio.
RStudio requires R 3.0.1+. If you don’t already have R, download it here.
RStudio is free, open source, and easy to install. Select the Desktop edition on their download page:
RStudio is comprised of a main menu and a series of panes, each with their own purpose and features. We focus on the following:
The console pane is where R expressions are evaluated. In other words, this is where your code is executed. Recall that working in-console is also known as working interactively and, typically, working in-console is more often for “quick and dirty” tasks, like printing contents of your working directory.
The Console Pane
The console panel lists your current working directory. Notably, even when using point-and-click mechanics to, e.g. import data or change directories, the code for such tasks will still execute in the console. Such click-to-code operations are called macros.
Pro Tip: When writing a script, especially when writing out new directory paths, it’s sometimes quicker to use use a click-to-code operation and simply copy and paste the macro code from the console to your script.
The source pane contains any opened scripts. In starting a new R session, this pane isn’t visible until you’ve opened a new or preexisting script. Multiple scripts may be opened at one time and are navigable using tabs along the top of the source pane.
The Source Pane
Depending on the type of script, e.g. plain text scripts (.R
), publications (.Rmd
), presentations (.Rpres
), and apps, each script provides different options in the pane’s toolbar. Common options include:
The Plots, Viewer, and Help panes are used to viewing data visualizations, HTML output, and helpful documentation.
The Plots pane allows users to view, export, and publish non-interactive data visualizations. R uses the built-in graphics
package by default, but a variety of packages exist such as lattice
and ggplot2
. While the output displayed is not interactive, it is responsive, i.e it will re-render its scales appropriately if you change the height or width of a plot. Notably, the “Zoom” option opens visualizations in a new window, while the “Export” option allows you to save the image with user-defined dimensions and in a variety of formats.
The Viewer pane renders interactive graphics in HTML with the same options as the Plots pane. Brevity aside, it’s awesome.
The Plots & Viewer Panes
The Help pane is one of the most valuable panes for any R user. By calling function help()
with a dataset, package, or bare function name (i.e. a function name without ()
), its documentation, if available, appears here.
Note: Unless you’re using external data or custom functions, there’s almost always documentation. Whether it’s the unit of measurement for a variable in a dataset or the limits you can specify for a function argument, this little nook in RStudio is invaluable to new and advanced users, alike.
Pro Tip: Instead of the help()
function, you can use the ?
function before an object name, e.g. ?install.packages
.
The Help Pane
The Environment and History panes display the objects in your environment and the history of your in-console commands.
Again, R is both a language and an environment. The Environment pane displays objects that are stored within your session’s workspace, or global environment, which must be recreated or reloaded with each new session. Note the following options:
The Environment Pane
The History pane documents every command you’ve executed in your session. When you select a line, you can paste it directly into the console pane with “To Console” or directly into the source pane with “To Source”.
The History Pane
In RStudio, you can customize both where panes are displayed as well as which panes to show by default.
Panes cannot be removed entirely from the RStudio interface, you but can shuffle them by order of importance. Click the “Tools” dropdown in themenu, “Global Options…”, and “Pane Layout”. We recommend focusing on those discussed in this chapter.
Customizing Pane Layout
There are a couple of panes worth mentioning for new users. However, they are seldom used by advanced users:
Pro Tip: You probably won’t use these panes often. One of the benefits of scripted languages is that they can be reproduced by other users. As a rule, since much of your work will require loading packages, you should include the library()
function with script-dependent packages at the start of every work. The same applies to working directories with the setwd()
function.
If you want to expand a pane, or “zoom”, to fullscreen mode, select “View” in the RStudio menu and “Panes”.
Toggling Fullscreen Panes
Global options are accessed in the “Tools” submenu and allow users to modify their RStudio interface in myriad ways, both aesthetically and functionally. We recommend new users experiment with these options and visit a few notable modifications.
Accessing Global Options
The “Code” section affects the way R automatically formats your code and how you choose to write and run it. It has sensible defaults, many of which you may not be prepared to appreciate quite yet. For now, consider the following:
The Appearance section allow you to customize the size, font, and color of your text as well as the “theme” colors of your RStudio interface. Here, “theme” is both functional and aesthetic. For example, darker themes are more conducive to night owls. For all themes, certain syntax uses different colors to make your code more interpretable - keep this in mind for each theme!
Notable Global Options: Code & Appearance
The following resources are helpful in learning more about RStudio and coding conventions:
I) Full Introductions to RStudio
II) About RStudio
III) Conventions