Navigating Code
RStudio includes a number of features to enable rapid navigation through 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 recursively searching every file for each occurrence of a given string. To display the Find in Files dialog box, go to the RStudio menu: Edit > Find in Files or use the keyboard short: Ctrl+Shift+F.
Find in Files also allows for further customization of the search with regular expressions and filters for specific file types. 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.
Go to File/Function
If the name of the source file or function is known, the “Go to File/Function” search box on the main RStudio toolbar allows for quick navigation to it:
The Go to File/Function feature works from a constantly updated index of your source code. The specific source files to index are determined as follows:
If an RStudio Project is active, then all R source files within the project directory are indexed.
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+. which will open up a floating Go to File/Function dialog box.
Go to Function Definition
Since an index of the current Project’s source code (as described above) is maintained, RStudio can also help with quick navigation to the definition of any R function. To navigate a function definition, place the cursor on the function name (it doesn’t have to be fully selected), and then from the RStudio menu choose the Code > Go to Function Definition command.
From either the source editor or the console, the following shortcuts provide access to the function definition:
Using the F2 keyboard shortcut
Using Ctrl+Click (Cmd+Click on Mac) with the mouse
It is possible to navigate to both user-defined own functions (defined in R source files) and 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.
Going back and forward
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). This is used to quickly return to the previous editing location when navigating through code, which is especially useful when navigating through a sequence of function calls.
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
Back shortcut: Ctrl+F9 (Cmd+F9 on Mac)
Forward shortcut: Ctrl+F10 (Cmd+F10 on Mac)