Editing and Executing Code in the RStudio IDE


Editing and Executing Code


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

The RStudio IDE 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. Working in the source editor makes it much easier to reproduce sequences of commands and to package commands 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:

  • R scripts
  • R Markdown documents
  • Sweave documents
  • HTML files
  • TeX documents
  • and many more

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

To open an existing file you use either the File -> Open File... menu or the Recent Files 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 in 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... 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:


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

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

Need Help?

RStudio Pro customers may open a discussion with RStudio Support at any time.

You may also ask for help from R and RStudio users on community.rstudio.com. Be sure to include a reproducible example of your issue. Click here to start a new community discussion.