R Studio Shortcuts and Tips

Estimated time:
time
min

<p style="text-align: center;"><em>Update: For more R Studio tips and tricks, see <a href="https://appsilon.com/r-studio-shortcuts-and-tips-part-2/">Part Two</a> of this series. You might also want to check out this new <a href="https://appsilon.com/video-tutorial-create-and-customize-a-simple-shiny-dashboard/">tutorial</a> on how to build and customize a simple Shiny Dashboard.</em></p> <h2 style="text-align: left;">How can you work faster in R Studio?</h2> <p style="text-align: justify;">In this article, I would like to share with you some of my favorite productivity features of R Studio along with their respective shortcuts. Additionally, I will provide information about some other tools and techniques that are useful. I've also prepared some visual examples for you to immediately see some of these shortcuts in action without the need to go into R Studio.</p> <blockquote> <p style="text-align: justify;">*Note: </span>Although the gifs have both PC and Mac shortcuts, I am using the 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 up the proper shortcut in R Studio with         <b>⌥ Option+Shift+K</b> (Alt+Shift+K)</p> </blockquote> Ready? Let’s dig in! <h3><strong>Moving Around</strong></h3> 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 <b>Ctrl </b>(in this case also Control on Mac) and a number corresponding to the desired pane. By adding a <b>Shift </b>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 (<b>Shift+Alt+t</b>). Preset windows: <i>Help</i>(<b>3</b>), <i>History</i>(<b>4</b>), <i>Plots</i>(<b>5</b>), or <i>Environment</i>(<b>8</b>). The two that you will be mostly jumping between frequently are <i>Source Editor</i> (<b>1</b>) and <i>Console</i> (<b>2</b>). Let’s now discuss how you can improve how you work in those.  <h3><b>Work Smarter, Not Harder</b></h3> 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. <h4><b>Code completion</b></h4> 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 <b>Global Options -&gt;  Code -&gt; Completion</b>, <b> </b>To fill in the suggested phrase you have to press either <b>Tab </b>or <b>Enter</b>, pressing <b>Ctrl + Space</b> 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. <h4><b>Paths</b></h4> If you need to type a path, you can use file path auto-complete which can be brought up by pressing auto-completion shortcut (<b>Tab </b>or <b>Ctrl + Spac</b><b>e</b>) 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 “../” <h3><b><img class="img wp-image-1846 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0232b3dff82577ed3431f_file_autocomplete.gif" alt="RStudio paths" width="562" height="276" /> </b></h3> <h3><b>Execute and Format Code</b></h3> Executing code in your scripts can be very easy with shortcuts. <ul><li><b>Ctrl + Enter</b> - Will run current line and jump to the next one, or run selected part without jumping further.</li><li><b>Alt + Enter</b> - 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.</li><li>There is also <b>Ctrl + Alt + R</b> to run whole script and</li><li><b>Ctrl + Alt + B/E</b> combinations to run it from <b>B</b>eginning to the current line and from the current line to the <b>E</b>nd.<img class="aligncenter wp-image-1884" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0232b8587215be0b2082b_blog_code_execution_optimized.gif" alt="RStudio Execute and Format code" width="607" height="383" /></li></ul> If you want to make your code look better quickly try using : <ul><li><b>Ctrl + I</b> to fix lines indentation</li><li><b>Ctrl + Shift + A </b>for complete reformatof the selected part of a code<img class="aligncenter wp-image-1869" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0232c7f68e07b0aa6893a_blog_reformat_optimized.gif" alt="RStudio Execute and Format code" width="481" height="299" /></li></ul> <blockquote>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.</blockquote> You may also benefit from remembering those super helpful shortcuts: Moving lines of code up and down is easily achieved with <b>Alt + Up/Down </b>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 <b>Ctrl + D</b> will delete current line/selection in no time. <h3><strong>Console History &amp; History Pane</strong></h3> 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 <i>History</i> pane(<b>4</b>) 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 (<b>Enter)</b> or source file (<b>Shift</b> + <b>Enter</b>). 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. <h3><strong>Dealing with Tabs</strong></h3> If you find yourself working on more than one tab in source editor, you might find it helpful to switch between them with <b>Ctrl+Tab</b> and <b>Ctrl+Shift+Tab</b> combinations. It will allow you to jump to the next and previous tab respectively, there is another way to do this with <b>Ctrl + F11</b>/<b> F12 </b>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. <img class="aligncenter wp-image-1849" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02331f7981e509d500a19_tabs-navigate-tab-history.gif-and-shift-tab.gif" alt="RStudio tabs" width="415" height="237" /> <p style="text-align: center;">Navigate tabs history back and forward</p> <img class="wp-image-1850" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0233240e2bdeffdb1d493_tabs_next_previous_first_last.gif" alt="RStudio tabs" width="422" height="241" /> <p style="text-align: center;">Jumping tabs</p> <img class="wp-image-1835" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02334a2bcff3465173676_jumpingtabs_merged.gif" alt="RStudio tabs" width="442" height="251" /> <p style="text-align: center;">Going through tabs back and forth</p> Closing a current tab is easy with<b> Ctrl + w</b>, 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: <i>Close All</i>  |<b> Ctrl + Shift + w</b> (+ <b>Alt</b> to keep currently open one), if you prefer to keep them you can search through your open tabs with <b>Ctrl + Shift + . </b>(be exact, no fuzzy matching here). This search can also be activated with ">>" icon on tabs bar. <img class="alignnone wp-image-1875" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b023367493b49978658cce_blog_tabs_search.gif" alt="RStudio tabs" width="649" height="297" /> Above shortcuts are also accessible from <i>File</i> dropdown menu - this can get in handy while using R studio browser session or simply if you forget them. <hr /> This is it for part one. I hope that you like what you just read and picked up something useful. For more, please read Part Two, where I 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.

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
r
tutorial
rstudio