R Studio: Paul’s tips & tricks

I’ve been using R Studio for quite a while now, and have consequently become pretty familiar (and yes, fond) of this most excellent R IDE. Over that time, I’ve found a handful of super useful tweaks and options that has made working in R Studio a true joy. At this point, if you’re asking yourself, “what the heck is R?” than the rest of this post will probably mean nothing to you; for everyone else, read on! Though I’ve only worked on Windows and Linux machines, the following tips and tricks should work just as well for the Mac version of R Studio. So without further ado, I present to you:

Paul’s R Studio Tips & Tricks

1. Tab to auto-complete
The auto-complete feature in R Studio is pretty darn good. To use it, simply begin typing an R command and then hit the tab key: R Studio will fill out the rest of the command for you. Give it a try; type “tap” into the command line and hit the tab key. Cool, eh? What’s more, not only will R Studio auto-complete R commands and functions, but it will also auto-complete the names of data objects (e.g. data.frames and vectors) that you’ve created. Finally (and this is really neat), R Studio will even auto-complete file paths for you. Money.

2. Close parenthesis and quotation highlighting
One of the most frequent mistakes in R coding, is forgetting to close a parenthetical or a quotation. Happily, R Studio makes this mistake a touch less likely by highlighting the opening parenthesis or quotation mark when the cursor is placed directly following the closing parenthesis or quotation mark. To illustrate: closing_ex 3. Disable automatic insertion of closing parenthesis/quotation mark
By default, R Studio will automatically insert a closing parenthesis or quotation mark when you begin typing an opening parenthesis or quotation mark. Personally, I find this a terrible feature: the number of incorrect-parenthesis errors that it caused me to make are too numerous to count. So, I turned it off. If, like me, you tend to jump around a lot in your code, you may consider doing the same. Here’s how:


4. Enable code wrapping
If, like me, you like to work with multiple windows up at once, you will undoubtedly run into the issue of not being able to see your code without scrolling sideways. While the best solution is to simply get in the habit of chopping your code up into shorter, more legible segment, a fair alternative is to have R Studio “soft wrap” it for you. All this means is that any part of your R script that would usually fall off the side of your screen is, instead, continued on the following line. Give it a try:


5. Installing packages from menu
A convenient way to install new R packages is to simply use the built-in menu option in R Studio: packageinstall 6. Enabling packages from side-menu
A convenient way to load up R packages in R Studio is to simply check/un-check the relevant boxes in the ‘Packages’ side-menu. This menu also allows you to quickly take inventory of all your active packages, which may be useful in situations where you need to free up some memory or debug a package incompatibility.


7. Copy+paste from plot view
It is possible to copy and paste a plot image directly from the zoom’d plot view without writing it to a .pdf or .png file first. Simply “Zoom” an active plot from the side-menu and right-click the plot view and select the “Copy Image” command. Super convenient. packageinstall

8. Set working directory from side-menu
If typing out the full ‘setwd()’ command is just too much of a chore for you, then you’re in luck; you can set your working directly via the ‘Files’ side-menu by first navigating to your desired file location and then simply selecting the ‘Set As Working Directory’ option from the ‘More’ drop-down menu (click the gears icon):


9. Quick Commenting
Last, but certainly not least, you can comment out your code in R Studio by highlighting the offensive lines and simply hitting ctrl+shift+c. To un-comment, simply re-highlight and hit ctrl+shift+c again. Simple, and effective.


11 thoughts on “R Studio: Paul’s tips & tricks

  1. Pingback: learning R: practical advice | I'm Paul Chung.

  2. David C

    Thanks for the tips, Paul. The automatic insertion of parenthesis/quotation marks was annoying the hell out of me. Thanks.

  3. Dan

    Thanks for the tip on turning off the autocomplete of quotes. That is the only annoying thing I have found with RStudio!

  4. Julian R

    Hi Paul –
    Hope you are well in sunny Calif… and thanks for sharing your fascinating statistical work using RStudio. This is intended as a helpful, courteous comment as I was looking for info on “R-Studio” … a low level German data recovery tool! It seems you have been inserting your own spurious hyphen in descriptions of work on your page, hence folk like me are being erroneously directed here (and I’ll wager, with some leaving less polite comments! lol)
    Hope you can rectify matters, greetings from London,
    best, J

  5. Shivi

    Hi, Is there a way similar to SAS Enterprise Guide where in don’t have to type the entire code. Lets say I need to calculate mean for a group so will time pr….. for proc and SAS will automatically highlight the entire word thus making it really easy for me. Is there any similar functionality in R?

  6. Glenn Meyers

    control shift c is now only commenting the first line in the highlighted selection – and it won’t uncomment, it just adds another “#” in the first line. It always worked before. How did this happen and how do I fix?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s