我使用Memoise包在我正在开发的R包中缓存函数调用。在开发过程中,我定期重建我的软件包。但是每次我这样做,缓存的函数调用就会被遗忘。对于如何保持这些函数调用缓存,有什么建议吗?最好使用记忆包。但如果不可能,另一项建议将得到赞赏。
要从头开始进行复制,请执行以下操作:
第1部分-创建包
在交互式R控制台上,创建一个名为"TmpTestPackage1“的包。(这将在当前工作目录中创建一个名为"TmpTestPackage1“的目录):
> library("devtools")
> create("TmpTestPackage1")创建文件./TmpTestPackage1 1/R/SomeCode.R并插入内容:
library("memoise")
longFunction = function() {
Sys.sleep(5)
return(7)
}
cachedLongFunction = memoise::memoise(longFunction)
someOtherFunction = function() {
return(cachedLongFunction())
}现在在R控制台上(来自TmpTestPackage1目录的父目录,仍然可用devtools ):
> library("devtools")
> install("TmpTestPackage1")第2部分-重现我的问题
> library("TmpTestPackage1")
> someOtherFunction() # This waits for 5 seconds as expected
> someOtherFunction() # Now completes almost immediately because
# the function call is cached. Good.
> install("TmpTestPackage1")
> someOtherFunction() # This waits 5 seconds again! I want it to
# still be cached however.发布于 2017-10-18 15:40:07
默认情况下,缓存在内存中,在重新启动R会话后,将在包重建中清除缓存。
您可以在memoise中使用文件系统缓存,例如,将缓存保存在inst文件夹中。
https://stackoverflow.com/questions/45017032
复制相似问题