Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read knitr/Rmd cache in interactive session?

I have an Rmd file with a lot of cached code chunks.

Now I want to keep developing that script using an interactive session to play around and test different solutions before putting the final code in a new chunk of the document.

With a plain R script, I could just source it to get my interactive session on par with the last line of the script. However, this would result in (re-)executing all code within the interactive session.

I want to read my Rmd file into an interactive session ignoring the Markdown part & making use of the existing knitr cache, ideally without creating any output.

How can I do this?

PS: I am not looking for some IDE-specific way to set this up but for a command that I can run from a simple R session in any terminal emulator.

like image 446
mschilli Avatar asked Jul 03 '15 07:07

mschilli


People also ask

How do I use cache in R markdown?

Caching a code chunk in R Markdown R Markdown has a built-in caching feature that can be enabled by setting cache=TRUE in the chunk's header. The second time the chunk is run, both the visual output and any objects created are loaded from disk.

How do I clear my knitr cache?

If you run into problems with cached output you can always clear the knitr cache by removing the folder named with a _cache suffix within your document's directory.

How does R knitr work?

When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.

What is knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


2 Answers

I've created functions load the objects from cached chunks into an interactive R session. The functions are lazyload_cache_dir and lazyload_cache_labels and are available in qwraps2 version > 0.2.4

A detailed example of the use of these functions is here:

Quick overview:

Say you have the file report.Rmd

---
title:  "A Report"
output: html_document
---

```{r first-chunk, cache = TRUE}
fit <- lm(mpg ~ wt + hp, data = mtcars)
x <- pi
```

```{r second-chunk, cache = TRUE}
fit <- lm(mpg ~ wt + hp + am, data = mtcars)
xx <- exp(1)
```

After knitting you end up with a this project directory

.
├── 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

and you want to load the objects from first-chunk.

lazyload_cache_labels("first-chunk", path = "report_cache/html")
## Lazyloading report_cache/html/first-chunk_bf368425c25f0c3d95cac85aff007ad1
ls()
## [1] "fit" "x"

See the blog post for details on loading only a whole directory of cached objects or loading specific objects from within a cached chunk.

like image 185
Peter Avatar answered Oct 26 '22 08:10

Peter


Internally, knitr uses lazyLoad to load cached results, and so can you:

lazyLoad('knitr_cache_dir/chunk_2c7d6b477306be1d4d4ed451f2f1b52a')

Make sure to supply the filename without the suffix.

like image 41
sieste Avatar answered Oct 26 '22 07:10

sieste