knitr的缓存机制如何检查是否运行块?

时间:2022-12-20 21:45:14

I am writing a document in knitr and Latex and having trouble with the caching mechanism. Specifically, I have the problem that I write multiple data frames into CSV files to read them in using pgfplotstable later on. However, knitr's caching mechanism fails to re-run a chunk if the corresponding CSV file has since been removed or altered in any way. For example, running the following document once, deleting mwe.dat and then running it again will produce an error

我正在使用knitr和Latex编写一个文档,并且在缓存机制方面遇到了麻烦。具体来说,我有一个问题,我将多个数据帧写入CSV文件,以便稍后使用pgfplotstable读取它们。但是,如果相应的CSV文件已被删除或以任何方式更改,则knitr的缓存机制无法重新运行块。例如,运行以下文档一次,删除mwe.dat然后再次运行它将产生错误

\documentclass{article}
\begin{document}
<<cache=TRUE>>=
df <- data.frame(a=rep(1,5), b=rep(2.5), c=rep(3,5))
write.csv(df, file='mwe.dat')
rm(df)
@

<<cache=FALSE>>=
df <- read.csv(file='mwe.dat')
## Warning: cannot open file ’mwe.dat’: No such file or directory
## Error: cannot open the connection
@
\end{document}

So the question is, how does knitr determine that a chunk needs to be re-run? If it's only about actual changes in the source code of the chunk, then I'll have to wrap my (considerable amount of) write.csv statements each in their own chunk, despite the fact that most of the time the chunk only sets up the data frame to be written.

所以问题是,knitr如何确定需要重新运行一个块?如果它只是关于块的源代码的实际更改,那么我将不得不将我的(相当多的)write.csv语句包装在它们自己的块中,尽管事实上大部分时间块只设置要写入的数据帧。

1 个解决方案

#1


1  

The cache page in the knitr website has explained this issue. In particular, search for #238 in that page for a similar case.

knitr网站上的缓存页面解释了这个问题。特别是,针对类似案例在该页面中搜索#238。

#1


1  

The cache page in the knitr website has explained this issue. In particular, search for #238 in that page for a similar case.

knitr网站上的缓存页面解释了这个问题。特别是,针对类似案例在该页面中搜索#238。