<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/"></div></div><div><a data-url="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/" data-counturl="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/" data-count="horizontal" data-text="Writing Good R Code and Writing Well" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rstudio.com/rviews/2016/12/02/writing-good-r-code-and-writing-well/"> RStudio</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p>by Joseph Rickert</p>
<p>If you are aspiring to write good R code, you may find it helpful to occasionally spend some time reading about writing: reading about writing R code, and reading about writing about R code that you’ve written. (If you write some excellent R code, you will likely have the opportunity to write about it.)</p>
<p>For reading about writing good R code, a place to start might be one of the many R style guides available. Hadley Wickham includes a <a target="_blank" rel="nofollow" href="http://adv-r.had.co.nz/Style.html">style guide</a> in his Advanced R Book that is short and sweet and based on the only slightly less concise <a target="_blank" rel="nofollow" href="https://google.github.io/styleguide/Rguide.xml">Google’s R Style Guide</a>. Also have a look at Graham Williams <a target="_blank" rel="nofollow" href="http://handsondatascience.com/StyleO.pdf">Sharing R Code – With Style</a>, a hybrid tutorial / style guide that is part of his <a target="_blank" rel="nofollow" href="http://togaware.com/onepager/">HandsOnDataScience</a> work in progress. In his elegant introduction, Graham writes:</p>
<blockquote><p>Data scientists write programs to ingest, manage, wrangle, visualise, analyse and model data in many ways. It is an art to be able to communicate our explorations and understandings through a language, albeit a programming language. Of course our programs must be executable by computers but computers care little about our programs except that they be syntactically correct. Our focus should be on engaging others to read and understand the narratives we present through our programs.</p>
</blockquote>
<p>Along these lines, Laurent Gatto offers his ideas on “better R programming in terms of cleaner and elegant syntax …” in his twenty-page tutorial <a target="_blank" rel="nofollow" href="https://www.bioconductor.org/help/course-materials/2013/CSAMA2013/friday/afternoon/R-programming.pdf">Writing better R code</a>.</p>
<p>If you are just beginning to write R functions, you may find Slawa Rokicki’s post on <a target="_blank" rel="nofollow" href="https://www.r-bloggers.com/how-to-write-and-debug-an-r-function/">How to write and debug an R function</a> or Cosma Shalizi’s write-up on <a target="_blank" rel="nofollow" href="http://www.stat.cmu.edu/~cshalizi/402/programming/writing-functions.pdf">Writing R Functions</a> helpful. If you do take the trouble to work through one of these style guides, please do adhere to at least the first rule of veteran programmer Diomidis Spinellis’ <a target="_blank" rel="nofollow" href="http://www.informit.com/articles/article.aspx?p=2223710">15 Rules for Writing Quality Code</a>.</p>
<p>Finally, I’m for taking Graham Williams seriously: programming is an art of communication, and I believe that practicing this art may also involve occasionally writing engaging narrative about programs. Here are a few short but valuable guides to writing well. William Zinsser is the master of explaining the art of clear and unaffected writing. His <a target="_blank" rel="nofollow" href="http://www.openculture.com/2015/05/10-writing-tips-from-legendary-writing-teacher-william-zinsser.html">10 Writing Tips</a> may lead you to spend some time with his book. Also worth a look are the writing tips from <a target="_blank" rel="nofollow" href="http://blog.uncollege.org/learn-to-write-well">Morgan Ostrowsky</a>, <a target="_blank" rel="nofollow" href="http://writetodone.com/how-to-write-well-self-editing-tips/">C.S Lakin</a>, and the <a target="_blank" rel="nofollow" href="http://learning.blogs.nytimes.com/2012/09/20/writing-rules-advice-from-the-new-york-times-on-writing-well/?_r=0">New York Times’</a> Amanda Christy Brown and Katherine Schulten. Three of these four writing coaches advise would-be writers to read, read widely beyond their area of expertise, and read the masters. With regard to this advice, I’ve found this short video by novelist Yaan Martel inspiring.</p>
<p><iframe height="315" width="560" allowfullscreen src="https://www.youtube.com/embed/IR9Av-TzSV4"></iframe></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/"></div></div><div><a data-url="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/" data-counturl="https://www.r-bloggers.com/writing-good-r-code-and-writing-well/" data-count="vertical" data-text="Writing Good R Code and Writing Well" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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.rstudio.com/rviews/2016/12/02/writing-good-r-code-and-writing-well/"> RStudio</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oKYHV3jIvug"></a></div>