<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/"></div></div><div><a data-url="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/" data-counturl="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/" data-count="horizontal" data-text="A link that can tell more than dozens of lines of R code – what’s new in archivist?" 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://smarterpoland.pl/index.php/2016/07/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/"> SmarterPoland.pl » English</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Can you spot the difference between this plot:</p>
<p><img src="http://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/07/Screen-Shot-2016-07-28-at-15.10.06-1024x921.png?w=456" data-recalc-dims="1"></p>
<p>And this one:</p>
<p><img src="http://i0.wp.com/smarterpoland.pl/wp-content/uploads/2016/07/Screen-Shot-2016-07-28-at-15.09.59-1024x938.png?w=456" data-recalc-dims="1"></p>
<p>You are right! The latter has an embedded piece of R code.<br>
What for?</p>
<p>It’s a call to a function aread from <a target="_blank" href="https://cran.r-project.org/web/packages/archivist/index.html">archivist</a> – a package that manages external copies of R objects. This piece of code was added by the function <a target="_blank" href="https://rawgit.com/pbiecek/archivist/master/scripts/ahistrory_and_terrorism.html">addHooksToPrint()</a>, that enriches knitr reports in links to all objects of a given class, e.g. ggplot. </p>
<p>You can copy this hook to your R session and you will automagically recreate this plot in your local session.</p>

<pre>archivist::aread('pbiecek/SmarterPoland_blog/arepo/e44de65f1e56ea42d2df2598c083d1ce')</pre>

<p>But it’s not all.<br>
Actually here the story is just beginning. </p>
<p>Don’t you think, that this plot is badly annotated? It is not clear what is being presented. Something about terrorism, but for which year, are these results for all countries or there is some filtering? What is on axes? Why the author skip all these important information? Why he does not include the full R code that explains how this plot was created?</p>
<p>Actually, having this single link you can get answers for all these questions.</p>
<p>First, let’s download the plot and extract the data out of it.</p>

<pre>pl &lt;- archivist::aread('pbiecek/SmarterPoland_blog/arepo/e44de65f1e56ea42d2df2598c083d1ce')
digest::digest(pl$data)
## ceed21e997efd00940cdbcba497559c7</pre>

<p>This data object is also in the repository so I can download it with the aread function.</p>

<pre>dat &lt;- archivist::aread('pbiecek/SmarterPoland_blog/arepo/ceed21e997efd00940cdbcba497559c7') head(dat)
#          country_txt sum_kills sum_wounds    n
# 1        Afghanistan      6208       6958 1926
# 2            Algeria        21         19   16
# 3            Bahrain         5         22   18
# 4         Bangladesh        76        695  465
# 5 Bosnia-Herzegovina         4          6    6
# 6       Burkina Faso         6          9    5</pre>

<p>But here is the coolest part.<br>
Having an object one can (in some cases) examine the history of this objects, i.e. check how it was created. Here is how to do this:</p>

<pre>archivist::ahistory(md5hash = 'pbiecek/SmarterPoland_blog/arepo/ceed21e997efd00940cdbcba497559c7')

#   small_data                           [d2ad05ac3e93aeaca02f57aa4f9f58bf]
#-> dplyr::filter(iyear == &quot;2015&quot;)       [01205474e0515ad29d3bae33ad4ba821]
#-> group_by(country_txt)                [e0d9c060107803889fbc7ffdea7a23f7]
#-> dplyr::summarise(sum_kills = sum(nkill, na.rm = TRUE), 
#                     sum_wounds = sum(nwound, na.rm = TRUE), 
#                     n = n())           [a78cf8a8e9cf10bdb1158af38422723d]
#-> dplyr::filter(sum_kills > 2, 
#                 sum_wounds > 2)        [ceed21e997efd00940cdbcba497559c7]</pre>

<p>Now you can see what operations have been used to create data used in this plot. It’s clear how the aggregation has been done, what is the filtering condition and so on.<br>
Also you have hashes to all objects created along the way, co you can download the partial results. This history is being recorded with an operator <code>%a%</code> that is working in a similar fashion to <code>%>%</code>.</p>
<p>We have the plot, now we now what is being presented, let’s change some annotations.</p>

<pre>pl + ggtitle(&quot;Victims of terrorism in 2015\nCountries with > 2 Fatalities&quot;) + theme_bw()</pre>

<p>The <a target="_blank" href="https://rawgit.com/pbiecek/archivist/master/scripts/ahistrory_and_terrorism.html">ahistory() </a>function for remote repositories was introduced to archivist in version 2.1 (on CRAN since yesterday). Other new feature is the support for repositories in shiny applications. Now you can enrich your app in links to copies of R objects generated by shiny.<br>
You can find more information about these and other features in the useR2016 presentation about archivist (<a target="_blank" href="https://rawgit.com/pbiecek/archivist/master/presentations/useR2016.html">html</a>, <a target="_blank" href="https://channel9.msdn.com/Events/useR-international-R-User-conference/useR2016/How-to-use-the-archivist-package-to-boost-reproducibility-of-your-research">video</a>).<br>
Or look for Marcin Kosiński talk during the <a target="_blank" href="http://smarterpoland.pl/index.php/2016/07/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/erum.ue.poznan.pl">european R users meeting</a> in Poznań.</p>
<p>The data presented in here is just a small fraction of data from National Consortium for the Study of Terrorism and Responses to Terrorism (START) (2016) retrieved from <a target="_blank" href="http://www.start.umd.edu/gtd">http://www.start.umd.edu/gtd</a>.</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="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/"></div></div><div><a data-url="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/" data-counturl="http://www.r-bloggers.com/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/" data-count="vertical" data-text="A link that can tell more than dozens of lines of R code – what’s new in archivist?" 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://smarterpoland.pl/index.php/2016/07/a-link-that-can-tell-more-than-dozens-of-lines-of-r-code-whats-new-in-archivist/"> SmarterPoland.pl » English</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/aHVwZBEZrY0"></a></div>