<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/lazyloading-cached-chunks-into-an-interactive-r-session/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.peteredewitt.com/dewittpe.github.io//dewittpe.github.io//2017/lazyloading-cached-chunks/"> Peter E. DeWitt</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>If you cache code chunks when using<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=knitr"><code>knitr</code></a> to generate reproducible<br>
documents then you’ve likely had the issue arrise of needing to load the results<br>
of cached chunks into an active interactive R session. The functions<br>
<code>lazyload_cache_dir</code> and <code>lazyload_cache_labels</code> have been added to the<br>
developmental version of <a target="_blank" rel="nofollow" href="https://github.com/dewittpe/qwraps2">qwraps2</a> to make<br>
loading cached objects into an interactive R session easy.</p>
<p>Let’s make some trivial cached chunks in a file <code>report.Rmd</code></p>
<div>
<pre><code>  ---
  title:  &quot;A Report&quot;
  output: html_document
  ---
  
  ```{r first-chunk, cache = TRUE}
  fit &lt;- lm(mpg ~ wt + hp, data = mtcars)
  x &lt;- pi
  ```
  
  ```{r second-chunk, cache = TRUE}
  fit &lt;- lm(mpg ~ wt + hp + am, data = mtcars)
  xx &lt;- exp(1)
  ```
</code></pre>
</div>
<p>The resulting project directory looks like this:</p>
<div>
<pre><code>    .
    ├── report_cache
    │   └── html
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.RData
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.rdb
    │       ├── first-chunk_bf368425c25f0c3d95cac85aff007ad1.rdx
    │       ├── __packages
    │       ├── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.RData
    │       ├── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.rdb
    │       └── second-chunk_2c7d6b477306be1d4d4ed451f2f1b52a.rdx
    ├── report.html
    └── report.Rmd
</code></pre>
</div>
<p>Now, let’s assume you need to come back to this project at a later date. You<br>
want to get the objects created in the cached chunks into a new interactive R<br>
session.</p>
<p>If you use <code>lazyload_cache_dir</code> all the cached chunks will be load.</p>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>ls</span><span>()</span><span>
</span><span>## character(0)
</span><span>qwraps2</span><span>::</span><span>lazyload_cache_dir</span><span>(</span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;report_cache/html&quot;</span><span>)</span><span>
</span><span>## Lazyloading: report_cache/html/first-chunk
## Lazyloading: report_cache/html/second-chunk
</span><span>ls</span><span>()</span><span>
</span><span>## [1] &quot;fit&quot; &quot;x&quot; &quot;xx&quot;
</span><span>fit</span><span>
</span><span>## ## Call:
## lm(formula = mpg ~ wt + hp + am, data = mtcars)
## ## Coefficients:
## (Intercept) wt hp am ## 34.00288 -2.87858 -0.03748 2.08371
</span><span>x</span><span>
</span><span>## [1] 3.141593
</span><span>xx</span><span>
</span><span>## [1] 2.718282
</span></code></pre>
</div>
<p>This could be a problem. There was a <code>fit</code> created in <code>first-chunk</code> and another<br>
object <code>fit</code> created in <code>second-chunk</code>. The <code>fit</code> object in the active<br>
workspace is from <code>second-chunk.</code> What if you want the fit from <code>first-chunk</code>?<br>
Use <code>lazyload_cache_labels</code>.</p>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>ls</span><span>()</span><span>
</span><span>## character(0)
</span><span>qwraps2</span><span>::</span><span>lazyload_cache_labels</span><span>(</span><span>&quot;first-chunk&quot;</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;report_cache/html&quot;</span><span>)</span><span>
</span><span>## Lazyloading report_cache/html/first-chunk_bf368425c25f0c3d95cac85aff007ad1
</span><span>ls</span><span>()</span><span>
</span><span>## [1] &quot;fit&quot; &quot;x&quot;
</span><span>fit</span><span>
</span><span>## ## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
## ## Coefficients:
## (Intercept) wt hp ## 37.22727 -3.87783 -0.03177
</span><span>x</span><span>
</span><span>## [1] 3.141593
</span><span>xx</span><span>
</span><span>## Error in eval(expr, envir, enclos): object 'xx' not found
</span></code></pre>
</div>
<p>Now, what if you want <code>fit</code> from <code>first-chunk</code> and <code>xx</code> which was created in<br>
<code>second-chunk</code>?</p>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>ls</span><span>()</span><span>
</span><span>## character(0)
</span><span>qwraps2</span><span>::</span><span>lazyload_cache_dir</span><span>(</span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;report_cache/html&quot;</span><span>)</span><span>
</span><span>## Lazyloading: report_cache/html/first-chunk
## Lazyloading: report_cache/html/second-chunk
</span><span>qwraps2</span><span>::</span><span>lazyload_cache_labels</span><span>(</span><span>&quot;first-chunk&quot;</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;report_cache/html&quot;</span><span>)</span><span>
</span><span>## Lazyloading report_cache/html/first-chunk_bf368425c25f0c3d95cac85aff007ad1
</span><span>ls</span><span>()</span><span>
</span><span>## [1] &quot;fit&quot; &quot;x&quot; &quot;xx&quot;
</span><span>fit</span><span>
</span><span>## ## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
## ## Coefficients:
## (Intercept) wt hp ## 37.22727 -3.87783 -0.03177
</span><span>x</span><span>
</span><span>## [1] 3.141593
</span><span>xx</span><span>
</span><span>## [1] 2.718282
</span></code></pre>
</div>
<p>A better solution would be to use unique object names.</p>
<p>Lastly, say you only want the <code>fit</code> from <code>first-chunk</code> and no other objects.<br>
The optional <code>filter</code> argument of <code>lazyload_cache_labels</code> can be used. The<br>
<code>filter</code> argument is passed to <code>lazyLoad</code>. “filter: An optional function which<br>
when called on a character vector of object names returns a logical vector:<br>
only objects for which this is true will be loaded.”</p>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>ls</span><span>()</span><span>
</span><span>## character(0)
</span><span>qwraps2</span><span>::</span><span>lazyload_cache_labels</span><span>(</span><span>&quot;first-chunk&quot;</span><span>,</span><span>
</span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;report_cache/html&quot;</span><span>,</span><span>
</span><span>filter</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>x</span><span> </span><span>==</span><span> </span><span>&quot;fit&quot;</span><span>)</span><span>
</span><span>## Lazyloading report_cache/html/first-chunk_bf368425c25f0c3d95cac85aff007ad1
</span><span>ls</span><span>()</span><span>
</span><span>## [1] &quot;fit&quot;
</span><span>fit</span><span>
</span><span>## ## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
## ## Coefficients:
## (Intercept) wt hp ## 37.22727 -3.87783 -0.03177
</span></code></pre>
</div>
<p>The <code>lazyload_cache_dir</code> and <code>lazyload_cache_labels</code> functions are currently in<br>
the developmental version of <code>qwarps2</code>. You can get the code and install the<br>
package from <a target="_blank" rel="nofollow" href="https://github.com/dewittpe/qwraps2">https://github.com/dewittpe/qwraps2</a>.<br>
I’m not sure when the next version of <code>qwraps2</code> will be pushed to CRAN, so the<br>
developmental version will have to do for now.</p>
<p>If you find any bugs or have suggestions on how to improve the functions please<br>
<a target="_blank" rel="nofollow" href="https://github.com/dewittpe/qwraps2/issues">create and issue</a>.</p>
<p>Note, the behavior of <code>knitr::load_cache</code> is very different from the behavior of<br>
<code>qwraps2::lazyload_cache_dir</code> and <code>qwraps2::lazyload_cache_labels</code>.<br>
<code>knitr::load_cache</code> is to be used to load cached values within a .Rmd prior to<br>
the chunk being called. See the documentation and<br>
<a target="_blank" rel="nofollow" href="https://github.com/yihui/knitr-examples">example 114-load-cache.Rmd</a> from the<br>
<a target="_blank" rel="nofollow" href="https://github.com/yihui/knitr-examples">knitr-examples</a> repository for the use of<br>
<code>knitr::load_cache</code>.</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://www.peteredewitt.com/dewittpe.github.io//dewittpe.github.io//2017/lazyloading-cached-chunks/"> Peter E. DeWitt</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/bkZq7FbYyHs"></a></div>