RStudio is the best IDE so far to use R. Its power is not only coming from the great developers behind it but also it’s an open source product which can be developed and maintained by contributors. I compile a list of some keyboard shortcuts, packages to increase performance and add-ins to boost the productivity that I use in my everyday tasks.

Chapters

Packages

The packages mentioned here just indicates how they can improve the working, not the actual work.

microbenchmark

You can measure the performance of computing with microbenchmark which computes each argument 100 times to give results on average. Further, the benchmark can be visualized via autoplot() function dependent upon ggplot2 package. The alternative of this package in base R is system.time() function.

install.packages("microbenchmark")

profvis

Profvis is a nice package/tool to understand how R spends its time. I will start trying to use to optimize my Shiny applications.

install.packages("profvis")

lintr

Lintr package gives you nice ‘linting’ which are gently reminders to have command over your own styling and syntax. Linting suggestions will not not improve performance or speed at all; however, the readability will boost up as the R language should be written for humans at first, and then computers. Therefore, run lintr::lint("file.R") and/or lintr::lint_package().

install.packages("lintr")

Add-ins

RStudio add-ins is just its extension mechanism to customize your working environment. Your productivity can significantly be increased together with lots of useful add-ins which you previously doing by hand spending hard time to fix the things to look it correct. I recently discovered the add-ins and they just great.

Most of the add-ins requires devtools package.

install.packages("devtools")

I here curate the addins I like. You can click here if you prefer to see a more comprehensive list of addins.

More little boxes

Although its name indicates that the boxes are small, for me the comment blocks are too big, which I found myself always scrolling up or down. The add-in is just great, quite helpful to differentiate the code sections visually; however, it needs to be tailored a bit for my needs. The original package is here.

devtools::install_github("strboul/morelittleboxes")

As I use this add-in often, I have assigned a keyboard shortcut for it (Windows/Linux Ctrl + Alt + H or Mac Cmd + Option + H). You can customize keyboard shortcuts in RStudio via Tools > Modify Keyboard Shortcuts....

datapasta

I have really found datapasta a huge time saver. You can see the GIF above which I recorded in my working environment. It formats, add quotes, and concatenate the lists and vectors. My favorite things it does: 1) Paste (as data.frame and vector) 2) Fiddle Selection 3) Toggle Vector Quotes. It’s dependent upon ‘readr’, ‘clipr’ packages, and uses RStudio’s API (possible to use without it). It’s on CRAN, so installation is easy as install.packages("datapasta").

However, I did not find useful yet to assign any keyboard shortcut to any of the provided functions.

ggThemeAssist

A Shiny based GUI theme assistant for ggplot2 can significantly help your plotting work.

devtools::install_github("calligross/ggthemeassist")

remedy

A RMarkdown addin which is useful e.g. when especially constructing tables.

devtools::install_github("ThinkR-open/remedy")

Keyboard-shortcuts

I will list the shortcuts that I usually exercise. For a comprehensive list, you can always visit Tools > Keyboard Shortcuts Help.

The updated link: https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts

General

Command Windows/Linux Mac Note
Keyboard shortcut reference Alt + Shift + K Option + Shift + K a shortcut to rule them all
New R file Ctrl + Shift + N Cmd + Shift + N  
Function help F1 F1 the most useful
Function source code F2 F2  
Search the history from console Ctrl + ↑ Cmd + ↑ powered by .Rhistory
Move cursor to editor Ctrl + 1 Ctrl + 1 moving without touching mouse
Move cursor to console Ctrl + 2 Ctrl + 2 (not touching mouse!)
Clear console Ctrl + L Cmd + L  
Open tab search Ctrl + Shift + . Ctrl + Shift + .  
Previous tab Ctrl + F11 Ctrl + F11  
Next tab Ctrl + F12 Ctrl + F12  
First tab Ctrl + Shift + F11 Ctrl + Shift + F11  
Last tab Ctrl + Shift + F12 Ctrl + Shift + F12  
Delete line Ctrl + D Cmd + D  
Autocompletion Tab Tab also Ctrl + Space
Assignment operator Alt + - Option + - ’<-‘
Pipe operator Ctrl + Shift + M Cmd + Shift + M ’%>%’ from magrittr
Add header Ctrl + Shift + R Cmd + Shift + R # Header - - - (…)
More little boxes Ctrl + Alt + H Cmd + Option + H customized add-in (see above)
Goto function/file Ctrl + . Cmd + . works in the project
Find in files Ctrl + Shift + F Cmd + Shift + F search in all files within scope
Comment/Un-comment line Ctrl + Shift + C Cmd + Shift + C when lines selected
Re-indent lines Ctrl + I Cmd + I  
Reformat code Ctrl + Shift + A Cmd + Shift + A readable code, sometimes odd
Focus to plot Ctrl + Shift + 6 Ctrl + Shift + 6 return to the main view with 0
Restart R session Ctrl + Shift + F10 Cmd + Shift + F10 ‘Session > Restart R’
       

RMarkdown (.Rmd files)

Command Windows/Linux Mac Note
Add a new chunk Ctrl + Alt + I Cmd + Option + I adds R chunk by default
Run all previous chunks Ctrl + Alt + P Cmd + Option + P  
       

Package Development

Command Windows/Linux Mac Note
Build and Reload Ctrl+Shift+B Cmd+Shift+B  
Load All (devtools) Ctrl+Shift+L Cmd+Shift+L  
Test Package (Desktop) Ctrl+Shift+T Cmd+Shift+T  
Test Package (Web) Ctrl+Alt+F7 Cmd+Alt+F7  
Check Package Ctrl+Shift+E Cmd+Shift+E  
       

Seeing the customized keyboard shortcuts from the add-ins which are stored in. If you are in any .nix based systems:

cd ~/.R/rstudio/keybindings
cat addins.json

If you modified any other shortcuts, also check editor_bindings.json and rstudio_bindings.json files.

Modify RStudio theme with CSS

This part is quite interesting cause it will give you much more control then you need in the console. The technique is originally adapted from this post, which has more detailed explanation. At the same time I update this post, I have R Studio version 1.1.447 (2018-04-18) and R version 3.4.3 (2017-11-30).

For a long time, I have been distracted from the line numbers placed on the right side of the main editor. I have always wish them to have more pale color that my eyes can differentiate it from the actual integer numbers in code file especially while I swiftly scroll up and down. Since RStudio uses JavaScript written Ace code editor, We see down that how modifying the theme is actually possible (without creating a new theme) by modularizing the code with your favorite browser’s console.

Before and after... Nice view!

Steps to do it:

  1. Choose an area you want to change
  2. Do a right click and select ‘Inspect Element
  3. The console layout like a browser will be opened up
  4. Scroll down a bit, find and click on the <link> tag referencing a CSS file
  5. Play with the CSS selectors in real time
  6. After you are happy with changes, go to the actual path and overwrite the document
    • (do not forget to take a backup of the original file)
  7. Then your changes are permanent

NOTE: I will keep this post updated as long as new shortcuts, tools and add-ins come along.