7 RStudio Tips and Tricks
NB: This guide assumes you are using RStudio v1.1 or later
7.1 Global Options
The Global Options
menu (found in the Tools
drop-down menu) allows you to customise the RStudio interface. This includes both aesthetic and functionality changes.
Appearance
menu: + Change your IDE colour schemes withRStudio themes
+ Change your text colour schemes withEditor themes
+ Change your font withEditor fonts
. Ligature-enabled fonts (none of the default fonts) let multi-character symbols like<-
be displayed as a single characterPane Layout
menu: + Lets you customise where each panel is displayed + Lets you move tabs between panes + Restricted to a 4x4 gridCode
menu: +Completion
tab:Use tab for multiline autocompletions
. Check this box to enable tab autocompletion of function calls over multiple lines +Diagnostics
tab:Check arguments to R function calls
. Check this box and R will show warning/error messages for missing arguments with no default values, and missing/additional commas/bracketsWarn if variable has no definition in scope
. Check this box and R will show warning messages if variable names supplied to a function aren’t defined anywhere. Picks up on spelling errors likeVariable1
instead ofvariable1
Warn if variable is defined but not used
. Check this box and R will show warning messages for defined variables that are never usedProvide R style guide diagnostics
. Check this box and R will show warning messages where you don’t adhere to Hadley Wickham’s style guide
7.2 Keyboard Shortcuts
RStudio has a slew of keyboard shortcuts to make life easier. Alt+Shift+K
brings up the menu, or they are available under the Tools
drop-down menu. Some useful ones:
Ctrl+<Number>
: Move focus to pane/tab corresponding to<Number>
. Most useful areCtrl+1
for Source andCtrl+2
for ConsoleCtrl+Shift+<Number>
: Maximise the pane/tab corresponding to<Number>
Ctrl+Ent
: Run selected line(s)Alt+Ent
: Run selected line(s) without moving cursorCtrl+Shift+P
: Re-run previous line(s). Lets you change values between runs.Ctrl+I
: Re-indent selected linesCtrl+Shift+C
: Comment/uncomment selected linesAlt+-
: Insert<-
Ctrl+Shift+M
: Insert%>%
Ctrl+L
: Clear consoleF1
: Bring up help file for selected function nameF2
: Bring up source file for selected function name
7.3 Code Folds
Code folds allow you to hide/show chunks of code in a script file to make navigating scripts easier. You might already be familiar with these since RStudio uses them automatically for braced regions like for
loops/if
statements/user-defined functions
. They show up as small triangles next to the line numbers.
You can manually add code folds with comment lines that include at least four trailing dashes -
, equal signs =
, or hashes #
. This will then let you hide all code between two lines with code folds. There is no set structure to code folds beyond this so you can work them into section headers to create a neat document. Folded sections show up in the Jump To
menu at the bottom of the script editor making it easy to navigate big documents. Example folds:
#----
#====
#####
# Section 1 ----
## Section 1a ----
# Load Data ===========================
#### Plotting ####
7.4 R Projects
R projects are a neat way of encompassing an entire set of work. Some of the benefits to this are:
- Relative filepaths. No more using
setwd()
orread.csv("C:/Users/Person/Documents/Folder/Folder2/Folder3/Data/File.csv")
! - When you re-open a project file the RStudio session is exactly as you ended it. The scripts you had open are still open, and
.RData
is loaded sothe variables are still stored in the environment (so you don’t have to re-run code). However, packages need to be reloaded and thePlots
pane is emptied. - Version control. R projects can be set up with version control software like
git
. RStudio has a two ways to interact withgit
: a Terminal (covered later) and a point and click GUI interface. We will probably cover this in a future Coding Club session.
7.5 Terminal
RStudio now has an in-built Terminal which allows access to the system shell within the RStudio IDE. For computers that don’t have a built-in Terminal (e.g. Windows), then you will need to install something like Git-Bash (might come with newer versions of RStudio). The Terminal can be used for things like version control with git
, remote logins, compiling python code, etc.