Some Useful Tricks in RStudio

November 2, 2018

(This article was first published on R on msperlin, and kindly contributed to R-bloggers)

I’ve been using Rstudio for a long time and I got some tricks to share. These are simple and useful commands and shortcuts that really help the productivity of my students. If you got a suggestion of trick, use the comment section and I’ll add it in this post.

Package rstudioapi

When using Rstudio, package rstudioapi gives you lots of information about your session. The most useful one is the script location. You can use it to automatically change the working folder to where you have the file locally saved.

Function rstudioapi::getActiveDocumentContext gives you details about the file being currently edited in RStudio. Have a look:

my.d <- rstudioapi::getActiveDocumentContext()
## Document Context: 
## - id:        'FA202F79'
## - path:      '~/Dropbox/11-My Website/'
## - contents:  <69 rows>
## Document Selection:
## - [24, 6] -- [24, 6]: ''

You can see that the file location is available in path. Let’s grab it:

my.file.location <- rstudioapi::getActiveDocumentContext()$path

Now, if we want the name of the directory, just call dirname(my.file.location):

my.dir <- dirname(my.file.location)
## [1] "/home/msperlin/Dropbox/11-My Website/"

So, if you want to change the working directory automatically to where the script is locally saved, just write:

my.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

This is very practical and I use it in all of my R scripts. If you copy the script to another folder, it will run without any directory problem. If you send the script to someone else within a zipped folder, he/she can run it without modifications as the working directory will change automatically.

Be aware, however, this only works in RStudio. If you run the code without the IDE, in a bash script for example, package rstudioapi will not be available. In this case, you’ll need to set the directory explicitly.

Dark theme for Rstudio

A dark theme is a productivity life-changer if you spend a lot of time in front of a computer. Before I used it, my eyes were always strained after a long period of work. By the end of the day, using tablets or even my phone was disconforting. You can change the theme in Rstudio by going into “tools” -> “global options” -> “appearance”. There are many dark themes available. Pick one that pleases you the most. See the difference between a white and dark theme next:

Going further, I also advise to change the theme of you operating system. I can assure you that, in the long run, it is worth it!

Autocomplete (tab) is your friend!

A commom misconception about programming is that you must memorize lot of names. This is far from the truth. You never need to memorize anything when using Rstudio! From function arguments to variable names and names of files, everything can be searched by pressing the tab key on your keyboard. When using naming conventions for functions and objects, this becomes even more useful. For example, every dataframe in my code starts with “df”, like in “df.prices”, “df.tickers” and so on. When I’m looking for the name of a dataframe, I just write “df.” and press tab. The result is a list of object names.

The autocomplete function also works for function arguments, directory and file locations and packages. In my book I have a whole section about it. Check it out.

Autocomplete for files

Autocomplete for files

Autocomplete for packages

Autocomplete for packages

Section naming with ----

You can name a section in any R script in Rstudio using the textual clue ----. This section will show up in the bottom left of the RStudio editor screen. When you want to jump to that section, just press the key. So, you can organized your code with sections like this:

# Get data ----

## code here

# clean data ----

## code here

# report results ----

## code here

To leave a comment for the author, please follow the link and comment on their blog: R on msperlin. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)