Alastair Rushworth
Alastair Rushworth

Reputation: 33

Share old knitr cache with new .Rnw files

I'm writing two reports (example1.Rnw and example2.Rnw, say) with cached chunks in the first that I'd like to be able to access and use in the second document.

Suppose example1.Rnw is

\documentclass[a4paper, 11pt]{article}
\begin{document} 
<<simpleExample, cache=TRUE>>=
  z<-1+1
@
\end{document}

Then I thought example2.Rnw would be something like

\documentclass[a4paper, 11pt]{article}
\begin{document}
<<setup>>=
 opts_chunk$set(cache.path = "~/DirectoryOfExample1/cache")
@
<<simplePrint, dependson = 'simpleExample'>>=
  print(z)
@
\end{document}

This seems similar to this question How to cache knitr chunks across two (or more) files? except that I'm not using externalisation. Is it possible to resuse an old cache in a new document this way, and if so how?

Upvotes: 3

Views: 152

Answers (1)

sgibb
sgibb

Reputation: 25736

It is possible to reuse a cache but IMHO you have to replicate you simpleExample chunk in your example2.Rnw file. It have to be exactly the same as in example1.Rnw (no different spaces, no different options, ...).

example2.Rnw:

\documentclass[a4paper, 11pt]{article}
\begin{document}
<<setup>>=
 opts_chunk$set(cache.path = "~/DirectoryOfExample1/cache")
@
<<simpleExample, cache=TRUE>>=
  z<-1+1
@
<<simplePrint, dependson = 'simpleExample'>>=
  print(z)
@
\end{document}

Upvotes: 1

Related Questions