FeaturedIT topics

Do more with R: RStudio addins and keyboard shortcuts

If you want to speed up your R programming workflow, RStudio addins and custom keyboard shortcuts are definitely worth a look. One of my favorites, bare combine, offers an elegant way to create R character vectors from plain text. Here’s how.

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 if they’re on GitHub. Bare combine is part of Bob Rudis’s hrbraddins on GitHub, so you can install it with remotes::install_github("hrbrmstr/hrbraddins").

Instead of installing a package that adds new functions, the hrbraddins package gives you several RStudio addins. You’ll find them in RStudio’s Addins menu above the upper-left scripts pane.

Sharon Machlis/IDG

Addins menu in 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):

RStudio custom keyboard shortcutSharon 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.

How cool is that?

Dean Attali keeps a fairly comprehensive 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.

Related Articles

Back to top button