<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/review-of-efficient-r-programming/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/05/19/efficient_r_programming/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <br>
</p>
<p><br>
</p>
<div>
<img src="https://i2.wp.com/rviews.rstudio.com/post/2017-05-15_Efficient_R_Programming_files/cover.PNG?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>In the crowded market space of data science and R language books, Lovelace and Gillespie’s <a rel="nofollow" href="http://shop.oreilly.com/product/0636920047995.do" target="_blank">Efficient R Programming</a> (2016) stands out from the crowd. Over the course of ten comprehensive chapters, the authors address the primary tenets of developing efficient R programs. Unless you happen to be a member of the R core development team, you will find this book useful whether you are a novice R programmer or an established data scientist and engineer. This book is chock full of useful tips and techniques that will help you improve the efficiency of your R programs, as well as the efficiency of your development processes. Although I have been using R daily (and nearly exclusively) for the past 4+ years, every chapter of this book provided me with new insights into how to improve my R code while helping solidify my understanding of previously learned techniques. Each chapter of <strong>Efficient R Programming</strong> is devoted to a single topic, each of which includes a “top five tips” list, covers numerous packages and techniques, and contains useful exercises and problem sets for consolidating key insights.</p>
<p>In <strong>Chapter 1. Introduction</strong>, the authors orient the audience to the key characteristics of R that affect its efficiency, compared to other programming languages. Importantly, the authors address R efficiency not just in the expected sense of algorithmic speed and complexity, but broaden its scope to include programmer productivity and how it relates to programming idioms, IDEs, coding conventions, and community support – all things that can improve the efficiency of writing and maintaining code. This is doubly important for a language like R, which is notoriously flexible in its ability to solve problems in multiple ways. The first chapter concludes by introducing the reader to two valuable packages: (1) <a rel="nofollow" href="https://cran.r-project.org/web/packages/microbenchmark/microbenchmark.pdf" target="_blank">microbenchmark</a>, an accurate benchmarking tool with nanosecond precision; and (2) <a rel="nofollow" href="https://cran.r-project.org/web/packages/profvis/profvis.pdf" target="_blank">profvis</a>, a handy tool for profiling larger chunks of code. These two packages are repeatedly used throughout the remainder of the book to illustrate key concepts and highlight efficient techniques.</p>
<p>In <strong>Chapter 2. Efficient Setup</strong>, the reader is introduced to techniques for setting up a development environment that facilitates efficient workflow. Here the authors cover choices in operating system, R version, R start-up, alternative R interpreters, and how to maintain up-to-date packages with tools like <a rel="nofollow" href="https://cran.r-project.org/web/packages/packrat/packrat.pdf" target="_blank">packrat</a> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/installr/installr.pdf" target="_blank">installr</a>. I found their overview of the R startup process particularly useful, as the authors taught me how to modify my <strong>.Renviron</strong> and <strong>.Rprofile</strong> files to cache external API keys and customize my R environment, for example by adding alias shortcuts to commonly used functions. The chapter concludes by discussing how to setup and customize the <a rel="nofollow" href="https://www.rstudio.com/" target="_blank">RStudio</a> environment (e.g., modifying code editing preference, editing keyboard shortcuts, and turning off restore <strong>.Rdata</strong> to help prevent bugs), which can greatly improve individual efficiency.</p>
<p><strong>Chapter 3. Efficient Programming</strong> introduces the reader to efficient programming by discussing “big picture” programming techniques and how they relate to the R language. This chapter will most likely be beneficial to established programmers who are new to R, as well as to data scientists and analysts who have limited exposure to programming in a production environment. In this chapter the authors introduce the “golden rule of R programming” before delving into the usual suspects of inefficient R code. Usefully, the book illustrates multiple ways of performing the same task (e.g., data selection) with different code snippets, and highlights the performance differences through benchmarked results. Here we learn about the pitfalls of growing vectors, the benefits of vectorization, and the proper use of factors. The chapter wraps up with the requisite overview of the apply function family, before discussing the use of variable caching (package <a rel="nofollow" href="https://cran.r-project.org/web/packages/memoise/memoise.pdf" target="_blank">memoise</a>) and byte compilation as important techniques in writing fast, responsive R code.</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-05-15_Efficient_R_Programming_files/byte.PNG?w=456&ssl=1" data-recalc-dims="1">
</div>
<p><strong>Chapter 4. Efficient Workflow</strong> will be of primary use to junior-level programmers, analysts, and project managers who haven’t had enough time or practice to develop their own efficient workflows. This chapter discusses the importance of project planning, audience, and scope before delving into common tools that facilitate project management. In my opinion, one of best aspects of R is the huge, maddeningly broad number of packages that are available on <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a> and <a rel="nofollow" href="https://github.com/" target="_blank">GitHub</a>. The authors provide useful advice and techniques for identifying the packages that will be of most use to your project. A brief discussion on the use of R Markdown and knitr concludes this chapter.</p>
<p><strong>Chapter 5. Efficient Input/Output</strong> is devoted to efficient read/write operations. Anybody who has ever struggled with loading a big file into R for analysis will appreciate this discussion and the packages covered in this chapter. The <a rel="nofollow" href="https://cran.r-project.org/web/packages/rio/rio.pdf" target="_blank">rio</a> package, which can handle a wide variety of common data file types, provides a useful starting point for exploratory work on a new project. Other packages that are discussed (including <a rel="nofollow" href="https://cran.r-project.org/web/packages/readr/readr.pdf" target="_blank">readr</a> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/data.table/data.table.pdf" target="_blank">data.table</a>) provide more efficient I/O than those in base R. The chapter ends with a discussion of two new file formats and associated packages, (<a rel="nofollow" href="https://cran.r-project.org/web/packages/feather/feather.pdf" target="_blank">feather</a> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/RProtoBuf/RProtoBuf.pdf" target="_blank">RProtoBuf</a>), that can be used for cross-language, fast, efficient serialized data I/O.</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-05-15_Efficient_R_Programming_files/feather.PNG?w=456&ssl=1" data-recalc-dims="1">
</div>
<p><strong>Chapter 6. Efficient Data Carpentry</strong> introduces what are, in my opinion, the most useful R tools for <em>data munging</em> – what Lovelace and Gillespie prefer to call by the more admirable term “data carpentry.” This chapter could more aptly be titled the “Tidyverse” or the “Hadleyverse”, for most of the tools discussed in this chapter were developed by prolific R package writer, <a rel="nofollow" href="https://github.com/hadley" target="_blank">Hadley Wickham</a>. Sections of the chapter are devoted to each of the primary packages of the <a rel="nofollow" href="https://github.com/tidyverse" target="_blank">tidyverse</a>: <a rel="nofollow" href="https://cran.r-project.org/web/packages/tibble/tibble.pdf" target="_blank">tibble</a>, a more useful and user-friendly data.frame; <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidyr/tidyr.pdf" target="_blank">tidyr</a>, used for reshaping data between short and long forms; <a rel="nofollow" href="https://cran.r-project.org/web/packages/stringr/stringr.pdf" target="_blank">stringr</a>, which provides a consistent API over obtuse regex functions; <a rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/dplyr.pdf" target="_blank">dplyr</a>, used for efficient data processing including filtering, sorting, mutating, joining, and summarizing; and of course <a rel="nofollow" href="https://cran.r-project.org/web/packages/magrittr/magrittr.pdf" target="_blank">magrittr</a>, for piping all these operations together with <code>%>%</code>. A brief section on package <a rel="nofollow" href="https://cran.r-project.org/web/packages/data.table/data.table.pdf" target="_blank">data.table</a> rounds out the discussion on efficient data carpentry.</p>
<p><strong>Chapter 7. Efficient Optimization</strong> begins with the requisite optimization quote by computer scientist Donald Knuth:</p>
<blockquote>
<p>The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.</p>
</blockquote>
<p>In this chapter, the authors introduce <a rel="nofollow" href="https://cran.r-project.org/web/packages/profvis/profvis.pdf" target="_blank">profvis</a>, and they illustrate the utility of this package by showing how it can be used to identify bottlenecks in a Monte Carlo simulation of a Monopoly game. The authors next examine alternative methods in base R that can be used for greater efficiency. These include discussion of <code>if()</code> vs. <code>ifelse()</code>, sorting operations, AND (<code>&amp;</code>) and OR (<code>|</code>) vs. <code>&amp;&amp;</code> and <code>||</code>, row/column operations, and sparse matrices. The authors then apply these tricks to the Monopoly code to show a 20-fold decrease in run time. The chapter concludes with a discussion and examples of parallelization, and the use of <a rel="nofollow" href="https://cran.r-project.org/web/packages/Rcpp/Rcpp.pdf" target="_blank">Rcpp</a> as an R interface to underlying fast and efficient C++ code.</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-05-15_Efficient_R_Programming_files/profvis.PNG?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>I found the chapter <strong>Efficient Hardware</strong> to be the least useful in the book (spoiler alert: add more RAM or migrate to cloud-based services), though the chapter on <strong>Efficient Collaboration</strong> will be particularly useful for novice data scientists lacking real-world experience developing data artifacts and production applications in a distributed, collaborative environment. In this chapter, the authors discuss the importance of coding style, code comments, version control, and code review. The final chapter <strong>Efficient Learning</strong>, will find appreciative readers among those just getting started with R (and if this describes you, I would suggest that you start with this chapter first!). Here the authors discuss using and navigating R’s excellent internal help utility, as well as the importance of vignettes and source code in learning/understanding. After briefly introducing <a rel="nofollow" href="https://cran.r-project.org/web/packages/swirl/swirl.pdf" target="_blank">swirl</a>, the book concludes with a discussion of online resources, including <a rel="nofollow" href="http://stackoverflow.com/" target="_blank">Stack Overflow</a>; the authors thankfully provide the newbie with important information on how to ask the right questions and the importance of providing a <a rel="nofollow" href="http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example" target="_blank">great R reproducible example</a>.</p>
<p>In summary, Lovelace and Gillespie’s <strong>Efficient R Programming</strong> does an admirable job of illustrating the key techniques and packages for writing efficient R programs. The book will appeal to a wide audience from advanced R programmers to those just starting out. In my opinion, the book hits that pragmatic sweet spot between breadth and depth, and it usefully contains links to external resources for those wishing to delve deeper into a specific topic. After reading this book, I immediately went to work refactoring a <a rel="nofollow" href="https://shiny.rstudio.com/" target="_blank">Shiny</a> dashboard application I am developing and several internal R packages I maintain for our data science team. In a matter of a few short hours, I witnessed a 5 to 10-fold performance increase in these applications just by implementing a couple of new techniques. I was particularly impressed with the greatly improved end-user performance and the ease with which I implemented intelligent caching with the <a rel="nofollow" href="https://cran.r-project.org/web/packages/memoise/memoise.pdf" target="_blank">memoise</a> package for a consumer decision tree application I am developing. If you care deeply about writing beautiful, clean, efficient code and bringing your data science to the next level, I highly recommend adding <strong>Efficient R Programming</strong> to your arsenal.</p>
<p>The book is published by <a rel="nofollow" href="https://www.oreilly.com/" target="_blank">O’Reilly Media</a> and is available <a rel="nofollow" href="https://csgillespie.github.io/efficientR/" target="_blank">online at the authors’ website</a>, as well as through <a rel="nofollow" href="https://www.safaribooksonline.com/home/" target="_blank">Safari</a>.</p>
<p> </p>
<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://rviews.rstudio.com/2017/05/19/efficient_r_programming/"> R Views</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/3hIkyOcJUHk" height="1"></a></div>