├── LICENSE.md ├── README.md ├── _config.yml ├── _includes ├── footer.html ├── header.html ├── js.html └── syntax.css ├── _layouts └── default.html ├── font ├── fontello.eot ├── fontello.svg ├── fontello.ttf └── fontello.woff ├── index.html └── stylesheet.css /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jean Fan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # R Style Guide 2 | ### Best practices for readable, sharable, and verifiable R code 3 | 4 | R is a high-level programming language used primarily for statistical computing and graphics. R does not have any well defined coding recommendations or de facto standards. This style guide provides some recommendations based on personal experience and expert opinions (see Additional Guides). Use your best judgement and common sense when deciding whether to adhere to these recommendations. Be consistent. Use style to enhance legibility. Ultimately, a unified style will help make collaboration and sharing code easier. 5 | 6 | **[Start reading ☞](http://jefworks.github.io/R-style-guide/)** 7 | 8 | Feel free to fork or clone and make your own R style guide for your lab, team, or collaborative R project. 9 | 10 | ## Additional Guides 11 | - https://style.tidyverse.org/ 12 | - http://r-pkgs.had.co.nz/style.html 13 | - https://google.github.io/styleguide/Rguide.xml 14 | - https://docs.google.com/document/d/1esDVxyWvH8AsX-VJa-8oqWaHLs4stGlIbk8kLc5VlII/edit 15 | - http://master.bioconductor.org/developers/how-to/coding-style/ 16 | 17 | ## Inspiration 18 | - http://mdo.github.io/code-guide/ 19 | 20 | ## Other Info 21 | - made with [Jekyll](https://help.github.com/articles/using-jekyll-with-pages/) 22 | - color scheme from [Shiny](http://shiny.rstudio.com/) 23 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | name: R Style Guide 2 | description: Best practices for readable, sharable, and verifiable R code 3 | url: http://jefworks.github.io/R-style-guide/ 4 | markdown: rdiscount 5 | permalink: pretty -------------------------------------------------------------------------------- /_includes/footer.html: -------------------------------------------------------------------------------- 1 | 11 | -------------------------------------------------------------------------------- /_includes/header.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 📖 4 |

{{ site.name }}

5 |

{{ site.description }}

6 |
7 |
8 | -------------------------------------------------------------------------------- /_includes/js.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JEFworks-Lab/R-style-guide/ef45fc096e03c2a1c1147fd20ad108306dcf9fe2/_includes/js.html -------------------------------------------------------------------------------- /_includes/syntax.css: -------------------------------------------------------------------------------- 1 | .hll { background-color: #ffffcc } 2 | /*{ background: #f0f3f3; }*/ 3 | .c { color: #999; } /* Comment */ 4 | .err { color: #AA0000; background-color: #FFAAAA } /* Error */ 5 | .k { color: #006699; } /* Keyword */ 6 | .o { color: #555555 } /* Operator */ 7 | .cm { color: #999; } /* Comment.Multiline */ /* Edited to remove italics and make into comment */ 8 | .cp { color: #009999 } /* Comment.Preproc */ 9 | .c1 { color: #999; } /* Comment.Single */ 10 | .cs { color: #999; } /* Comment.Special */ 11 | .gd { background-color: #FFCCCC; border: 1px solid #CC0000 } /* Generic.Deleted */ 12 | .ge { font-style: italic } /* Generic.Emph */ 13 | .gr { color: #FF0000 } /* Generic.Error */ 14 | .gh { color: #003300; } /* Generic.Heading */ 15 | .gi { background-color: #CCFFCC; border: 1px solid #00CC00 } /* Generic.Inserted */ 16 | .go { color: #AAAAAA } /* Generic.Output */ 17 | .gp { color: #000099; } /* Generic.Prompt */ 18 | .gs { } /* Generic.Strong */ 19 | .gu { color: #003300; } /* Generic.Subheading */ 20 | .gt { color: #99CC66 } /* Generic.Traceback */ 21 | .kc { color: #006699; } /* Keyword.Constant */ 22 | .kd { color: #006699; } /* Keyword.Declaration */ 23 | .kn { color: #006699; } /* Keyword.Namespace */ 24 | .kp { color: #006699 } /* Keyword.Pseudo */ 25 | .kr { color: #006699; } /* Keyword.Reserved */ 26 | .kt { color: #007788; } /* Keyword.Type */ 27 | .m { color: #FF6600 } /* Literal.Number */ 28 | .s { color: #d44950 } /* Literal.String */ 29 | .na { color: #4f9fcf } /* Name.Attribute */ 30 | .nb { color: #336666 } /* Name.Builtin */ 31 | .nc { color: #00AA88; } /* Name.Class */ 32 | .no { color: #336600 } /* Name.Constant */ 33 | .nd { color: #9999FF } /* Name.Decorator */ 34 | .ni { color: #999999; } /* Name.Entity */ 35 | .ne { color: #CC0000; } /* Name.Exception */ 36 | .nf { color: #CC00FF } /* Name.Function */ 37 | .nl { color: #9999FF } /* Name.Label */ 38 | .nn { color: #00CCFF; } /* Name.Namespace */ 39 | .nt { color: #2f6f9f; } /* Name.Tag */ 40 | .nv { color: #003333 } /* Name.Variable */ 41 | .ow { color: #000000; } /* Operator.Word */ 42 | .w { color: #bbbbbb } /* Text.Whitespace */ 43 | .mf { color: #FF6600 } /* Literal.Number.Float */ 44 | .mh { color: #FF6600 } /* Literal.Number.Hex */ 45 | .mi { color: #FF6600 } /* Literal.Number.Integer */ 46 | .mo { color: #FF6600 } /* Literal.Number.Oct */ 47 | .sb { color: #CC3300 } /* Literal.String.Backtick */ 48 | .sc { color: #CC3300 } /* Literal.String.Char */ 49 | .sd { color: #CC3300; font-style: italic } /* Literal.String.Doc */ 50 | .s2 { color: #CC3300 } /* Literal.String.Double */ 51 | .se { color: #CC3300; } /* Literal.String.Escape */ 52 | .sh { color: #CC3300 } /* Literal.String.Heredoc */ 53 | .si { color: #AA0000 } /* Literal.String.Interpol */ 54 | .sx { color: #CC3300 } /* Literal.String.Other */ 55 | .sr { color: #33AAAA } /* Literal.String.Regex */ 56 | .s1 { color: #CC3300 } /* Literal.String.Single */ 57 | .ss { color: #FFCC33 } /* Literal.String.Symbol */ 58 | .bp { color: #336666 } /* Name.Builtin.Pseudo */ 59 | .vc { color: #003333 } /* Name.Variable.Class */ 60 | .vg { color: #003333 } /* Name.Variable.Global */ 61 | .vi { color: #003333 } /* Name.Variable.Instance */ 62 | .il { color: #FF6600 } /* Literal.Number.Integer.Long */ 63 | 64 | .css .o, 65 | .css .o + .nt, 66 | .css .nt + .nt { color: #999; } 67 | -------------------------------------------------------------------------------- /_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {{ site.name }} 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | {% include header.html %} 20 | 21 | {{ content }} 22 | 23 | {% include footer.html %} 24 | 25 | {% include js.html %} 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /font/fontello.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JEFworks-Lab/R-style-guide/ef45fc096e03c2a1c1147fd20ad108306dcf9fe2/font/fontello.eot -------------------------------------------------------------------------------- /font/fontello.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Copyright (C) 2014 by original authors @ fontello.com 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /font/fontello.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JEFworks-Lab/R-style-guide/ef45fc096e03c2a1c1147fd20ad108306dcf9fe2/font/fontello.ttf -------------------------------------------------------------------------------- /font/fontello.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JEFworks-Lab/R-style-guide/ef45fc096e03c2a1c1147fd20ad108306dcf9fe2/font/fontello.woff -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | --- 4 | 5 |
6 |
7 | R is a high-level programming language used primarily for statistical computing and graphics. R does not have any well defined coding recommendations or de facto standards. This style guide provides some recommendations based on personal experience and expert opinions (see Additional Guides). Use your best judgement and common sense when deciding whether to adhere to these recommendations. Be consistent. Use style to enhance legibility. Ultimately, a unified style will help make collaboration and sharing code easier. 8 |
9 |
10 | 11 |
12 |

Table of contents

13 |
14 |
15 |
16 | 38 |
39 |
40 | 41 | 42 |
43 |

Naming Conventions

44 |
45 |
46 |
47 |

File Names

48 | 54 |
55 |
56 | {% highlight R %} 57 | # Good 58 | getData.R 59 | 60 | # Acceptable 61 | get_data.R 62 | 63 | # Bad 64 | foo.bar.r 65 | 89317240934735.r 66 | {% endhighlight %} 67 |
68 |
69 |
70 |
71 |

Identifiers

72 | 80 |
81 |
82 | {% highlight R %} 83 | # Good 84 | pcaEmbedding # variable 85 | getPcaEmbedding <- function() # function 86 | 87 | # Acceptable 88 | pca.embedding 89 | getEmb <- function() 90 | 91 | # Bad 92 | pca_embedding 93 | get_pca_embedding <- function() 94 | get.pca.embedding <- function() 95 | 96 | # Really bad 97 | pe 98 | gpe <- function() 99 | foo-bar <- function() 100 | {% endhighlight %} 101 |
102 |
103 | 104 | 105 |
106 |

Syntax

107 |
108 | 109 |
110 |
111 |

General Syntax

112 | 117 |
118 |
119 | {% highlight R %} 120 | # Good 121 | x <- 23 122 | y <- 12 123 | 124 | # Bad 125 | x = 23; y = 12 126 | 23 -> x 127 | {% endhighlight %} 128 |
129 |
130 | 131 |
132 |
133 |

Spacing

134 | 139 |
140 |
141 | {% highlight R %} 142 | # Good 143 | someFunction <- function(a, b, c, d = 100) { 144 | if (a < d) { 145 | a <- (b + c) * d 146 | } 147 | } 148 | 149 | # Bad 150 | someFunction <- function(a,b,c,d= 100) { 151 | if(a 155 |
156 | 157 |
158 |
159 |

Curly Braces

160 |
    161 |
  • Do not place { on its own line
  • 162 |
  • Place } on its own line unless followed by else
  • 163 |
  • Surround else statements with curly braces
  • 164 |
165 |
166 |
167 | {% highlight R %} 168 | # Good 169 | if (a < d) { 170 | a <- (b + c) * d 171 | } else { 172 | a <- d 173 | } 174 | 175 | # Bad 176 | if (a < d) 177 | { 178 | a <- (b + c) * d 179 | } else a <- d 180 | {% endhighlight %} 181 |
182 |
183 | 184 |
185 |
186 |

Line Length

187 |
    188 |
  • Keep each line less than 80 characters
  • 189 |
  • Align lines by shared features like parameters if possible
  • 190 |
191 |
192 |
193 | {% highlight R %} 194 | # Good 195 | someFunction(optionA = alpha, 196 | optionB = bravo, 197 | optionC = charlie, 198 | optionD = delta) 199 | 200 | # Acceptable 201 | someFunction(optionA = alpha, 202 | optionB = bravo, optionC = charlie, 203 | optionD = delta) 204 | 205 | # Bad 206 | someFunction(optionA = alpha, optionB = bravo, optionC = charlie, optionD = delta) 207 | {% endhighlight %} 208 |
209 |
210 | 211 |
212 |

Organization

213 |
214 | 215 |
216 |
217 |

Commenting

218 |
    219 |
  • General comments should be identified with # followed by a space
  • 220 |
  • Inline comments need 2 spaces before the #
  • 221 |
  • Function and object descriptions should adhere to roxygen guidelines
  • 222 |
  • Separate function comments and function code with blank line 223 |
  • Functions from previous code using 2 blank lines 224 |
225 |
226 |
227 | {% highlight R %} 228 | #' Add together two numbers. 229 | #' 230 | #' @param x A number. 231 | #' @param y A number. 232 | #' 233 | #' @return The sum of x and y. 234 | #' 235 | #' @examples 236 | #' add(1, 1) 237 | #' add(10, 1) 238 | #' 239 | add <- function(x, y) { 240 | # general comment 241 | x + y # inline comment 242 | } 243 | 244 | 245 | #' Next function 246 | {% endhighlight %} 247 |
248 |
249 | 250 | 251 |
252 |

Additional Tips

253 |
254 | 255 |
256 |
257 |

Loops

258 |
    259 |
  • Use seq(along = x) to protect against instances where x is empty
  • 260 |
  • Use apply, lapply, etc when possible 261 |
262 |
263 |
264 | {% highlight R %} 265 | # Good 266 | lapply(seq(along = x), function(i) { 267 | doSomething(i) 268 | }) 269 | 270 | # Acceptable 271 | for (i in seq(along = x)) { 272 | doSomething(i) 273 | } 274 | 275 | # Bad 276 | for (i in 1:length(x)) { 277 | doSomething(i) 278 | } 279 | {% endhighlight %} 280 |
281 |
282 |
283 |
284 |

Repeat

285 |
    286 |
  • Use repeat for better interpretability
  • 287 |
288 |
289 |
290 | {% highlight R %} 291 | # Good 292 | repeat { 293 | ... 294 | } 295 | 296 | # Bad 297 | while (TRUE) { 298 | ... 299 | } 300 | {% endhighlight %} 301 |
302 |
303 | 304 | 305 |
306 |

References and Resources

307 |
308 | 309 |
310 | 318 |
319 |
320 |
321 |

Inspiration

322 |
  • Code Guide by mdo
  • 323 |
    324 |
    325 | 326 | 327 | -------------------------------------------------------------------------------- /stylesheet.css: -------------------------------------------------------------------------------- 1 | --- 2 | layout: nil 3 | --- 4 | 5 | 6 | /* 7 | * Fonts 8 | */ 9 | 10 | @font-face { 11 | font-family: 'fontello'; 12 | src: url('font/fontello.eot'); 13 | src: url('font/fontello.eot#iefix') format('embedded-opentype'), 14 | url('font/fontello.woff') format('woff'), 15 | url('font/fontello.ttf') format('truetype'), 16 | url('font/fontello.svg') format('svg'); 17 | font-weight: normal; 18 | font-style: normal; 19 | } 20 | 21 | [class^="icon-"]:before, [class*=" icon-"]:before { 22 | font-family: "fontello"; 23 | font-style: normal; 24 | font-weight: normal; 25 | speak: none; 26 | display: inline-block; 27 | text-decoration: inherit; 28 | width: 1em; 29 | margin-right: .2em; 30 | text-align: center; 31 | font-variant: normal; 32 | text-transform: none; 33 | } 34 | 35 | .icon-github-circled:before { content: '\e800'; } /* '' */ 36 | .icon-twitter:before { content: '\e801'; } /* '' */ 37 | 38 | 39 | /* 40 | * Scaffolding and type 41 | */ 42 | 43 | html { 44 | font-size: 16px; 45 | } 46 | @media (min-width: 48em) { 47 | html { 48 | font-size: 20px; 49 | } 50 | } 51 | 52 | body { 53 | margin: 0; 54 | font: 1rem/1.5 "PT Sans", sans-serif; 55 | color: #5a5a5a; 56 | } 57 | 58 | a { 59 | color: #08c; 60 | text-decoration: none; 61 | } 62 | a:hover { 63 | text-decoration: underline; 64 | } 65 | 66 | h1, h2, h3, h4 { 67 | margin: 0 0 .5rem; 68 | font-weight: normal; 69 | line-height: 1; 70 | color: #2a2a2a; 71 | letter-spacing: -.05em; 72 | } 73 | h1 { font-size: 3rem; } 74 | h2 { font-size: 2.5rem; } 75 | h3 { font-size: 1.75rem; } 76 | h4 { font-size: 1.25rem } 77 | 78 | p { 79 | margin: 0 0 1rem; 80 | } 81 | .lead { 82 | font-size: 1.3rem; 83 | } 84 | 85 | blockquote { 86 | position: relative; 87 | margin: 0 1rem 1rem; 88 | font-style: italic; 89 | color: #7a7a7a; 90 | } 91 | blockquote p { 92 | margin-bottom: 0; 93 | } 94 | 95 | ul li { 96 | margin-bottom: .25rem; 97 | } 98 | 99 | /* Tighten up margin on last items */ 100 | p:last-child, 101 | ul:last-child, 102 | blockquote:last-child{ 103 | margin-bottom: 0; 104 | } 105 | 106 | 107 | 108 | /* 109 | * Code 110 | */ 111 | 112 | code, 113 | pre { 114 | font-family: "PT Mono", Menlo, "Courier New", monospace; 115 | font-size: 95%; 116 | } 117 | code { 118 | padding: 2px 4px; 119 | font-size: 85%; 120 | color: #d44950; 121 | background-color: #f7f7f9; 122 | border-radius: .2rem; 123 | } 124 | 125 | pre { 126 | display: block; 127 | margin: 0 0 1rem; 128 | line-height: 1.4; 129 | white-space: pre; 130 | white-space: pre-wrap; 131 | } 132 | pre code { 133 | padding: 0; 134 | color: inherit; 135 | background-color: transparent; 136 | border: 0; 137 | } 138 | .highlight { 139 | margin: 0; 140 | } 141 | .highlight pre { 142 | margin-bottom: 0; 143 | } 144 | .highlight + .highlight { 145 | margin-top: 1rem; 146 | } 147 | 148 | 149 | /* 150 | * The Grid 151 | */ 152 | 153 | .col { 154 | padding: 2rem 1rem; 155 | } 156 | .col p { 157 | max-width: 40rem; 158 | } 159 | .col + .col { 160 | border-top: 1px solid #dfe1e8; 161 | background-color: #f7f7f9; 162 | } 163 | @media (min-width: 38em) { 164 | .col { 165 | padding: 2rem; 166 | } 167 | } 168 | @media (min-width: 48em) { 169 | .section { 170 | display: table; 171 | width: 100%; 172 | table-layout: fixed; 173 | } 174 | .col { 175 | display: table-cell; 176 | padding: 3rem; 177 | vertical-align: top; 178 | } 179 | .col + .col { 180 | border-top: 0; 181 | } 182 | } 183 | 184 | /* Make the ToC a whole section */ 185 | .toc .col + .col { 186 | background-color: #fff; 187 | } 188 | 189 | 190 | /* 191 | * Masthead 192 | */ 193 | 194 | .masthead { 195 | padding: 3rem 1rem; 196 | color: rgba(255,255,255,.5); 197 | text-align: center; 198 | background-color: #428bca; 199 | } 200 | .masthead h1 { 201 | color: #fff; 202 | margin-bottom: .25rem; 203 | } 204 | .masthead .icon { 205 | display: inline-block; 206 | font-size: 3rem; 207 | margin: 0 .5rem; 208 | } 209 | .masthead-links { 210 | font-size: 2rem; 211 | } 212 | .masthead-links a { 213 | color: rgba(255,255,255,.5); 214 | text-decoration: none; 215 | transition: all .15s linear; 216 | } 217 | .masthead-links a:hover { 218 | color: #fff; 219 | } 220 | 221 | @media (min-width: 38em) { 222 | .masthead { 223 | padding-top: 4rem; 224 | padding-bottom: 4rem; 225 | } 226 | } 227 | 228 | 229 | /* 230 | * Sections 231 | */ 232 | 233 | .heading { 234 | padding: 2rem 1rem 1.5rem; 235 | background-color: #2f6391; 236 | } 237 | .heading h2 { 238 | color: #fff; 239 | } 240 | 241 | @media (min-width: 38em) { 242 | .heading { 243 | padding: 3rem 3rem 2.5rem; 244 | } 245 | } 246 | 247 | .section { 248 | border-bottom: 1px solid #dfe1e8; 249 | } 250 | 251 | 252 | /* 253 | * Footer 254 | */ 255 | 256 | .footer { 257 | padding: 3rem 1rem; 258 | font-size: 90%; 259 | text-align: center; 260 | } 261 | .footer p { 262 | margin-bottom: .5rem; 263 | } 264 | 265 | .quick-links { 266 | list-style: none; 267 | margin-left: 0; 268 | } 269 | .quick-links li { 270 | display: inline; 271 | } 272 | 273 | 274 | /* 275 | * Syntax highlighting 276 | */ 277 | 278 | {% include syntax.css %} 279 | 280 | --------------------------------------------------------------------------------