<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/shiny-archivist-reproducible-interactive-exploration/"></div></div><div><a data-url="http://www.r-bloggers.com/shiny-archivist-reproducible-interactive-exploration/" data-counturl="http://www.r-bloggers.com/shiny-archivist-reproducible-interactive-exploration/" data-count="horizontal" data-text="Shiny + archivist = reproducible interactive exploration" 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/06/shiny-archivist-reproducible-interactive-exploration/"> SmarterPoland.pl » English</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img align="right" src="http://i0.wp.com/smarterpoland.pl/wp-content/uploads/2016/06/download-300x281.jpeg?w=300" data-recalc-dims="1"><br>
Shiny is a great tool for interactive exploration (and not only for that). But, due to its architecture, all objects/results that are generated are stored in a separate R process so you cannot access them easily from your R console. </p>
<p>In some cases you may wish to retrieve a model or a plot that you have just generated. Or maybe just wish to store all R objects (plots, data sets, models) that have been ever generated by your Shiny application. Or maybe you would like to do some further tuning or validation of a selected model or plot. Or maybe you wish to collect and compare all lm() models ever generated by your app? Or maybe you would like to have an R code that will recover given R object in future.</p>
<p><strong>So, how to do this?</strong></p>
<p><center><a target="_blank" href="https://gist.github.com/pbiecek/f8a644497d3ed378d9a73f65be629240"><img src="http://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/06/Screen-Shot-2016-06-26-at-10.00.27-1024x651.png?resize=456%2C293" data-recalc-dims="1"></a></center></p>
<p><span></span></p>
<p>Here I am going to present a solution based on a combination of archivist + shiny.<br>
<a target="_blank" href="http://pbiecek.github.io/archivist/">Archivist</a> is a package for management of R objects. It allows you to create a repository (local folder or git repo) and push all interesting R objects to it. Each object will be stored along with its metadata. Each object will get it’s unique key, i.e. md5hash.<br>
Then you or your collaborators may access these objects from any R process, local or remote.</p>
<p>So how to extract an R object from shiny to your local R console?<br>
1.	In the app create an archivist repository with archivist::createLocalRepo().<br>
2.	In the app push selected objects from shiny to this repository with archivist::saveToLocalRepo().<br>
3.	In the app print links to these objects somewhere in the shiny UI.<br>
4.	In the console, read the R object from the repository with archivist::aread().</p>
<p>Below you will find an example shiny application, that creates a repo and stores all plots in this repo. Also it’s printing hooks to selected objects in a figure caption.</p>

<pre>library(shiny)
library(archivist)
library(ggplot2)

# [a] archivist repo
# create if not exist, or set as default if exists
if (!file.exists(&quot;arepo&quot;)) {
  createLocalRepo(&quot;arepo&quot;, default = TRUE)
} else {
  setLocalRepo(&quot;arepo&quot;)
}

# UI with plot and archivist hooks
ui &lt;- shinyUI(fluidPage(
   titlePanel(&quot;Example integration of shiny and archivist&quot;),
   sidebarLayout(
      sidebarPanel(
        selectInput(&quot;var1&quot;, &quot;Variable on OX:&quot;, colnames(iris), &quot;Sepal.Length&quot;),
        selectInput(&quot;var2&quot;, &quot;Variable on OY:&quot;, colnames(iris), &quot;Sepal.Width&quot;)
      ),
      
      mainPanel(
         plotOutput(&quot;plotIt&quot;),
         uiOutput(&quot;printArchivistHooks&quot;)
      )
   )
))

server &lt;- shinyServer(function(input, output) {
  # create a plot   
  createPlot &lt;- reactive({
    var1 &lt;- input$var1
    var2 &lt;- input$var2
    ggplot(iris, aes_string(var1, var2, color=&quot;Species&quot;)) +
      geom_point() + geom_smooth(method=&quot;lm&quot;, se=FALSE)
  })
  
  # just plot the plot
  output$plotIt &lt;- renderPlot({
    createPlot()
  })
  # add the plot to repository and print it's link
  output$printArchivistHooks &lt;- renderUI({
    pl &lt;- createPlot()
    hash &lt;- saveToLocalRepo(pl)
    HTML(paste0(&quot;&lt;i>Link to the plot:&lt;/i>&lt;br/>&lt;code>archivist::aread('&quot;,hash,&quot;')&lt;/code>&quot;))
  })
})

# Run the application 
shinyApp(ui = ui, server = server)</pre>

<p>In order to read a single R object from default repo you can use <code>aread()</code> function, like</p>
<pre>archivist::setLocalRepo('arepo')
archivist::aread('82560bb87decd327edcd1c875e87aec9')</pre>

<p>Since the repository stores all meta data related to stored objects, you may ask for R objects that match some pattern. The instruction below downloads all R objects of the class ‘gg’ and plot them all.</p>

<pre>library(&quot;gridExtra&quot;)
allPlots &lt;- asearch(&quot;class:gg&quot;)
do.call(grid.arrange, allPlots)</pre>

<p><center><img src="http://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/06/Screen-Shot-2016-06-26-at-07.12.54-1024x612.png?resize=456%2C272" data-recalc-dims="1"></center></p>
<p>If you wish to learn more about the archivist package, come to <a target="_blank" href="http://schedule.user2016.org/event/7BYx/how-to-use-the-archivist-package-to-boost-reproducibility-of-your-research#">the useR 2016 archivist talk</a> or see <a target="_blank" href="http://pbiecek.github.io/archivist/">the archivist webpage</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/shiny-archivist-reproducible-interactive-exploration/"></div></div><div><a data-url="http://www.r-bloggers.com/shiny-archivist-reproducible-interactive-exploration/" data-counturl="http://www.r-bloggers.com/shiny-archivist-reproducible-interactive-exploration/" data-count="vertical" data-text="Shiny + archivist = reproducible interactive exploration" 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/06/shiny-archivist-reproducible-interactive-exploration/"> 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/G-JfXpphd_A"></a></div>