Other Recommendations
In this lesson, we have added a few recommendation for using the R language.
R Language recommendations
- Use
library()instead ofrequire(), unless it is a conscious choice. Package names should be characters (avoid NSE – non-standard evaluation).
- In a function call, arguments can be specified by position, complete name, or partial name. Never specify by partial name and never mix by position and complete name.
-
While developing a package, specify arguments by name.
-
The required (with no default value) arguments should be first, followed by optional arguments.
- The
...argument should either be in the beginning or the end.
- Good practice rule is to set default arguments inside the function using
NULLidiom, and avoid dependence between arguments:
-
Always validate arguments in a function.
-
While developing a package, specify the namespace of each used function, except if it is from a base package.
-
Do NOT put more than one statement (command) per line. Do NOT use a semicolon
;as a termination of the command.
-
Avoid using
setwd("/Users/irudnyts/path/that/only/I/have"). Almost surely your collaborators will have different paths, which makes the project not portable. Instead, usehere::here()function fromhere()package. -
Avoid using
rm(list = ls()). This statement deletes all objects from the global environment and gives you an illusion of a fresh R start.
Remember
- Use common sense and BE CONSISTENT.
- If you are editing code, take a few minutes to look at the code around you and determine its style.
- The point of having style guidelines is to have a common vocabulary.