How to cache knitr chunks across two (or more) files?

半腔热情 提交于 2020-01-07 00:53:06

问题


I want to use some R-Code in two different *.Rnw files and want to use caching across those files. I read http://yihui.name/knitr/demo/externalization/

Caching in one file just works fine. But running the second one the whole code is executed again:

plain.R

## @knitr random1
a <- rnorm(10)
a

doc1.Rnw (and doc2.Rnw)

\documentclass{article}

<<set-options, echo=FALSE, cache=FALSE>>=
 options(replace.assign=TRUE)
 opts_chunk$set(external=TRUE, cache=TRUE, echo=FALSE, fig=TRUE)
 read_chunk('plain.R')
@

\title{Doc 1}

\begin{document}

<<random1>>=
@

\end{document}

Is there a way to share the cache across several documents?


回答1:


It is entirely possible to reuse the cache across multiple source documents. Please read the cache page carefully to understand when cache will be rebuilt. In your case, the cache is not supposed to be rebuilt unless your two documents have different chunk options (condition 1), or different getOption('width') (condition 3), since your code remains the same (condition 2).

You have to post a reproducible example, otherwise this is not considered a real question.




回答2:


After completely resetting the example it turned out that the cache is reused by both files. I'm not sure what caused the problem before ....

But in a bigger project the chunks are not cached. So I'm not sure what causes the problem - maybe just a different count of spaces ....



来源:https://stackoverflow.com/questions/17503843/how-to-cache-knitr-chunks-across-two-or-more-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!