|
| 1 | +# Google's R Style Guide |
| 2 | + |
| 3 | +R is a high-level programming language used primarily for statistical computing |
| 4 | +and graphics. The goal of the R Programming Style Guide is to make our R code |
| 5 | +easier to read, share, and verify. |
| 6 | + |
| 7 | +The Google R Style Guide is a fork of the |
| 8 | +[Tidyverse Style Guide](https://style.tidyverse.org/) by Hadley Wickham |
| 9 | +[license](https://creativecommons.org/licenses/by-sa/2.0/). Google modifications |
| 10 | +were developed in collaboration with the internal R user community. The rest of |
| 11 | +this document explains Google's primary differences with the Tidyverse guide, |
| 12 | +and why these differences exist. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +### Naming conventions |
| 17 | + |
| 18 | +Google prefers identifying functions with `BigCamelCase` to clearly distinguish |
| 19 | +them from other objects. |
| 20 | + |
| 21 | +``` |
| 22 | +# Good |
| 23 | +DoNothing <- function() { |
| 24 | + return(invisible(NULL)) |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +The names of private functions should begin with a dot. This helps communicate |
| 29 | +both the origin of the function and its intended use. |
| 30 | + |
| 31 | +``` |
| 32 | +# Good |
| 33 | +.DoNothingPrivately <- function() { |
| 34 | + return(invisible(NULL)) |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +We previously recommended naming objects with `dot.case`. We're moving away from |
| 39 | +that, as it creates confusion with S3 methods. |
| 40 | + |
| 41 | +### Don't use attach() |
| 42 | + |
| 43 | +The possibilities for creating errors when using `attach()` are numerous. |
| 44 | + |
| 45 | +## Pipes |
| 46 | + |
| 47 | +### Right-hand assignment |
| 48 | + |
| 49 | +We do not support using right-hand assignment. |
| 50 | + |
| 51 | +``` |
| 52 | +# Bad |
| 53 | +iris %>% |
| 54 | + dplyr::summarize(max_petal = max(Petal.Width)) -> results |
| 55 | +``` |
| 56 | + |
| 57 | +This convention differs substantially from practices in other languages and |
| 58 | +makes it harder to see in code where an object is defined. E.g. searching for |
| 59 | +`foo <-` is easier than searching for `foo <-` and `-> foo` (possibly split over |
| 60 | +lines). |
| 61 | + |
| 62 | +### Use explicit returns |
| 63 | + |
| 64 | +Do not rely on R's implicit return feature. It is better to be clear about your |
| 65 | +intent to `return()` an object. |
| 66 | + |
| 67 | +``` |
| 68 | +# Good |
| 69 | +AddValues <- function(x, y) { |
| 70 | + return(x + y) |
| 71 | +} |
| 72 | +
|
| 73 | +# Bad |
| 74 | +AddValues <- function(x, y) { |
| 75 | + x + y |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Qualifying namespaces |
| 80 | + |
| 81 | +Users should explicitly qualify namespaces for all external functions. |
| 82 | + |
| 83 | +``` |
| 84 | +# Good |
| 85 | +purrr::map() |
| 86 | +``` |
| 87 | + |
| 88 | +We discourage using the `@import` Roxygen tag to bring in all functions into a |
| 89 | +NAMESPACE. Google has a very big R codebase, and importing all functions creates |
| 90 | +too much risk for name collisions. |
| 91 | + |
| 92 | +While there is a small performance penalty for using `::`, it makes it easier to |
| 93 | +understand dependencies in your code. There are some exceptions to this rule. |
| 94 | + |
| 95 | +* Infix functions (`%name%`) always need to be imported. |
| 96 | +* Certain `rlang` pronouns, notably `.data`, need to be imported. |
| 97 | +* Functions from default R packages, including `datasets`, `utils`, |
| 98 | + `grDevices`, `graphics`, `stats` and `methods`. If needed, you can `@import` |
| 99 | + the full package. |
| 100 | + |
| 101 | +When importing functions, place the `@importFrom` tag in the Roxygen header |
| 102 | +above the function where the external dependency is used. |
| 103 | + |
| 104 | +## Documentation |
| 105 | + |
| 106 | +### Package-level documentation |
| 107 | + |
| 108 | +All packages should have a package documentation file, in a |
| 109 | +`packagename-package.R` file. |
0 commit comments