lindelof
lindelof

Reputation: 35240

Why doesn't `knitr`, when called from the console, use the same cache as when called from the IDE?

When I knit a document in RStudio (by clicking on the "Knit") button, a cache folder gets created named after the file's name as ${filename}_cache.

I would like to be able to inspect the objects created by the compilation, and have them in my workspace. To do that I call knit() from the console:

knit("filename.Rmd")

The workspace now contains all the objects, but doing so created a cache named simply cache/.

I've tried to trick knitr into using the same cache name with an option:

opts_chunk$set(cache.path = "filename_cache/")

Now it would appear that the same cache directory is used, but calling knit() from the command line seems to invalidate the cache created when clicking on the "Knit" button.

For example, if you have this document, saved as cache.Rmd:

```{r}
library(knitr)
opts_chunk$set(cache.path = "cache_cache/")
```

```{r, cache=TRUE}
foo <- rnorm(10)
foo
```

You will have different results if you compile this with the "Knit" button or from the console.

Is there a way to use the same cache, independently on how knit() was called?

Upvotes: 0

Views: 124

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30124

For R Markdown documents, do not call knitr::knit() but rmarkdown::render() instead. rmarkdown = knitr + Pandoc, and rmarkdown modifies several default chunk options in knitr, including the cache.path option (see source).

Upvotes: 1

Related Questions