blog posts

Learn 4 useful and interesting tricks in RStudio

The following are simple and useful commands and shortcuts that really help users’ productivity.

1- rstudioapi package

RStudio, When using Rstudio, the rstudioapi package gives you a lot of information about your session. The most useful is the script location. You can use it to automatically change the folder to where the file is stored locally.

The rstudioapi :: getActiveDocumentContext function gives you details about the file being edited in RStudio. Take a look at the following code:

my.d <- rstudioapi :: getActiveDocumentContext ()

print (my.d)

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

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

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

my.dir <- dirname (my.file.location)

print (my.dir)

## [1] “/ home / msperlin / Dropbox / 11-My Website / www.msperlin.com-blog / content / post”

So if you want to automatically change the job directory to where the script is stored, just write:

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

setwd (my.dir)

This is very useful and we use it in all our R scripts. If you copy the script to another folder, the directory will run smoothly. If you send the script to another person in a compressed folder, it can execute it unchanged because the work directory changes automatically.

Be aware that this only works on RStudio. If you run the code without an IDE, for example in a bash script, the rstudioapi package will not be available. In this case, you must specify the directory explicitly.

2. Dark theme for Rstudio

If you spend a lot of time in front of the computer, a dark theme is right for you. Before using it, the eyes always get tired after a long period of work. At the end of the day, using a tablet or even a cell phone will be uncomfortable. You can change the theme in Rstudio by going to “tools” -> “global options” -> “appearance”. There are many dark themes available. Choose the one you like the most.

We also recommend changing your operating system theme as you go along. We assure you that it is worth it in the long run!

3- (Autocomplete (your friend tab!)

One misconception about programming is that you have to remember a lot of names. This is far from the truth. You never need to remind yourself when using Rstudio! From function arguments to variable names and file names, everything can be searched by pressing the tab button on the keyboard. Using the tab becomes more useful when using naming conventions for functions and objects. For example, every data frame in our code starts with “df”, such as “df.prices”, “df.tickers” and so on. When looking for the name of a data frame, just type “df” and press the tab. The result is a list of object names.

The autocomplete function also works for function arguments, directories, and file locations and packages.

4- Naming the section with —-

In Rstudio, you can name a section in any R script using text clues. This section appears at the bottom left of the RStudio Editor screen. When you want to go to that part, just press the key. So you can organize your code with parts like this:

# Get data —-

## code here

# clean data —-

## code here

# report results —-

## code here