<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/rs-testing-predicament/"></div></div><div><a data-url="https://www.r-bloggers.com/rs-testing-predicament/" data-counturl="https://www.r-bloggers.com/rs-testing-predicament/" data-count="horizontal" data-text="R’s Testing Predicament" 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="http://www.mango-solutions.com/wp/2016/10/rs-testing-predicament/"> Mango Solutions » R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4><em>Joe Russell</em></h4>
<p>If you were to ask any R-user for the reason for R’s success, you’re almost guaranteed to hear the words “open source”.  As the second most popular open source language (behind Python) R has exploded in popularity in recent years. This, however, brings with it challenges that must be addressed.</p>
<p>Here at Mango we have written our own unit tests for many packages and bundled them together to create what we call ValidR. Essentially it creates a single fully tested instance of R, enabling R to be used in companies with strict regulatory requirements. It is, therefore, especially popular within the pharmaceutical industry.</p>
<p>As a part of the ValidR process, we like to look at the tests written by package authors themselves. Through some simple coding in R (which I have included below), we find that the number of packages currently available as downloads from CRAN sits at a bewildering 9,231. Delving a little deeper, we find that these have been created by an equally bewildering 7,882 unique authors. It is impressive that CRAN is able to ensure that all of these authors are writing their packages accurately and consistently maintaining them, let alone including sufficient testing.</p>
<p>Unfortunately, this is not the case. If we search, using some beloved <em>dplyr</em>, for packages in CRAN that incorporate some form of unit testing, we come to a shockingly low figure of just 17%.  For a coding language that is the predominant choice of users with statistical backgrounds, perhaps this is not surprising and perhaps something that many of us do not deem concerning. However this is something that will make people believe that R cannot be trusted, harming its success and, inevitably, our own as R users. This is an issue that must be resolved, so what is being done?</p>
<p>Gabor Csardi, one of the Senior Consultants here at Mango, is currently writing a package named ‘goodPractice’ (a work in progress, but available on GitHub if you want to take a look). This package is designed to advise package authors on how best to write their code, whether this is certain functions that shouldn’t be used or syntax best avoided.</p>
<p>Ideally, projects such as goodPractice and ValidR would not be necessary, and hopefully in the future they will become obsolete. However, for now they serve a key role in ensuring R’s long term success and pave a way for improving R’s testing standards.</p>
<hr>
<pre># Script counts the number of packages on CRAN that use a formal unit testing framework
library(dplyr)

# What's on CRAN?
download.file(&quot;http://cran.R-project.org/web/packages/packages.rds&quot;,
&quot;packages.rds&quot;, mode=&quot;wb&quot;)
cranPacks &lt;- readRDS(&quot;packages.rds&quot;)

#Number of unique package authors
authors &lt;- cranPacks[,17]
numAuthors &lt;- length(unique(authors))

# Reduce the size down a bit to keep just the interesting columns
cranPacks &lt;- as.data.frame(cranPacks)[, 1:7]

# Just the packages that use the formal testing framework
unitTestFramework &lt;- cranPacks %>%
filter(grepl(&quot;testthat&quot;, Depends) | grepl(&quot;testthat&quot;, Imports) | grepl(&quot;testthat&quot;, Suggests) |
grepl(&quot;RUnit&quot;, Depends) | grepl(&quot;RUnit&quot;, Imports) | grepl(&quot;RUnit&quot;, Suggests) |
grepl(&quot;svUnit&quot;, Depends) | grepl(&quot;svUnit&quot;, Imports) | grepl(&quot;svUnit&quot;, Suggests) |
grepl(&quot;testit&quot;, Depends) | grepl(&quot;testit&quot;, Imports) | grepl(&quot;tesit&quot;, Suggests) |
Package %in% c(&quot;testthat&quot;, &quot;RUnit&quot;, &quot;svUnit&quot;, &quot;testit&quot;))

# Proportion of packages that use a testing framework
nrow(unitTestFramework) / nrow(cranPacks)

## [1] 0.1712033
</pre>
<p> </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/rs-testing-predicament/"></div></div><div><a data-url="https://www.r-bloggers.com/rs-testing-predicament/" data-counturl="https://www.r-bloggers.com/rs-testing-predicament/" data-count="vertical" data-text="R’s Testing Predicament" 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="http://www.mango-solutions.com/wp/2016/10/rs-testing-predicament/"> Mango Solutions » R Blog</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/zp1t_4KCB40"></a></div>