RStudio IDE

Editing and Executing Code

Overview

RStudio's source editor includes a variety of productivity enhancing features including syntax highlighting, code completion, multiple-file editing, and find/replace.

RStudio also enables you to flexibly execute R code directly from the source editor. For many R developers this represents their preferred way of working with R. By executing commands from within the source editor rather than the console it is much easier to reproduce sequences of commands as well as package them for re-use as a function. These features are described in the Executing Code section below.

Managing Files

RStudio supports syntax highlighting and other specialized code-editing features for the following types of files:

  • R scripts
  • Sweave documents
  • TeX documents

To create a new file you use the File -> New menu:

To open an existing file you use either the File -> Open menu or the Open Recent menu to select from recently opened files.

If you open several files within RStudio they are all available as tabs to facilitate quick switching between open documents. If you have a large number of open documents you can also navigate between them using the >> icon on the tab bar or the View -> Switch to Tab) menu item:

Code Completion

RStudio supports the automatic completion of code using the Tab key. For example, if you have an object named pollResults in your workspace you can type poll and then Tab and RStudio will automatically complete the full name of the object.

Code completion also works in the console, and more details on using it can be found the console Code Completion documentation.

Find and Replace

RStudio supports finding and replacing text within source documents:

Find and replace can be opened using the Ctrl+F shortcut key, or from the Edit -> Find and Replace menu item.

Extract Function

RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any "free" variables within the selection (objects that are referenced but not created within the selection) are converted into function arguments:

Comment/Uncomment

You can comment and uncomment entire selections of code using the Edit -> Comment/Uncomment Lines menu item (you can also do this using the Ctrl+/ keyboard shortcut):

Indentation

As you write R code in RStudio it is automatically indented according to the current indentation options (see Customizing RStudio). R code is also re-indented:

  1. Whenever new code is pasted into a source document.
  2. When the Reindent Lines command (pictured above) is invoked.

Note that RStudio automatic indentation is R syntax-aware and is therefore only used on source files containing R code.

Executing Code

RStudio supports the direct execution of code from within the source editor (the executed commands are inserted into the console where their output also appears).

Executing a Single Line

To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button):

After executing the line of code, RStudio automatically advances the cursor to the next line. This enables you to single-step through a sequence of lines.

Executing Multiple Lines

There are three ways to execute multiple lines from within the editor:

  • Select the lines and press the Ctrl+Enter key (or use the Run toolbar button); or
  • After executing a selection of code, use the Re-Run Previous Region command (or its associated toolbar button) to run the same selection again. Note that changes to the selection including additional, removal, and modification of lines will be reflected in this subsequent run of the selection.
  • To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).

The difference between running lines from a selection and invoking Source is that when running a selection all lines are inserted directly into the console whereas for Source the file is saved to a temporary location and then sourced into the console from there (thereby creating less clutter in the console).

Source on Save

When editing re-usable functions (as opposed to freestanding lines of R) you may wish to set the Source on Save option for the document (available on the toolbar next to the Save icon). Enabling this option will cause the file to automatically be sourced into the global environment every time it is saved.

Setting Source on Save ensures that the copy of a function within the global environment is always in sync with its source, and also provides a good way to arrange for frequent syntax checking as you develop a function.

Keyboard Shortcuts

Beyond the keyboard shortcuts described above, there are a wide variety of other shortcuts available. Some of the more useful ones include:

  • Ctrl+Shift+N — New document
  • Ctrl+O — Open document
  • Ctrl+S — Save active document
  • Ctrl+1 — Move focus to the Source Editor
  • Ctrl+2 — Move focus to the Console

You can find a list of all shortcuts in the Keyboard Shortcuts article.

 

Related Topics