Getting comfortable with the RStudio environment

I have started using R relatively recently, but I see more and more people learning and using it around me. In this post, I offer little tips that might be helpful for beginners setting up their RStudio environment. It is small things that make your lives better, and if you don’t already know, here are some minor but useful tips can make your R experience much smoother and more efficient.

All settings are based on Windows OS, although the settings within RStudio are nearly identical between Windows and Mac.

Content:

  1. Working directory
  2. General options
  3. Code options
  4. Appearance
  5. Layout
  6. Shorcuts

1. Working directory

This is entirely my preference but on my computer (actually on a cloud), I have a folder where I keep all R-related files. The way I have organized it is like this:
[[[R analysis]]]
– [[01_projects]]
— [01_project1]
—- [00_shortcut to project1 folder]
—- 01_data.xlsx
—- 02_data_cleaning.r
— [02_project2]

– [[02_practice]]
– [[03_statistics]]

So the top folder is called [[[R analysis]]] in this example, and it includes other sub-folders: 01_projects, 02_practice, 03_statistics, etc.

Under [[01_projects]], I have separate folders for different projects. For example, I have a folder named [01_project1]. Here, I first make a shortcut (in Windows File Explorer) to another folder where I keep things like research articles and original data that do not need to be called into RStudio. So these files are not cluttering my R folder, but still linked for convenience. In [01_project1], I will have copies of data files that I will use in RStudio and the R scripts that I create.

This may not be perfect, but it works pretty well for me. It’s fairly organized and I don’t have to jump from one folder to another to find the file that I’m looking for, or fail to remember which folder I saved R scripts and things of that sort. I also don’t have to change the working directory so much when I start working because everything I use in RStudio will be under this folder. So if I start writing a script for project1, I would begin my script with:

setwd("01_projects/01_project1")

This way, even with time I know which folder I was working out of and I don’t have to scuffle through folders to open up files for this project.

2. General options

How do I set up the default working directory? From here and on, I will be talking about the [Global Options] which you can find under [Tools] from the menu bar.

When the [Global Options] window opens up, it will show the [General] tab. Here you can browse directory and set up your default working directory.

rstudio_2018-08-28_20-08-15

Other settings you might want to adjust is “Save workspace to .RData on exit”. I’ve set it to “never”, because then RStudio doesn’t ask whether I want to save the workspace (which I usually don’t) every time I shut down the program.

3. Code options

Let’s move on to the [Code] menu on the left-side.

rstudio_2018-08-28_20-09-00

I use a tab width of 4 spaces, but this is entirely up to you. Unless you’re a developer, it might not matter but I would probably make it at least 2, so that the tabbed lines are noticeable.

When you first install RStudio, “Soft-wrap R source files” is unchecked. Make sure to check this: you don’t have to scroll horizontally anymore when you have a really long line.

Snippets are worth discussing to enhance efficiency but I won’t talk about it in this post.

rstudio_2018-08-28_20-09-33

On top there are more tabs. Navigate to [Display]. I have checked the first two highlighting options. When you have a screen full of codes and get lost, this really helps to see where you are. I also have checked “Show whitespace characters” mostly because I have OCD. Spaces don’t really matter in R scripts but I do like to keep them consistent and tidy.

4. Appearance

rstudio_2018-08-28_20-10-01

Moving on to the [Appearance] menu, this is where you get to have some fun. First, you want to be able to read off the screen without getting a neck hump or losing your eyesight. I find the default font size way too small! Increase the zoom ratio according to your liking.

Explore the options for the font face and the theme as well. I prefer a dark one with less vibrant colors which are easy on my eyes (though I use a light theme on my laptop because of its glossy screen and reflections). Be aware that some types of fonts cause discrepancy between the cursor blink and the actual location of the cursor. I would stick with the widely used ones, such as Consolas or Courier, though you can try out some pretty cool coding fonts listed here and make it fun.

5. Pane Layout

In deciding the layout of the four panes, I mostly followed this post: perfect Rstudio layout.

I don’t use the connections pane so I moved it to the history pane (which is kept minimized in this layout). As with other settings, try out different things and find out what you like.

6. Shortcuts

If you use R regularly, shortcuts are very useful. I have customized frequently used ones and though the shortcuts probably make only milliseconds or a couple seconds of differences, I feel more efficient and less tired because they reduce movements. Navigate to [Keyboard Shortcuts] under [Tools] from the main menu. Here are some examples of shortcut customization:

rstudio_2018-08-28_20-12-56

By default, moving to the source (script) pane is [Ctrl+1] and moving to the console pane is [Ctrl+2]. Using this numbering, I have assigned shortcuts for each pane in the order they appear on my screen: Environment pane is [Ctrl+3], Files pane is [Ctrl+4], so on and so forth. When I’m writing on the source pane and need to refer to, for example, the help pane, I can do so without reaching to the mouse and I can easily recall the shortcut because they are numbered in an orderly fashion.

rstudio_2018-08-28_20-14-49

These are the ones I use less often but the default shortcut for inserting a new section (which inserts #——————————- with a text) is [Ctrl+Shift+R]. This doesn’t make sense to me. What does R stand for? I have no clue. Therefore, I have changed it to [Ctrl+Shift+S], which I will definitely remember each time I insert a new Section. I assigned [Ctrl+Shift+R] to restarting R session (originally [Ctrl+Shift+F10] — difficult to remember!).

The shortcuts I use most frequently are inserting an assignment operator (<-; yes, I’m old school) and inserting a pipe operator (%>%).

rstudio_2018-08-28_21-08-19

Because typing the pipe operator takes so much work (Shift+5, Shift+., Shift+5, plus spaces front and back for my liking), this shortcut makes my life so much easier. It even automatically inserts one space before and after the operator!

It is important to explore and adjust the settings with any software, to create a better working environment as you would do for your physical working environment. I hope these little tips make your R experience even more neat and slick.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s