# 18 Bestiary of Brackets and other R notation

!#@%&?

What does that mean?

This page is a guide to decoding what all the non-alphanumeric used in R mean. It covers base R use and the most common uses within tidyverse and related packages.

## 18.1 Assignment arrows

The usual way to assign an object to a name is to use a left-pointing arrow <-.

x <- 42 # This will assign the value 42 to the name x

The keyboard short-cut for the assign arrow is Alt+-.

It is also possible to use right-pointing arrows ->.

42 -> x # This will assign the value 42 to the name x

This is not recommended for use in scripts because it makes the code difficult to read.

## 18.2 Backslash

Some characters cannot be directly represented in character strings so an escape sequence is used instead. Escape sequences always start with the backslash character \.

• \n new line
• \t tab (very useful for importing tab-delimited files with read_delim)
• \\ backslash
• \U1F600 including Unicode or emoji 😀

If you want to use a literal backslash in a character string, it needs to be escaped with another backslash. Note that print shows the contents of the string, including the backslashes, while cat shows what would be shows by ggplot etc.

x <- "Backslash-n gives a newline\nThis is a literal backslash '\\'"
print(x)
##  "Backslash-n gives a newline\nThis is a literal backslash '\\'"
cat(x)
## Backslash-n gives a newline
## This is a literal backslash '\'

## 18.3 Brackets

Three different types of bracket are used in R.

The round brackets () are used to contain all the arguments to a function.

rnorm(n = 10, mean = 10)

Round brackets () are also used to force the order of operations in an calculation or Boolean logic.

9 * 6 + 5 # 59
##  59
9 * (6 + 5) # 99
##  99

Single square brackets [] are used for sub-setting vectors, matrices and similar objects.

month.name[6:7] # extract the 6th and 7th element of month.name
##  "June" "July"
penguins[1, "species"] # first element of species column (dplyr gives neater ways to do this)
## # A tibble: 1 × 1
##   species
##   <fct>
## 1 Adelie

Double square brackets are used to extract elements from a list

x <- list(a = 1, b = TRUE)
x # single square bracket returns a list - see the $a ##$a
##  1
x[] # double square bracket returns element directly
##  1

Curly brackets or braces {} are used to keep code that needs to be run together as a single expression. This is commonly done when writing a function

cylinder_volume <- function(radius = 1, height){
radius ^ 2 * pi * height
}

cylinder_volume(height = 2)

or when writing an if statement

if(logical_condition){
#some code to run if logical_condition is TRUE
}

Curly brackets are also used in the glue package, an easy to use alternative to paste. The curly brackets demarcate R code to include in the output.

glue::glue("The penguins dataset has {nrow(penguins)} rows")
## The penguins dataset has 344 rows
paste("The penguins dataset has", nrow(penguins), "rows") # base R alternative gets clunky fast
##  "The penguins dataset has 344 rows"

Double curly brackets {{}} are used programming with tidyverse. See the dplyr programming vignette for details.

## 18.4 Boolean operators

Boolean operators are used to combine logical tests.

If a and b are logical vectors (i.e. TRUE and FALSE). or vectors that can be coerced to logical vectors, then

• !a NOT a
• a & b a AND b
• a | b a OR b

Boolean operators can be combined to form complex statements. Round brackets can be used to force the order of evaluation

x <- 10
x > 0 | x < 20  # is x between 0 and 20
##  TRUE
x > 0 | x < 20  & x %% 2 == 1 # is x an odd number between 0 and 20. Wrong order of evaluation.
##  TRUE
(x < 20 | x > 0)  & x %% 2 == 1 # Brackets force correct order.
##  FALSE

It is probably a good idea to always use brackets in statements with multiple Boolean operators to make the order of evaluation clear to the reader.

You will also see doubled up || and && operators. These are special Boolean operators that only return the first element of any vector of results. This is useful with if statements which want only a single TRUE or FALSE.

## 18.5 Colons

A single colon : is used to get a sequence between two values with a step size of one.

1:10
##    1  2  3  4  5  6  7  8  9 10
#equivalent to
seq(from = 1, to = 10, by = 1)
##    1  2  3  4  5  6  7  8  9 10

A single colon is also used when detaching a package which is almost, but not quite, the opposite of running library.

library(mgcv) # loads mgcv package
detach("package:mgcv") # 

Because detach is not exactly the opposite of library, it is normally better to restart the R session (In Rstudio go to ‘Session’ then ‘Restart R’).

Double colons :: let you use a single function from a package without loading the entire package. For example, readxl::read_excel() will run read_excel(). This can prevent conflicts between functions with the same name in different packages, or make code clearer by being explicit where a function comes from.

Triple colons ::: are used to access a package’s internal functions. This can be useful to access the help file or to view the code, but you should not rely on the internal functions staying the same.

## 18.6 Commas

Arguments in a function need to be separated by a comma.

x <- rnorm(n = 10, mean = 1, sd = 0.5)

If you forget to use a comma, you will get a reminder from R, and Rstudio should underline the problem.

x <- rnorm(n = 10, mean = 1 sd = 0.5)
## Error: <text>:1:29: unexpected symbol
## 1: x <- rnorm(n = 10, mean = 1 sd
##                                 ^ Rstudio puts a cross in the margin and underlines the unexpected text after the missing comma.

## 18.7 Dot

The dot . is used as the decimal separator in R, for example in 3.1415927.

Object names can have a dot in them. If the name starts with a dot, the object is invisible and won’t show up in the environment tab in Rstudio.

Dots in function names can be decorative, but can also be used by R to determine which function needs to be used for each type of object. For example, there is a summary.lm() and an anova.lm() which process lm class objects generated by the function lm(). There are also glm(), lme(), and many other versions of these functions available to process different classes of objects. When you run summary() or anova(), which are known as a generic functions, R will automatically select the correct version to use based on the class of object given. You can find out the class of an object my using the function class().

## 18.8 …

You will often see three dots ... in a function’s arguments. These are used when the name (and number) of arguments is not known in advance. For example, the functionc() has ... in its arguments, allowing you to put any number of objects with any names into the function to make a vector. The three dots are often used by generic functions because the different versions of the function for different classes of object need different arguments. In dplyr, the dots are used for the column names of the data frame or tibble being processed.

## 18.9 Dollars

The dollar symbol $ is used to subset lists. Many objects in R, including data frames, are special types of lists. x <- list(a = 1, b = TRUE) x$a
##  1

## 18.20 Semi-colon

The semi-colon ; can optionally be used at the end of a line of code, but there is no advantage to doing this (in other languages it can be compulsory). It can be used to separate two R statements on the same line. This is not recommended as it can make code difficult to read.

# bad
i <- 7 * 6; print(i)

# good
i <- 7 * 6
print(i)

## 18.21 Underscore

The underscore _ can be used in object names along with alphanumeric characters and dot. It cannot be the first character of the name. It is useful as a word separator in snake_case.

## 18.22 White space

White space is free and usually has no impact on how the code runs. Use lots of it to make your code more readable.

## 18.23 @

You will occasionally see an @ in R code. They are used to subset S4 class objects, analogous to the use of \$ to subset lists and data frames. Unless you are coding your own S4 classes, you probably won’t see or use @ very often. See Chapter 15 of Advanced R for more information about S4 classes.

### Contributors

Richard J. Telford