<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/the-making-of-datasaurus/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://itsalocke.com/the-making-of-datasaurus/"> R – Locke Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div></div>
<p>Around 8:30pm I saw this tweet and duly retweeted</p>
<div class="twitter-tweet">
<blockquote data-lang="en" class="twitter-tweet">
<p dir="ltr" lang="en">&lt;throws away Anscombe quartet> <a target="_blank" rel="nofollow" href="https://t.co/WuyU9D6npK">https://t.co/WuyU9D6npK</a></p>
<p>— Richie Cotton (@richierocks) <a target="_blank" rel="nofollow" href="https://twitter.com/richierocks/status/859126142405677056">May 1, 2017</a></p>
</blockquote>
</div>
<p>It turns out awesome folks, <a target="_blank" rel="nofollow" href="https://www.autodeskresearch.com/people/george-fitzmaurice">George</a> and <a target="_blank" rel="nofollow" href="https://www.autodeskresearch.com/people/justin-matejka">Justin</a>, had made a process whereby they can <a target="_blank" rel="nofollow" href="https://www.autodeskresearch.com/publications/samestats">generate different distributions of points</a> that retain the same summary statistics. They used this process for making some friends for Dino the Datasaurus who was created by <a target="_blank" rel="nofollow" href="http://www.thefunctionalart.com/">Alberto Cairo</a>. They made the data for Dino and the rest of the Datasaurus Dozen available for download.</p>
<p>The data seemed like an ideal thing to get into R, so it was R package development time!</p>
<p>I know a lot of folks can be a bit scared of producing packages so I thought I’d share the workflow so folks can see how easy the actual package bits and bobs can be.</p>
<h2>Starting a package</h2>
<p>For a long time I’ve been using <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=devtools"><code>devtools</code></a> to make package building easier, and indeed I still do. However, I wanted to be even lazier so I didn’t even have to type out the ~10 lines of code to set up a best practice package. To be lazy, I made a package called <a target="_blank" rel="nofollow" href="https://github.com/lockedata/pRojects"><code>pRojects</code></a>. <code>pRojects</code> helps with the initial setup of different project types, including packages. It’s still under development and it’s a great place to cut your teeth on contributing to a package.</p>
<p>To start the <a target="_blank" rel="nofollow" href="https://github.com/stephlocke/datasauRus"><code>datasauRus</code></a> package I just did:</p>
<pre><code>install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;lockedata/pRojects&quot;)
pRojects::createPackageProject(&quot;datasauRus&quot;)
</code></pre>
<blockquote><p>
Well, I’ve already got those prerequisites so I only did 1 line!
</p>
</blockquote>
<p>Then some (optional) online stuff is required to complete the awesomeness:</p>
<ol>
<li><a target="_blank" rel="nofollow" href="https://help.github.com/articles/creating-a-new-repository/">Create a new repository on github</a></li>
<li><a target="_blank" rel="nofollow" href="https://travis-ci.org/getting_started">Turn on travis continuous integration</a></li>
<li><a target="_blank" rel="nofollow" href="https://coveralls.zendesk.com/hc/en-us">Turn on coveralls</a></li>
</ol>
<p>Then we need to add github repository to our project. I use the git command line for this:</p>
<pre><code>git remote add origin git@github.com:stephlocke/datasauRus.git
git push --set-upstream origin master
</code></pre>
<p>With just these things, I have a package that contains the unit test framework, documentation stubs, continuous integration and test coverage, and source control.</p>
<p>That is all you need to do to get things going!</p>
<h2>Adding package contents</h2>
<p>I’m not going to take you through an in-depth piece on writing package contents as Hadley does that extremely well in <a target="_blank" rel="nofollow" href="http://r-pkgs.had.co.nz/">R packages</a> (or get the <a target="_blank" rel="nofollow" href="http://geni.us/rpkgs">book</a>). This package only requires datasets to be built.</p>
<p><a target="_blank" rel="nofollow" href="https://github.com/LucyMcGowan">Lucy McGowan</a> made a PR overnight that converted the raw data (stored in <code>inst/extdata/</code>) into R datasets (stored in <code>data</code>) with this R code:</p>
<pre><code># #here is how I converted, we can delete this<br>
# files =  list.files(path = &quot;./inst/extdata&quot;,pattern=&quot;*.tsv&quot;, full.names = TRUE)<br>
#<br>
# purrr::walk(files, function(x){<br>
#   nm &lt;- gsub(&quot;\\.tsv&quot;,&quot;&quot;,basename(x))<br>
#   nm &lt;- gsub(&quot;-&quot;,&quot;_&quot;,nm)<br>
#<br>
#   #make it slither<br>
#   nm &lt;-  gsub(&quot;^_+&quot;, &quot;&quot;, tolower(gsub(&quot;([A-Z])&quot;, &quot;_\\1&quot;, nm)))<br>
#<br>
#   if (grepl(&quot;wide&quot;,nm)){<br>
#     header1 &lt;- scan(x, nlines = 1, what = character())<br>
#     header1 &lt;- paste(header1,c(&quot;x&quot;,&quot;y&quot;),sep=&quot;_&quot;)<br>
#     .dat &lt;- readr::read_tsv(x, col_names = header1, skip = 2)<br>
#   } else {<br>
#     .dat &lt;- readr::read_tsv(x)<br>
#   }<br>
#   assign(nm, .dat)<br>
#   save(list = nm, file = paste0(&quot;data/&quot;,nm,&quot;.rda&quot;))<br>
# })<br>

</code></pre>
<p>This made <code>.Rda</code> files that would now be loaded when the package installed. Lucy also added minimal documentation for each file too in the R package doc created by default in the package setup phase. Each data entry looks like:</p>
<pre><code>#'Datasaurus Dozen (wide) data
&quot;datasaurus_dozen_wide&quot;
</code></pre>
<h2>Adding tests</h2>
<p>We don’t have functions in this package but we do have datasets, so I wanted to write some unit tests for these.</p>
<p>Unit testing with <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=testthat"><code>testthat</code></a> is setup by default so I just needed to add a file beginning with <code>test-</code> into the <code>test/testthat/</code> directory that contained some tests about the data.</p>
<p>As the code would be repetitive, I made a function that could be applied to each dataset.</p>
<pre><code>context(&quot;datasets&quot;)

datashapetests&lt;-function(df, ncols, nrows, uniquecol=NULL, nuniques=NULL){
  expect_equal(ncol(df),ncols)
  expect_equal(nrow(df),nrows)
  if(!is.null(uniquecol))
  expect_equal(nrow(unique(df[uniquecol])),nuniques)
}

test_that(&quot;box_plots is correctly shaped&quot;,{
  datashapetests(box_plots,6,2484)
})

test_that(&quot;datasaurus_dozen is correctly shaped&quot;,{
  datashapetests(datasaurus_dozen,3,1846,&quot;dataset&quot;,13)
})
</code></pre>
<h2>Documentation</h2>
<p>I mentioned some of the minimally written dataset documentation. These should be expanded, and the <a target="_blank" rel="nofollow" href="https://github.com/stephlocke/datasauRus/issues/3">vignette needs filling in</a>. This could be based on the information I’ve already provided in the README. Once the documentation is polished, this package could go to CRAN.</p>
<h2>Run checks</h2>
<p>Once you’ve made some changes to your package, you should make sure your code passes it’s unit tests, that documentation is correctly structured, and that your code compiles. Remove as many warnings and notes as possibles.</p>
<p>Do this with <code>devtools</code>:</p>
<pre><code>devtools::check()
</code></pre>
<p>Travis CI will also verify your code passes these checks in a clean environment, and if you hooked up code coverage then you’ll see how much your tests test your codebase.</p>
<h2>That’s it</h2>
<p>There can be nuance in writing your own functions and coding defensively, but you can now make a great package skeleton in just one line of code. The <a target="_blank" rel="nofollow" href="https://github.com/stephlocke/datasauRus"><code>datasauRus</code></a> is almost ready to go to CRAN and it took less than 50 lines of code, including the package contents. I hope this has been some useful insight into R package development.</p>
<p>If you want to tackle making your first package, I’ll happily give you a hand – book into my <a target="_blank" rel="nofollow" href="https://calendly.com/lockedata/officehours">office hours</a> and let’s do it!</p>
<p>The post <a target="_blank" rel="nofollow" href="http://itsalocke.com/the-making-of-datasaurus/">The making of datasauRus</a> appeared first on <a target="_blank" rel="nofollow" href="http://itsalocke.com/">Locke Data</a>. Locke Data are a data science consultancy aimed at helping organisations get ready and get started with data science.</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="http://itsalocke.com/the-making-of-datasaurus/"> R – Locke Data</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/JeG9hJw8UX8"></a></div>