Navigating Code


Navigating Code

RStudio includes a number of features to enable rapid navigation through R source code. Learning these features can be a major productivity enhancement and can also assist in gaining a better understanding of source code written by others on your team or within external packages.

Find in Files

Given a specific directory, Find in Files allows you to recursively search every file for each occurrence of a given string. To display the Find in Files dialog box, go to the edit menu and select Find in Files:

You can further customize your search with regular expressions and filters for specific file types. Your search results will display in the pane adjacent to the console. For each matching string, the following will be displayed:

  • The file path
  • The line number of each occurrence
  • The matching string (highlighted), in the context of the current line

Double clicking the line, will open the file in the RStudio source editor. The keyboard shortcut for Find in Files is Ctrl+Shift+F

Go to File/Function

If you know the name of the source file or function that you want to edit next you can quickly navigate to it using the Go to File/Function search box on the main RStudio toolbar:

The Go to File/Function feature works off a constantly updated index of your source code. The specific source files to index are determined as follows:

  1. If an RStudio Project is active then all R source files within the project directory are indexed.
  2. If a Project is not active then all currently open R source files are indexed.

The keyboard shortcut for Go to File/Function is Ctrl+.

Go to Function Definition

Since an index of your R source code (as described above) is maintained, RStudio can also help you quickly navigate to definition of any R function. To navigate a function definition you place your cursor on the function name (it doesn't have to be fully selected) and then choose the Go to Function Definition command:

You can also access go to function definition:

  • Using the F2 keyboard shortcut
  • Using Ctrl+Click with the mouse
  • From either the source editor or the console

It is possible to navigate to both your own functions (defined in R source files) as well as any other function defined within an R package. For functions defined within packages the code is displayed in a special Source Viewer pane which is read-only.

Navigating within a Source File

There are several ways to navigate through the code within a source file:

  • The go to function definition feature described above also works for functions within the current source file (giving precedence to functions defined within the calling scope).
  • The Jump to Line command can be used to go to any line in the current file. Keyboard shortcut: Alt+Shift+G (Mac: Option+Shift+Cmd+G).
  • You can also use the function menu (shown below) to quickly jump to functions by name.

Note that the function menu currently supports standard R functions however does not yet support S4 methods.

Going Back and Forward

When navigating through code (especially when navigating through a sequence of function calls) you often want to quickly return to the previous editing location. RStudio maintains a list of active navigations and allows you to traverse them using the Back and Forward commands (available on the Edit menu and on the far left of the source editor toolbar).

Back and Forward apply to the following navigation gestures:

  • Opening a document (or switching tabs)
  • Going to a function definition
  • Jumping to a line
  • Jumping to a function using the function menu

You can also invoke Back and Forward using the Ctrl+F9/Ctrl+F10 (Cmd+F9/Cmd+F10 on the Mac) keyboard shortcuts.

Have more questions? Submit a request


Powered by Zendesk