The features that we will discuss in this chapter are spread across the four main panels of RStudio. Most panels harbor multiple tabs with different functionalities. The main panels shown in the following figure (in clockwise order) are as follows:
The
source editor and data viewer panel: This panel can harbor a variable number of tabs, each containing an open (source) file or a view of a data.frame
The command history and workspace browser: When working with RStudio projects, a tab for version control features can be added
The R console: This panel helps in working directly with R. It has no separate tabs
The file, help, package, and plots panel : This panel is used for browsing files, viewing help, searching, and package (un)loading and installation
Each tab in each panel has its own set of menu items, relevant for the content of that tab.
Every panel has a maximize/minimize button at the top right-hand side. When maximized or minimized, the respective button...
We will now talk about various features of the R console in this section.
The most direct way to work with R is by entering commands straight in the console. When RStudio is started for the first time, its interface to the R console is on the left-hand side. The console window has three buttons on its top bar. On the right-hand side, there are two buttons that minimize or maximize the command window. On the left-hand side, just after the word Console, the current working directory is shown. On the right-hand side is an arrow that, when clicked, opens the file browser on the right-hand side to view RStudio's current working directory.
To execute a command from the console, type it after the prompt (the >
symbol) and press Enter. The command is sent to the R engine, executed, and printed back to the screen in a different color. This is the first example of what is called
syntax highlighting to which we will return extensively in the next subsection...
The most important panel in RStudio is the source editing panel. This is where you write your R scripts and probably spend most of your time working on the project. It has several features that make writing R scripts in RStudio much more comfortable than most other editors. The editor panel of RStudio supports editing several file formats such as HTML, Sweave, Markdown, C, C++, and JavaScript files. In this chapter we will discuss editing R scripts, leaving features for some of the other languages to Chapter 5, Generating reports.
For easy editing and code inspection, the appearance of code in the editor can be customized. Code folding allows you to temporarily hide user-defined sections or indented blocks (functions, loops, and so on). RStudio also offers shortcuts and menus that allow for quick navigation between blocks and sections.
Long scripts with many blocks of code can be hard to read. Often this is an indication that the script should be split into multiple files, but alternatively RStudio has a code folding feature that allows you to collapse blocks of code. All the blocks with curly brackets ({}) and code sections (see the following code snippets) can be folded. All foldable code is preceded with a small triangle. Clicking on the triangle collapses or expands a code block. That a block of code is collapsed can also be seen in the gap of line numbers. Here, the folded/collapsed function, the body of the function at line 5, is collapsed:
There are several ways to execute code in the script editing window. Most of them literally copy the lines with R script to the console window and execute them.
To execute the current line or selection use Ctrl+Enter. The previous command can be rerun using Ctrl+Shift+P.
Code completion of your code in the editor window will only work if the objects are available in your workspace. Make sure that you execute the assignment of objects in the editor.
Executing a script file line by line is tedious. So RStudio makes it easy to execute (or source) all the lines of a script file with Ctrl+Shift+Enter. This will copy all the lines to the console and execute them. The output of the script is printed in the console windows. Note that RStudio treats the execution of all the lines as one statement.
It is also possible to source the current file without printing statements in the console. This can be done with Ctrl+Shift+S . RStudio makes this even easier with the Source on Save option...
This chapter treated the editing and completion features of the console and methods for command history retrieval and storage. Important features of the source code editor were discussed, including code appearance features (highlighting, sectioning, folding), editing features (find/replace, commenting, indentation), and code execution. Some of the more advanced features, such as function extraction and the code viewer have not been discussed here yet. These are left to Chapter 6, Using RStudio Effectively.
Now we know how to write scripts in RStudio it is time to see how data can be viewed and plotted.