Where is cache cache stored?

The cache directory can be used lazyLoadto view the environment at the end of the fragment. But where is the output of the block (which will be printed if the document is compiled)?

+2
source share
1 answer

Use the source!

Look at the source code here. https://github.com/yihui/knitr/blob/master/R/cache.R

You can see the mechanism is explained here (in function new_cache)

# when cache=3, code output is stored in .[hash], so cache=TRUE won't lose
# output as cacheSweave does; for cache=1,2, output is the evaluate() list
cache_output = function(hash, mode = 'character') {
  get(sprintf('.%s', hash), envir = knit_global(), mode = mode, inherits = FALSE)
}

those. it is saved as an object in knit_globalenvironmentemnt`

You can check these objects on ls(knitr::knit_global(), all = TRUE)

those. 3 simple pieces below

```{r, cache=TRUE}
summary(cars)
```

```{r }
 ls(knitr::knit_global(), all = TRUE)

```


```{r }
 get(ls(knitr::knit_global(), all = TRUE)[1], knitr::knit_global())

```

Give the following conclusion

summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
 ls(knitr::knit_global(), all = TRUE)
## [1] ".Preview-2b40490e2591_cache/unnamed-chunk-1_766fcb86fd875984b372e3c23210bfad"
## [2] "metadata"
 get(ls(knitr::knit_global(), all = TRUE)[1], knitr::knit_global())
## [1] "\n```r\nsummary(cars)\n```\n\n```\n##      speed           dist    \n##  Min.   : 4.0   Min.   :  2  \n##  1st Qu.:12.0   1st Qu.: 26  \n##  Median :15.0   Median : 36  \n##  Mean   :15.4   Mean   : 43  \n##  3rd Qu.:19.0   3rd Qu.: 56  \n##  Max.   :25.0   Max.   :120\n```"

R, *.RData load. , get cat, "\n" .

+3

Source: https://habr.com/ru/post/1541374/


All Articles