R Studio Shortcuts and Tips
This article is originally published at https://appsilon.com
How can you work faster in R Studio? Do you really want to know?
In this article, I would like to share with you some of my favorite productivity features of R Studio along with their respective shortcuts. As well I will provide information about some other tools and techniques that are useful. I also prepared some visual incentives for you to immediately see some of them in action without the need to go into R Studio.
*Note: Although the gifs have both, PC on top of Mac shortcuts, I am using former in the text, if you are a Mac user most of the shortcuts fall under this dependence: Ctrl == ⌘ Command && Alt == ⌥ Option , but keep in mind that in some cases Ctrl will also be a Control key on Mac which can be confusing. You can always look it up in R Studio with ⌥ Option+Shift+K (Alt+Shift+K)
Ready? Let’s dig in!
Depending on your work you will be using at least few Rstudio panes on a regular basis. Learning how to change focus between those utilized the most in a quick manner, and without using your pointing device, is a crucial skill for speeding up your workflow. It is achieved by pressing Ctrl (in this case also Control on Mac) and a number corresponding to the desired pane. By adding a Shift to the combination you can also toggle maximize pane for the one that you are switching to at the same time, very handy if you need a broader perspective. The only pane with a different access shortcut is the terminal (Shift+Alt+t). Preset windows: Help(3), History(4), Plots(5), or Environment(8). The two that you will be mostly jumping between frequently are Source Editor (1) and Console (2). Let’s now discuss how you can improve how you work in those.
Work smart, not hard
Usually, the first thing you have to do when you start working is creating some code. It is crucial to be aware that there are some features that can make it both easier and faster. Even basic tricks can have a great impact after you master using them, especially when combined together.
Suggestion list will pop up as you type or can be accessed manually by either pressing Tab or Ctrl + Space. You can adjust those settings in Global Options -> Code -> Completion, To fill in the suggested phrase you have to press either Tab or Enter, pressing Ctrl + Space with auto-completion list open will close it. You can navigate through the suggestion list with arrows or just hover over the item before filling in.
If the list is too long try providing more letters to narrow it down. Beside auto-completing functions and variables, you can also insert snippets. We will get back to discuss those later. It’s good to be aware that auto-completion in R, as well as some search fields, supports fuzzy matching which means that you don’t really have to type all the letters, you can skip any of them as long as those typed are in order and identify what are You looking for. It is especially useful for long function names that you use often, mastering this will allow you to type code much faster. Take note that for fuzzy matching to work with auto-completion, suggestion popup must be already active. In case it doesn’t behave as you would expect, try tweaking it in code completion options.
If you need to type a path, you can use file path auto-complete which can be brought up by pressing auto-completion shortcut (Tab or Ctrl + Space) from a pair of double or single quotes.
By default it starts in your working directory, you can navigate from the root location like in shell console starting with “/”, or step up levels in the directory tree by stacking “../”
Execute and Format code
Executing code in your scripts can be very easy with shortcuts.
- Ctrl + Enter – Will run current line and jump to the next one, or run selected part without jumping further.
- Alt + Enter – Allows running code without moving the cursor to the next line if you want to run one line of code multiple times without selecting it.
- There is also Ctrl + Alt + R to run whole script and
- Ctrl + Alt + B/E combinations to run it from Beginning to the current line and from the current line to the End.
If you want to make your code look better quickly try using :
- Ctrl + I to fix lines indentation
- Ctrl + Shift + A for complete reformat of the selected part of a code
If you are not happy with the outcome of those you can always undo the changes. If you look for a more flexible solution for styling check out the styler package.
You may also benefit from remembering those super helpful shortcuts:
Moving lines of code up and down is easily achieved with Alt + Up/Down combination, no need to cut and paste. You can move a single active line that way, or even whole selection. If you need to remove something Ctrl + D will delete current line/selection in no time.
Console History & History pane
Everything that you passed to the console doesn’t have to be typed again. Accessing previously executed lines is as easy as navigating with the up arrow and down arrows to cycle between them in chronological order. If you want more visual feedback u can press Ctrl + Up arrow to get a list of last commands. If you combine it with typing in a part of the searched phrase you can narrow it down and easily find even complicated commands that are buried deep in the history. It will also override autocomplete popup if its active. Note: searching console history doesn’t support fuzzy matching so you have to be exact. If you want to clear your console use Ctrl+L, command history will be preserved.
There is also History pane(4) which stores executed commands. It allows searching, easily select the ones you need (pick range with shift or gather individual positions with ctrl). Then easily insert them back into the console (Enter) or source file (Shift + Enter). The latter helps you avoid copying multiple commands from console to source manually which is troublesome due to line signs “>” that get copied along and would otherwise have to be removed.
Dealing with Tabs
If you find yourself working on more than one tab in source editor, you might find it helpful to switch between them with Ctrl+Tab and Ctrl+Shift+Tab combinations. It will allow you to jump to the next and previous tab respectively, there is another way to do this with Ctrl + F11/ F12 if it suits you better. It is also possible to jump to the first or last one by adding Shift to those. Last option that is quite interesting is navigating through tabs in the order they were accessed with Ctrl + F9/F10.
Navigate tabs history back and forward
Going through tabs back and forth
Closing a current tab is easy with Ctrl + w, it is a much better choice than using those small “x” buttons on the right side of your tabs.
If you get to the point where you have a huge amount of tabs open u can either:
Close All | Ctrl + Shift + w (+ Alt to keep currently open one),
if you prefer to keep them you can search through your open tabs with Ctrl + Shift + . (be exact, no fuzzy matching here). This search can also be activated with “>>” icon on tabs bar.
Above shortcuts are also accessible from File dropdown menu – this can get in handy while using R studio browser session or simply if you forget them.
This is it for part one. I hope that you like what you just read and picked up something useful. Stay tuned for part 2 of this article with more advanced tips! I will cover some techniques for generating parts of code quicker as well as more code manipulation tricks combined with different ways to search through your projects.
To be continued…
Please visit source website for post related comments.