How to Structure Your Code in R - dummies

How to Structure Your Code in R

By Andrie de Vries, Joris Meys

Names aren’t the only things that can influence the readability of your R code. When you start nesting functions or perform complex calculations, your code can turn into a big mess of text and symbols rather quickly.

Luckily, you have some tricks to clear up your code so you can still decipher what you did three months down the road.

Nesting functions and doing complex calculations can lead to very long lines of code. If you want to make a vector with the names of your three most beloved song titles, for example, you’re already in for trouble. Luckily, R lets you break a line of code over multiple lines in your script, so you don’t have to scroll to the right the whole time.

You don’t even have to use a special notation or character. R will know that the line isn’t finished as long as you give it some hint. Generally speaking, you have to make sure the command is undoubtedly incomplete. There are several ways to do that:

  • You can use a quotation mark to start a string. R will take all the following input — including the line breaks — as part of the string, until it meets the matching second quotation mark.

  • You can end the incomplete line with an operator (like +, /, <-, and so on). R will know that something else must follow. This lets you create structure in longer calculations.

  • You can open a parenthesis for a function. R will read all the input it gets as one line until it meets the matching parenthesis. This allows you to line up arguments below a function, for example.

The following little script shows all these techniques:

baskets.of.Geraldine <-
Intro <- "It is amazing! The All Star Grannies scored
a total of"
Outro <- "baskets in the last six games!"
Total.baskets <- baskets.of.Granny +
Text <- paste(Intro,

You can copy this code into a script file and run it in the console. If you run this little snippet of code, you see the following output in the console:

It is amazing! The All Star Grannies scored
a total of 71 baskets in the last six games!

This immediately shows what the cat() function does. It prints whatever you give it as an argument directly to the console. It also interprets special characters like line breaks and tabs. If you look at the vector Text, you would see this:

> Text
[1] "It is amazing! The All Star Grannies scored na total of 71 baskets in the last six games!"

The n represents the line break. Even though it’s pasted to the a, R will recognize n as a separate character.

All this also works at the command line. If you type an unfinished command, R will change the prompt to a + sign, indicating that you can continue to type your command:

> cat('If you doubt whether it works,
+ just try it out.')
If you doubt whether it works,
just try it out.

RStudio automatically adds a line break at the end of a cat() statement if there is none, but R doesn’t do that. So, if you don’t use RStudio, remember to add a line break (or the symbol n) at the end of your string.