Productivity Tips

Become more productive working with R and RStudio.
Author

Michael McCarthy

Published

January 24, 2023

Overview

RStudio includes a variety of features to make you more productive working with R and RStudio. These features are covered in detail in the RStudio User Manual. Here I highlight some of the features I think are particularly helpful for working productively.

Customize RStudio

RStudio provides a number of preferences you can customize to change the look and feel of the IDE, provide certain defaults, and so forth. Go through them and find what works for you.

My own preferences look something like:

~/.config/rstudio/rstudio-prefs.json
{
    "show_margin": false,
    "save_files_before_build": true,
    "wrap_tab_navigation": false,
    "save_workspace": "never",
    "reuse_sessions_for_project_links": true,
    "jobs_tab_visibility": "shown",
    "rainbow_parentheses": true,
    "restore_source_documents": false,
    "restore_last_project": false,
    "load_workspace": false,
    "scroll_past_end_of_document": true,
    "syntax_color_console": true,
    "panes": {
        "quadrants": [
            "Source",
            "TabSet1",
            "Console",
            "TabSet2"
        ],
        "tabSet1": [
            "History",
            "Connections",
            "Packages",
            "Presentation"
        ],
        "tabSet2": [
            "Files",
            "Environment",
            "Plots",
            "Viewer",
            "Build",
            "VCS",
            "Help"
        ],
        "hiddenTabSet": [
            "Tutorial"
        ],
        "console_left_on_top": false,
        "console_right_on_top": true,
        "additional_source_columns": 0
    },
    "show_indent_guides": true,
    "highlight_r_function_calls": true,
    "auto_append_newline": true,
    "strip_trailing_whitespace": true,
    "code_completion_characters": 2,
    "graphics_backend": "ragg",
    "rmd_viewer_type": "pane",
    "show_help_tooltip_on_idle": true,
    "source_with_echo": true,
    "rmd_chunk_output_inline": false
}

Use the command palette

RStudio comes equipped with a command palette that gives instant, searchable access to all of RStudio’s commands. Bring it up with Command-Shift-P.

Use keyboard shortcuts

RStudio comes equipped with over 100 keyboard shortcuts to speed up your workflow, each of which you can modify to suit your preferences. You can even write your own custom keyboard shortcuts to execute RStudio application commands, editor commands, or user-defined R functions.

Common shortcuts

Description Command
Copy Ctrl+C
Paste Ctrl+V
Cut Ctrl+X
Save Ctrl+S
Undo Ctrl+Z
Redo Ctrl+Shift+Z

Editing shortcuts

Description Command
Indent (at beginning of line) Tab
Insert pipe operator Shift+Command+M
Insert code block Command+Option+I
Insert additional cursor on line above Ctrl+Option+Up
Insert additional cursor on line below Ctrl+Option+Down
Insert additional cursor at click position Command+Option+Click

Mac shortcuts

Description Command
Switch between open apps Command+Tab
Navigate app switcher (while holding Command) Left/Right

Write your own code snippets

RStudio supports text macros, called code snippets, useful for quickly inserting common snippets of code or text. If you find yourself writing the same boilerplate over and over again, it might be time to turn it into a code snippet.

For inspiration, here are code snippets written by others in the R community:

Manage R with .Rprofile and .Renviron

The .Rprofile and .Renviron dotfiles can be used to modify the startup behaviour of an R session on a global or per-project basis:

  • .Rprofile contains R code to be run when R starts up
  • .Renviron contains environment variables to be set when R starts up

The behaviour of these files is described in more detail in the R Startup chapter in What They Forgot to Teach You About R by Jenny Bryan and Jim Hester.

Reproducibility

As Jenny Bryan and Jim Hester write in What They Forgot to Teach You About R: “A good rule of thumb is you should only put things in your .Rprofile that you run interactively in the R terminal. If it ever appears in a R script or R Markdown file it should not be in your .Rprofile.”

For inspiration, here are .Rprofiles written by others in the R community:

Note that some of them contain code that could break the rule of thumb for reproducibility.