May 2, 2023 2:09 PM
video

Do more with R: RStudio addins and keyboard shortcuts

Speed up your R programming workflow with RStudio addins and customized keyboard shortcuts

Thinkstock

If you want to speed up your R programming workflow, RStudio addins and custom keyboard shortcuts are definitely worth a look. A number of R packages offer add-ins as well as functions, and some are even add-ins only.

One of my earliest favorite add-ins, Bare Combine, offers an elegant way to create R character vectors from plain text.

Let’s say I’ve got text with the names of … anything that I’d like to turn into an R vector. Adding quotation marks around each item will quickly get annoying if there are more than a couple of entries. Hence the problem that Bare Combine aims to solve.

You install addins the same way you install packages with install.packages() if they’re on CRAN, or whatever package and function you use to install from GitHub, Bitbucket and elsewhere if the version you want is somewhere else. 

You can find add-ins in RStudio’s Addins menu above the upper-left scripts pane.

Sharon Machlis/IDG

Addins menu in RStudio

You can also search for add-ins by using the RStudio command palette, accessible with the keyboard shortcut ctrl-shift-P, or in the Tools > Addins > Browse Addins menu.

Bare Combine is part of Bob Rudis’s hrbraddins on Bitbucket. You can install the full package with remotes::install_bitbucket("hrbrmstr/hrbraddins"). A number of add-ins should now be callable from RStudio.

If you paste a comma-separated list of words into RStudio such as

Chrome, Firefox, Safari, Internet Explorer, Edge, Opera

and then highlight it and click on the Bare Combine addin form the menu, the result will be code for a properly formatted R vector:

c("Chrome", "Firefox", "Safari", "Internet Explorer", "Edge", "Opera")

It even works if the items aren’t comma-separated but are each on a separate line.

It’s somewhat cumbersome and time-consuming to scroll through a list of addins, though. Fortunately, you can create a keyboard shortcut for any addin choosing Tools > Modify Keyboard Shortcuts in RStudio.

You first see all the built-in possibilities for shortcuts. If you keep scrolling, you eventually see your addins. Or, much easier, you can use the search box to look for your addin. Click into the empty field under the Shortcut column and type your key combination. Here, I chose Ctrl-Alt-C (and next clicked Apply):

Sharon Machlis/IDG

Adding a custom keyboard shortcut in RStudio

Now if I select the text and press Ctrl-Alt-C, my addin works—without having to scroll through my entire addin menu.

Dean Attali has a list of RStudio addins on GitHub.

It’s worth taking a look at the list; there might be a couple of addins you didn’t know about that will help you speed up your R workflow. Another of my favorites: datapasta by Miles McBain, which lets you copy data into your clipboard and then paste it into RStudio as a data frame.

R tip: Addins and keyboard shortcuts