Knitr缓存的输出存储在哪里?
在缓存目录中,可以使用lazyLoad
来查看块末尾的环境.但是大块的输出(如果编译了文档,将输出)存储在哪里?
In the cache directory, one can use lazyLoad
to view the environment at the end of a chunk. But where is the output of the chunk (that will be printed if the document is compiled) stored?
使用源代码!
在此处查看源代码https://github.com/yihui/knitr/blob/master/R/cache.R
您可以看到此处已对此机制进行了说明(在new_cache
函数中)
You can see that the mechanism is explained here (within the new_cache
function)
# 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)
}
即它作为对象存储在knit_global
环境`
I.e. it is stored as an object in the knit_global
environemnt`
您可以通过ls(knitr::knit_global(), all = TRUE)
即下面的3个简单块
```{r, cache=TRUE}
summary(cars)
```
```{r }
ls(knitr::knit_global(), all = TRUE)
```
```{r }
get(ls(knitr::knit_global(), all = TRUE)[1], knitr::knit_global())
```
提供以下输出
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,则可以使用load
命令从缓存文件夹中的* .RData文件中加载数据.另外,要输出get
的结果,请考虑使用cat
,它将把"\ n"转换为行,并且应看起来像原始输出.
If you have exited R, you can load the data from the file *.RData in the cache folder using the load
command. Also, to output the result of get
, consider to use cat
which will turn the "\n" into lines and should look like original output.