Caching child files in knitr

I have a problem with child files in knitr. Caching works fine, but dependencies don't work. My sandbox example looks like this:

\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 "child.Rnw" it looks like this:

 <<child>>= print(x) @ 

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

Thanks for the help!

+4
source share
1 answer

For some time I was thinking about this problem, and now it’s difficult for me to fix it. The problem is that the parent document does not really know what is in the child document, and dep_auto() does not take into account the child documents when setting up the dependency structure. There are two ways to solve this problem. The first one is hacker:

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

As you probably know ::: means "danger zone" in R. In knitr , dep_list is an internal object that controls the structure of dependencies. Both dep_auto() and dep_prev() rely on this object (similar to how the chunk dependson function dependson ).

The second way is to write your object in the chunk option, for example

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

Read the third section on the knitr cache page for more details .

+3
source

All Articles