Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cache=FALSE for a knitr markdown document and override code chunk settings?

Tags:

I understand that I can use the cache=TRUE option to cache an R code chunk using R Markdown with knitr. E.g., It might look something like this:

```{r longanalysis, cache=TRUE} for (i in 1:5000) {     lm((i+1)~i) } ``` 

And I realise that If I wanted to disable the cache for that analysis that I could change the option to cache=FALSE in the code chunk. However, if I have many R code chunks with caching enabled, this would require a careful find and replace operation.

Question

  • Is there a way of temporarily setting cache=FALSE for an entire R Markdown document?

Comments

I've seen this example where the first line is. So I imagine this is a way of setting global setting, but I think local settings override global settings.

`r opts_chunk$set(fig.width=5, fig.height=5, fig.path='')` 
like image 236
Jeromy Anglim Avatar asked May 17 '12 00:05

Jeromy Anglim


People also ask

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 do you not run a chunk in R Markdown?

If you don't want any code chunks to run you can add eval = FALSE in your setup chunk with knitr::opts_chunk$set() . If you want only some chunks to run you can add eval = FALSE to only the chunk headers of those you don't want to run.

How do I 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.


2 Answers

It seems the default is set to FALSE and local chunk options override the global options but one thing you could do is set the global options to cache by default by adding this to the top of your document

`r opts_chunk$set(cache=TRUE)` 

Then for the sections you don't want cached ever you would explicitly set those sections to cache=FALSE.

Then if you want to set the whole document to not cache anything you could change the global option to FALSE and rerun it.

The problem is that if any of the chunk options are set to cache=TRUE then those will override the global setting and won't be rerun if you set the global option to FALSE. So I think the only way to achieve what you want is to change the default to cache=TRUE, explicitly set chunks that you don't want cached to have cache=FALSE, and then you can switch the global option to FALSE to do what you want when the time occurs.

like image 169
Dason Avatar answered Sep 28 '22 08:09

Dason


Delete cache option

knitr creates a directory to store the cached objects. By default it is called cache. To ensure that all analyses are run regardless of the code chunks cache setting, simply delete the contents of the cache directory.

Thus, in Rstudio on Linux

  1. Go to menu Tools - Shell to open a console in the working directory containing the markdown file.
  2. Enter the command rm cache/*

A basic workflow

This is my basic workflow at the moment

  • If knitting is quick (e.g., less than 10 seconds), don't cache.
  • If knitting is taking a while (e.g., more than 10 seconds), add `r opts_chunk$set(cache=TRUE)` to the R Markdown file.
  • If caching is causing problems and knitting is relatively quick (e.g., under a few minutes), delete the entire cache.
  • If caching is causing problems and knitting takes a long time (e.g., several minutes or hours), name R code chunks and use the dependson option in knitr. Naming also permits selective deletion of named R code chunks in the cache directory.
like image 37
Jeromy Anglim Avatar answered Sep 28 '22 08:09

Jeromy Anglim