<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/inwts-guidelines-for-r-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.inwt-statistics.com/read-blog/inwts-guidelines-for-r-code.html"> INWT-Blog-RBloggers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>“It turns out that style matters in programming for the same reason that it matters in writing. It makes for better reading.“ </em><strong>Douglas Crockford in <a rel="nofollow" href="http://shop.oreilly.com/product/9780596517748.do" target="_blank">JavaScript: The Good Parts</a></strong></p>
</div>
<div>
<p>Why do we need yet another style guide?</p>
</div>
<div>
<p><em>“The reason to care about a style guide is just one thing: We want that our source code is not only interpretable by a computer but also by a human.“</em></p>
</div>
<div>
<p>If you start to write not only for yourself but also for others, you do not want to lose your readers. You have to start to care about format, style and clarity. The format and partially the style can be addressed by sticking to a style guide. However, your code is not becoming prose just like that. If an author does not care if her code is readable, even a style guide won’t help. On the contrary, if you do care about readability, format and style are the easiest things to get started with and it will have an immediate impact.</p>
<p>When you begin to care about readability, here is a short list of benefits:</p>
<ul>
<li>Your colleagues will like you and are happy to pick your work up where you left off. Well, not quite, but we can get there.</li>
<li>Errors are easier to spot.</li>
<li>When you review your own source code you will fewer and fewer ask yourself: Who wrote this? And what does it mean?</li>
</ul>
<p>When you decide to stick to any style guidelines, be aware that it is not that important which one you pick. What is important is that you and your team can agree on sticking to it. There are two popular style guides for R. One is <a rel="nofollow" href="https://google.github.io/styleguide/Rguide.xml" target="_blank">the style guide from Google</a> and the other one can be found in <a rel="nofollow" href="http://adv-r.had.co.nz/Style.html" target="_blank">Advanced R by Hadley Wickham</a>.</p>
<p>We deviate only on some specific rules from these guides. Furthermore, we have decided to make them mandatory. Every time code is committed into a git repository, a CI tool will automatically check for any style violations. Another component is a template for a typical script which can be found below. We also implemented most of these rules and some more ideas in a dedicated package INWTUtils which can be found in <a rel="nofollow" href="https://github.com/INWT/INWTUtils" title="INWTUtils Github Repository" target="_blank">its GitHub repository</a>.</p>
</div>
<div>
<h2>Naming convention</h2>
<p>In contrast to the two style guides mentioned before, we decided to use lowerCamelCase for most identifiers. Underscores and dots are not allowed in usual object names. There is one case where you could deviate from this convention: By naming GLOBAL_VARIABLES – they are only used in scripts and only defined at the top – in upper case with underscores, they can be found at a glance.</p>
</div>
<div>
<h2>Automatic style checking</h2>
<p>If you want to go a step further, you can even check your R code for deviations from style rules with automated checks. The lintr package contains useful functions to run such checks. You can apply these checks to a package or a single file. While doing so, you can choose exactly which rules you want to test. Our own checks are:</p>
<ul>
<li>Use &lt;-for assignment, not =</li>
<li>Spaces should be after all commas, but not before</li>
<li>Operators like +, -, =, … should be surrounded by spaces</li>
<li>The line length should not exceed a certain number of characters, e.g., 80 or 100</li>
<li>Spaces should be used instead of tabs</li>
<li>Object names should not be too long</li>
<li>Left parentheses should have a space before, except for function calls</li>
<li>Remove empty blank lines at the end of the file</li>
</ul>
<p>We also wrote a few of our own checks which can then be easily integrated with the package:</p>
<ul>
<li>Function arguments without default should be listed before arguments with default</li>
<li>Avoid double spaces (except for indents)</li>
<li>Don’t use <em>setwd</em> or <em>source</em> in package functions – this can lead to unexpected side effects</li>
<li>Remove whitespaces at the end of lines</li>
<li>We want to force ourselves not to use internal functions from one of our own packages (via <em>pkg:::foo</em>). This would be a hint that we should rather write a documentation for that function and export it. This rule applies only to scripts, not to package functions.</li>
</ul>
<p>If you want to get started, we recommend you to install the package <em>lintr</em> and read the help for <em>lintr::lint</em>.</p>
<p>In the beginning, it may be hard to follow these style rules, especially if they have been chosen by someone else and are not in line with your personal preferences. But eventually, it will help you to write cleaner code. In addition, you may adapt a consistent coding style in your team, which makes cooperation a bit more comfortable.</p>
</div>
<h2> The structure of a script</h2>
<div>
<h3>Header</h3>
<p>It’s a good idea to start a script with a header including the purpose, the files it needs as input, and the output it produces. In addition, the header should list the author’s name and email address. This can also be an incentive to take on ownership: You may look at your code more carefully if your name is written above it.</p>
</div>
<div>
<h3>Cleaning up the workspace</h3>
<p>The first step is to clean up your workspace, e.g., removing all objects. Otherwise it could happen that your code uses an object from the workspace which will not be available anymore at a later point.</p>
</div>
<div>
<h3>Cleaning up the search path</h3>
<p>The search path contains all packages that are attached, that is, whose functions you can access directly. Some packages may be attached and you are not aware of them anymore. If you want to keep control, it’s better to know what is on your search path. A useful function for this task is <em>rmPkgs</em> from the <em>INWTUtils</em> package. It will put your search path back into the state of a fresh R session. Afterwards you can attach the packages you need for the analysis via <em>library</em> and execute source statements.</p>
</div>
<div>
<h3>Contents</h3>
<p>Now you can start with the actual analysis. First, declare your global variables, if any. By declaring all global variables at the top of your code you will always find them quickly and not overlook any of them. To obtain a clear structure, you can divide your code into numbered sections (e.g. “loading data”, “data preparation”, “model estimation”, etc.). This may seem quite trivial, but it helps to get an overview very quickly. Also modern editors may provide the possibility to collapse regions of code. This can further help to browse a file.</p>
</div>
<pre><pre><code> ################################################################################ # This script is a template for the general structure of R scripts. This first # # header should describe what the script does, which input files it needs and  # # which output it produces. This header also contains name and e-mail adress   # # of the author.                                                               # #                                                                              # # Author: Mira Céline Klein                                                    # # E-mail: mira-klein@inwt-statistic.de                                         # ################################################################################ # 00 Preparation --------------------------------------------------------------- # Clean up workspace rm(list = ls(all.names = TRUE)) # Clean up search path INWTUtils::rmPkgs() # Load packages library(INWTUtils) # Define global variables A_GLOBAL_VARIABLE &lt;- 4 # Upper case and underlines for global variables # 01a Load data ---------------------------------------------------------------- exampleVector &lt;- 1:3 # lowerCamelCase for ordinary object names # 01b Prepare data ------------------------------------------------------------- # 02 ... ----------------------------------------------------------------------- </code></pre></pre>
<div>
<p><span>What articles you might also be interested in:</span></p>
<ul>
<li><a rel="nofollow" href="https://www.inwt-statistics.com/read-blog/a-meaningful-file-structure-for-r-projects.html" title="https://www.inwt-statistics.com/read-blog/a-meaningful-file-structure-for-r-projects.html" target="_blank">A meaningful file structure for R projects</a></li>
</ul></div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.inwt-statistics.com/read-blog/inwts-guidelines-for-r-code.html"> INWT-Blog-RBloggers</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/ErkrUKqUTKQ" height="1"></a></div>