7

I have a problem with child files in knitr. The caching works fine, but the dependencies do not work. My sandbox example looks like that:

\documentclass{article}

\begin{document}

<<setup, cache=FALSE>>=
opts_chunk$set(cache=TRUE, autodep=TRUE)
dep_auto() # figure out dependencies automatically
@

<<a>>=
x <- 14
@

<<b>>=
print(x)
@

<<child, child='child.Rnw', eval=TRUE>>=
@

\end{document}

With the 'child.Rnw' looking like this:

<<child>>=
print(x)
@

When I now compile the code, then change x in chunk a and then compile it again: chunk b reacts properly, but the child does not. Am I doing something obviously wrong?

Thanks for the help!

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
panuffel
  • 624
  • 1
  • 8
  • 15
  • A new `R` package [`cache`](https://cran.r-project.org/web/packages/cache/index.html) might be helpful in this regard. – MYaseen208 Jun 23 '21 at 06:20

1 Answers1

4

I thought for a while about this issue, and I find it difficult to fix at the moment. The problem is that the parent document does not really know what is in the child document, and dep_auto() does not take the child documents into account when setting the dependency structure. There are two ways to solve this problem. The first one is hackish:

knitr:::dep_list$set(a = c('child', 'b'))

As you probably know, ::: means "danger zone" in R. In knitr, dep_list is the internal object that controls the dependency structure. Both dep_auto() and dep_prev() rely on this object (similarly that is how the chunk option dependson works).

The second way is to write your object into chunk options, e.g.

<<child, whatever=x>>=
print(x)
@

Read the third section in the knitr cache page for details.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Many thanks for the thoughts! I tried the second way, added all objects from the parent document to all chunks of the child document and it seems to work out. – panuffel Feb 27 '13 at 09:32