My favourite RStudio tricks

My first post on this blog was on the 31st March 2019, almost exactly a year ago. My aim was to post at least once a month. Looking back over the year, I’ve posted 20 times (not including this) across topics like web scraping, regular expressions and creating Shiny apps, as well as chiming in with my thoughts and experiences around becoming a data scientist without a traditional STEM academic background. My most popular post has been my reasons for loving the R package data.table - which is great because I still love it! I’m definitely going to write some more posts about using it over the next year. I’m also hoping to continue some of the projects I started in my first year to demonstrate some machine learning techniques - particularly looking at sentiment analysis as well as language in Shakespeare’s plays.

I’m not going to cover any of that now, though. Recently I’ve been pulling together some R training to deliver remotely for my team and others in my organisation. It made me think about how much I’ve learnt using RStudio that just makes my life so much easier than it was when I started out. So this is a small collection of keyboard shortcuts that I’ve picked up. They’re not need-to-know but they help!

Tidy up code

Maybe your code looks messy and while it doesn’t seem mission critical to tidy it up, it’s actually really taxing reading it and your code collaborators are despairing. There are a number of quick ways to deal with this.

If your indentation is all over the place, use CTRL+I or CMD+I (that’s I for igloo (or indentation I guess)). You can do this line by line or highlight a section.

Gif showing lines being neatly indented

But that might not be enough. Use CTRL+Shift+A (CMD+Shift+A) while your code is highlighted to reformat it completely.

Gif showing lines being reformatted

Switch things around

Wait, you’ve got some stuff the wrong way round. Sure, you could cut and paste one of the lines, or even retype it…but you could also hold down Alt (or Option) while pressing the up or down arrow to switch a line with the line above or below it.

Gif showing two lines switching place in RStudio

Or if you frequently have typos, you can use CTRL+T (CMD+T) to transpose letters…

Gif showing two letters switching place in RStudio

Cursor across multiple rows

Yes, it’s true that you shouldn’t be in a position that your code is duplicated a lot. But sometimes, you have a few lines of code where you need to change the same thing and it’s a pain addressing each line individually, not to mention error-prone. If that’s the case, you can hold down Alt (or Option on a Mac) while dragging your mouse to expand your cursor over several lines, allowing you to edit several lines at the same time.

Gif showing three rows being edited in RStudio

This was the one that felt most like magic to me when I learnt it!


For more ideas, check out the RStudio cheat sheet, or for a list of keyboard shortcuts, press Alt+Shift+K (or Option+Shift+K) while in RStudio.

comments powered by Disqus