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: 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: 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.
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.