Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning RStudio for R Statistical Computing

You're reading from  Learning RStudio for R Statistical Computing

Product type Book
Published in Dec 2012
Publisher Packt
ISBN-13 9781782160601
Pages 126 pages
Edition 1st Edition
Languages
Concepts

Chapter 2. Writing R Scripts and the R Console

In this chapter we will discuss the two panels of RStudio that are used the most—the console and the source editor. Additionally we discuss the history panel.

Moving around RStudio


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...

Features of the R console


We will now talk about various features of the R console in this section.

Executing commands

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...

Features of the source editor


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.

Tip

Every code completion feature described in the previous section also works in the source editor.

If you're accustomed to the Vi or Vim editor you can let RStudio emulate some of their properties by going to Tools | Options | Code editing and selecting Enable Vim Editing mode.

A few words on code quality

A development process, either for a software project or when authoring a statistical analysis, is unavoidably comprised of writing...

Folding, sectioning, and navigation


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.

Code folding

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:

Keyboard shortcuts for...

Code execution


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.

Tip

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...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning RStudio for R Statistical Computing
Published in: Dec 2012 Publisher: Packt ISBN-13: 9781782160601
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}