r-如何修改Knitter缓存的存储和加载方式



当我编织一个cache=TRUE的文档时,我经常发现重新编织并没有快多少。我想知道实际缓存的文件是否以不太理想的格式存储在磁盘上,并"缓慢"地加载回来。我在Knitter缓存的输出存储在哪里?引用https://github.com/yihui/knitr/blob/master/R/cache.R其中我认为对象是作为R数据文件存储和加载的,而不是存储在RAM中。问题是:

  • 我说得对吗
  • 有批准的修改方法吗,我想使用允许多线程保存和加载的qs格式
  • 最好的方法是简单地将对象保存在不同的环境中,并根据需要访问它们,这可行吗

如指向knitr/R/cache.R的链接所示,knitr设置了一个函数列表来管理缓存,并将其存储在未导出的knitr:::cache对象中。该对象是一个函数列表;这些函数共享从调用CCD_ 4时起的评估帧作为它们的环境。

如果要更改缓存对象的存储位置或存储方法,可以修改knitr:::cache对象。

这有点棘手,因为它不是导出的对象。但如何做到这一点:

newcache <- knitr:::cache
#  Make all the changes you like to newcache!
assignInNamespace("cache", newcache, "knitr")

例如,此文档在呈现日志中打印一些关于调用exists的信息:

---
title: "Untitled"
date: "2022-07-29"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
newcache <- knitr:::cache
oldexists <- newcache$exists
newcache$exists <- function(hash, lazy = TRUE) {
cat("cache$exists called with hash=", hash, "n")
oldexists(hash, lazy)
}
assignInNamespace("cache", newcache, "knitr")
```

```{r}
x <- 1:100
print(x[1:10])
```

我在给Pandoc:打电话之前在日志中看到了这一点

processing file: Untitled.Rmd
|..................                                                    |  25%
ordinary text without R code
|...................................                                   |  50%
label: setup (with options) 
List of 1
$ include: logi FALSE
|....................................................                  |  75%
ordinary text without R code
|......................................................................| 100%
label: unnamed-chunk-1
cache$exists called with hash= Untitled_cache/html/unnamed-chunk-1_bc684450204aad4eef880fd842708be8 
cache$exists called with hash= Untitled_cache/html/unnamed-chunk-1_bc684450204aad4eef880fd842708be8 

最新更新