fbpx
Tips and Tricks in RStudio and R Markdown Tips and Tricks in RStudio and R Markdown
If you have the chance to work with an experienced programmer, you may be amazed by how fast she can write... Tips and Tricks in RStudio and R Markdown

If you have the chance to work with an experienced programmer, you may be amazed by how fast she can write code. In this article, I share some tips and shortcuts you can use in RStudio and R Markdown to speed up the writing of your code.

[Related Article: Image Compression In 10 Lines of R Code]

Run code

You most probably already know this shortcut but I still mention it for new R users. From your script you can run a chunk of code with:

command + Enter on Mac
Ctrl + Enter on Windows

Insert a comment in R and R Markdown

To insert a comment:

command + Shift + C on Mac
Ctrl + Shift + C on Windows

This shortcut can be used both for:

  • R code when you want to comment your code. It will add a # at the beginning of the line
  • for text in R Markdown. It will add <!-- and --> around the text

Note that if you want to comment more than one line, select all the lines you want to comment then use the shortcut. If you want to uncomment a comment, apply the same shortcut.

Knit a R Markdown document

You can knit R Markdown documents by using this shortcut:

command + Shift + K on Mac
Ctrl + Shift + K on Windows

Code snippets

Code snippets is usually a few characters long and is used as a shortcut to insert a common piece of code. You simply type a few characters then press Tab and it will complete your code with a larger code. Tab is then used again to navigate through the code where customization is required. For instance, if you type fun then press Tab, it will auto-complete the code with the required code to create a function:

name <- function(variables) {
  
}

Pressing Tab again will jump through the placeholders for you to edit it. So you can first edit the name of the function, then the variables and finally the code inside the function (try by yourself!).

There are many code snippets by default in RStudio. Here are the code snippets I use most often:

  • lib to call library()
library(package)
  • mat to create a matrix
matrix(data, nrow = rows, ncol = cols)
  • ifel, and ei to create conditional expressions such as if() {}else {} and else if () {}
if (condition) {
  
}
else {
  
}
else if (condition) {
  
}
  • fun to create a function
name <- function(variables) {
  
}
  • for to create for loops
for (variable in vector) {
  
}
  • ts to insert a comment with the current date and time (useful if you have very long code and share it with others so they see when it has been edited)
# Tue Jan 21 20:20:14 2020 ------------------------------
  • shinyapp every time I create a new shiny app
library(shiny)
ui <- fluidPage(
  
)
server <- function(input, output, session) {
  
}
shinyApp(ui, server)

You can see all default code snippets and add yours by clicking on Tools > Global Options… > Code (left sidebar) > Edit Snippets…

Ordered list in R Markdown

In R Markdown, when creating an ordered list such as this one:

  1. Item 1
  2. Item 2
  3. Item 3

Instead of bothering with the numbers and typing

1. Item 1
2. Item 2
3. Item 3

you can simply type

1. Item 1
1. Item 2
1. Item 3

for the exact same result (try it yourself or check the code of this article!). This way you do not need to bother which number is next when creating a new item.

To go even further, any numeric will actually render the same result as long as the first item is the number you want to start from. For example, you could type:

1. Item 1
7. Item 2
3. Item 3

which renders

  1. Item 1
  2. Item 2
  3. Item 3

However, I suggest always using the number you want to start from for all items because if you move one item at the top, the list will start with this new number. For instance, if we move 7. Item 2 from the previous list at the top, the list becomes:

7. Item 2
1. Item 1
3. Item 3

which incorrectly renders

  1. Item 2
  2. Item 1
  3. Item 3

New code chunk in R Markdown

When editing R Markdown documents, you will need to insert a new R code chunk many times. The following shortcuts will make your life easier:

command + option + I on Mac (or command + alt + I depending on your keyboard)
Ctrl + ALT + I on Windows

New R code chunk in R Markdown

New R code chunk in R Markdown

Reformat code

A clear and readable code is always easier and faster to read (and look more professional when sharing it to collaborators). To automatically apply the most common coding guidelines such as whitespaces, indents, etc., use:

cmd + Shift + A on Mac
Ctrl + Shift + A on Windows

So for example the following code which does not respect the guidelines (and which is not easy to read):

1+1
  for(i in 1:10){if(!i%%2){next}
print(i)
 }

becomes much more neat and readable:

1 + 1
for (i in 1:10) {
  if (!i %% 2) {
    next
  }
  print(i)
}

RStudio addins

RStudio addins are extensions which provide a simple mechanism for executing advanced R functions from within RStudio. In simpler words, when executing an addin (by clicking a button in the Addins menu), the corresponding code is executed without you having to write the code. RStudio addins have the advantage that they allow you to execute complex and advanced code much more easily than if you would have to write it yourself.

The addin I use most often is probably the {esquisse} addin, which allows to draw plots from the {ggplot2} package in a user-friendly and interactive way, and without having to write the code myself.

RStudio addins are quite diverse and require a more detailed explanation, so I wrote an article focusing on these addins. See the article here.

{pander} for aesthetics

The pander() function from the {pander} package is very useful for R Markdown documents and reporting. It is not actually a shortcut but it greatly improves the aesthetics of R outputs.

For instance, see below the difference between the default output of a Chi-square test of independence and the output from the same test with the pander() function (using the diamonds dataset from the {ggplot2} package):

library(ggplot2)
dat <- diamonds
test <- chisq.test(table(datcut</span>, dat<span class="hljs-variable">color))
test
## 
##  Pearson's Chi-squared test
## 
## data:  table(datcut, datcolor)
## X-squared = 310.32, df = 24, p-value < 2.2e-16
library(pander)
pander(test)
Pearson’s Chi-squared test: table(datcut, datcolor)
Test statisticdfP value
310.3241.395e-51 * * *

All information that you need are displayed in an elegant table. The pander() function works on many statistical tests (not to say all of them, but I have not tried it on all available tests in R) and on regression models:

# Linear model with lm()
model <- lm(price ~ carat + x + y + z, data = dat)
model
## 
## Call:
## lm(formula = price ~ carat + x + y + z, data = dat)
## 
## Coefficients:
## (Intercept)        carat            x            y            z  
##      1921.2      10233.9       -884.2        166.0       -576.2
pander(model)
Fitting linear model: price ~ carat + x + y + z
EstimateStd. Errort valuePr(>|t|)
(Intercept)1921104.418.411.977e-75
carat1023462.94162.60
x-884.240.47-21.852.317e-105
y16625.866.4211.365e-10
z-576.239.28-14.671.277e-48

The pander function also makes datasets, tables, vectors, etc. more readable in R Markdown output. For example, see the differences below:

head(diamonds) # first 6 observations of a dataset
## # A tibble: 6 x 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
pander(head(diamonds))
caratcutcolorclaritydepthtablepricexyz
0.23IdealESI261.5553263.953.982.43
0.21PremiumESI159.8613263.893.842.31
0.23GoodEVS156.9653274.054.072.31
0.29PremiumIVS262.4583344.24.232.63
0.31GoodJSI263.3583354.344.352.75
0.24Very GoodJVVS262.8573363.943.962.48
summary(diamonds) # main descriptive statistics
##      carat               cut        color        clarity          depth      
##  Min.   :0.2000   Fair     : 1610   D: 6775   SI1    :13065   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 4906   E: 9797   VS2    :12258   1st Qu.:61.00  
##  Median :0.7000   Very Good:12082   F: 9542   SI2    : 9194   Median :61.80  
##  Mean   :0.7979   Premium  :13791   G:11292   VS1    : 8171   Mean   :61.75  
##  3rd Qu.:1.0400   Ideal    :21551   H: 8304   VVS2   : 5066   3rd Qu.:62.50  
##  Max.   :5.0100                     I: 5422   VVS1   : 3655   Max.   :79.00  
##                                     J: 2808   (Other): 2531                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  950   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2401   Median : 5.700   Median : 5.710  
##  Mean   :57.46   Mean   : 3933   Mean   : 5.731   Mean   : 5.735  
##  3rd Qu.:59.00   3rd Qu.: 5324   3rd Qu.: 6.540   3rd Qu.: 6.540  
##  Max.   :95.00   Max.   :18823   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z         
##  Min.   : 0.000  
##  1st Qu.: 2.910  
##  Median : 3.530  
##  Mean   : 3.539  
##  3rd Qu.: 4.040  
##  Max.   :31.800  
## 
pander(summary(diamonds))
Table continues below
caratcutcolorclaritydepth
Min. :0.2000Fair : 1610D: 6775SI1 :13065Min. :43.00
1st Qu.:0.4000Good : 4906E: 9797VS2 :122581st Qu.:61.00
Median :0.7000Very Good:12082F: 9542SI2 : 9194Median :61.80
Mean :0.7979Premium :13791G:11292VS1 : 8171Mean :61.75
3rd Qu.:1.0400Ideal :21551H: 8304VVS2 : 50663rd Qu.:62.50
Max. :5.0100NAI: 5422VVS1 : 3655Max. :79.00
NANAJ: 2808(Other): 2531NA
tablepricexyz
Min. :43.00Min. : 326Min. : 0.000Min. : 0.000Min. : 0.000
1st Qu.:56.001st Qu.: 9501st Qu.: 4.7101st Qu.: 4.7201st Qu.: 2.910
Median :57.00Median : 2401Median : 5.700Median : 5.710Median : 3.530
Mean :57.46Mean : 3933Mean : 5.731Mean : 5.735Mean : 3.539
3rd Qu.:59.003rd Qu.: 53243rd Qu.: 6.5403rd Qu.: 6.5403rd Qu.: 4.040
Max. :95.00Max. :18823Max. :10.740Max. :58.900Max. :31.800
NANANANANA
table(datcut</span>, dat<span class="hljs-variable">color) # contingency table
##            
##                D    E    F    G    H    I    J
##   Fair       163  224  312  314  303  175  119
##   Good       662  933  909  871  702  522  307
##   Very Good 1513 2400 2164 2299 1824 1204  678
##   Premium   1603 2337 2331 2924 2360 1428  808
##   Ideal     2834 3903 3826 4884 3115 2093  896
pander(table(datcut</span>, dat<span class="hljs-variable">color))
DEFGHIJ
Fair163224312314303175119
Good662933909871702522307
Very Good151324002164229918241204678
Premium160323372331292423601428808
Ideal283439033826488431152093896
names(dat) # variable names
##  [1] "carat"   "cut"     "color"   "clarity" "depth"   "table"   "price"  
##  [8] "x"       "y"       "z"
pander(names(dat))

caratcutcolorclaritydepthtablepricexy and z

rnorm(4) # generates 4 observations from a standard normal distribution
## [1]  0.1002168 -0.1493376  0.0469573  0.4508323
pander(rnorm(4))

1.591-0.36030.624 and -0.305

Others

Similar to many other programs, you can also use:

[Related Article: 15+ Resources to Get Started with R]

  • command + Shift + N on Mac and Ctrl + Shift + N on Windows to open a new R Script
  • command + S on Mac and Ctrl + S on Windows to save your current script or R Markdown document

Thanks for reading. I hope you find these tips and tricks useful. If you are using others, feel free to share them in the comment section.

Originally Posted Here

Antoine Soetewey

Antoine Soetewey

Antoine Soetewey is a PhD student in statistics at UCLouvain (Belgium) within the Institute of Statistics, Biostatistics and Actuarial Sciences. His research interests focus on survival analysis and bio-statistical procedures applied to cancer patients. In parallel with his doctoral thesis, he is a teaching assistant for several courses in statistics and probability at bachelor and master’s level. He also provides trainings and workshops in statistics and R (an open source statistical software program) as part of a Belgian statistical consulting company. Given his experience, he also helps academics and professionals in performing statistical data analyses for their academic or work-related projects. See more at www.antoinesoetewey.com (personal website) or www.statsandr.com (blog).

1