10 Comments
Guest *Charles Berry* @ 2012-08-18 04:08:00 originally posted:
Can the caching of chunks in child documents be made to depend on chunks in the parent document?
I have used dependson="mainchunk" in a child document, where 'mainchunk' appears in the parent. No error is reported, but the cache does not get rebuilt when the code in 'mainchunk' is altered.
Unfortunately, no. The reason is that dependencies are built at the parsing time of a parent document, when the child documents are not parsed yet. However, there is at least one workaround. For example, you can use an arbitrary chunk option in the child chunk like foo=x where x comes from mainchunk, so that whenever x is changed, this child chunk will be updated accordingly.
Originally posted on 2012-08-18 05:14:04
Guest *Charles Berry* @ 2012-08-18 21:22:24 originally posted:
Thanks. That works.
But now I find a bug for labelled chunks in the child document when called from a loop in the parent.
Using knitr_0.7, I get
Error: duplicated label 'noname'
where 'noname' is the first label in the child doc when the second call to knit_child is run.
I have knitr_0.4 on another computer, where the same code runs just fine.
The 0.7 behavior is expected; 0.4 is dangerous because it may bring potential chunk conflicts of which users can be unaware. For example, the noname chunk in the child doc can override the chunk with the same label in the parent doc. In 0.7 I required chunk labels to be unique across all the documents to avoid this problem.
Originally posted on 2012-08-19 01:16:47
Guest *jamiefolson* @ 2012-10-26 02:06:45 originally posted:
So it is not possible to put/load knitr chunks in a loop? Or is it possible to assign unique chunk names for them?
@google-9efc2d0be4fe563faf44e811749aa874:disqus I do not quite understand what you mean. Perhaps example 20 at https://github.com/yihui/knitr-examples ?
Originally posted on 2012-10-26 03:59:24
Guest *Hyao* @ 2012-12-14 20:34:53 originally posted:
Hi Yihui,
If I save Rnw file in a different directory from output files, such as .tex, .pdf, etc, and when I run R(knitr) in the output directory, the dep_auto fails to find cache files.
For example, a project is created in the ./projectK, and the Rnw file is saved in ./projectK/src/test.Rnw. Now R and knitr are running from ./projectK/analysis using knit("../src/test.Rnw") and the root.dir='./projectK/analysis'. The following warning shows up:
Warning: file cache/__objects not found
And the dep_auto fails. Could you please help on this issue?
Thanks,
Hui Yao
You should see this warning the first time you compile the document, but I think it should go away the next time.
Originally posted on 2012-12-14 22:51:54
Guest *Hyao* @ 2012-12-27 23:03:57 originally posted:
Thanks for your reply. I compiled several times but the warning still showed up and dep_auto still failed. It seems to me that the only way to make it work is to save Rnw file into the same directory of Tex and other output files.
Hui
Did you cache the chunk containing dep_auto() as well? You are not supposed to cache that one.
Originally posted on 2012-12-28 01:33:38
Guest *Hyao* @ 2012-12-28 15:56:05 originally posted:
No. I do not cache the chunk containing dep_auto(). I pretty much just copied and pasted one of your examples. Here is my code:
<<setup, cache="FALSE">>=
global chunk options
opts_knit$set(root.dir='/workspace/hyao/MyProjects/bcb/testKnitr/analysis')
opts_chunk$set(cache=TRUE, autodep=TRUE,cache.path='cache/test-knitr-cache-KB-02-')
dep_auto() # figure out dependencies automatically
@
<>=
x = 100
@
<**>=
(y = x + 2)
@
As I said, if the code in Rnw file was saved in the testKnitr/src/ subdirectory and the code was compiled in the testKnitr/analysis subdirectory, the cache does not work. If the Rnw was moved to the /analysis subdirectory, it worked.
Thanks,
Hui
**
I still cannot reproduce the problem. It works fine for me. Are you using the latest version of knitr?
Originally posted on 2012-12-28 16:40:05
Guest *Hyao* @ 2013-01-03 20:19:17 originally posted:
I am using knitr_0.9 at R version 2.15. The session info is listed as below.
sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] gdata_2.12.0 xtable_1.7-0 markdown_0.5.3 codetools_0.2-8 knitr_0.9 DESeq_1.11.0 lattice_0.20-10
[8] locfit_1.5-8 Biobase_2.16.0 BiocGenerics_0.2.0
loaded via a namespace (and not attached):
[1] annotate_1.34.1 AnnotationDbi_1.18.4 DBI_0.2-5 digest_0.6.0 evaluate_0.4.3 formatR_0.7
[7] genefilter_1.38.0 geneplotter_1.34.0 grid_2.15.1 gtools_2.7.0 IRanges_1.14.4 RColorBrewer_1.0-5
[13] RSQLite_0.11.2 splines_2.15.1 stats4_2.15.1 stringr_0.6.2 survival_2.37-2 tools_2.15.1
[19] XML_3.95-0.1
Sorry I just figured out the bug yesterday, and it has been fixed in the devel version: yihui/knitr@a35827cc
Please install the devel version on Github and see if the bug has gone. Thanks!
library(devtools); install_github('knitr', 'yihui')
Originally posted on 2013-01-08 22:46:42
Guest *Hyao* @ 2013-01-15 01:08:05 originally posted:
Thanks a lot! I will try it later.
Hui
Note that the new version (1.0) has been released to CRAN now, so it should work if you install from CRAN directly.
Originally posted on 2013-01-15 01:20:15
Guest *Hyao* @ 2013-01-15 01:20:14 originally posted:
Hi Yihui,
I found a situation where the cache seems not work well. I hope that you could help me on it. The sample code is listed as below.
In the testCache.Rnw:
<<setup, cache="FALSE">>=
global chunk options
opts_chunk$set(cache=T, autodep=T,cache.path='cache/test-knitr-cache-KB-03-')
dep_auto() # figure out dependencies automatically
@
<<a,cache=f>>=
source("../src/test-knitr-cache-KB-03.R")
x=testCache(5)
c=5
@
<>=
(y = x + 2)
@
In addition, in the "../src/test-knitr-cache-KB-03.R":
testCache <- function(x=100){
2*x+50
}
In this case, if I change the function, testCache, the x can be updated and y CANNOT be updated accordingly, because y is cached. However, if the first chunk gets cached as, <<a,cache=t>>, even x CANNOT be updated, since literally nothing is changed in testCache.Rnw.
I know that the function, testCache, could be copied and pasted into the testCache.Rnw to solve the problem. But in some situations, I hope that the report is simple and clean, and thus would like to save utility functions in to a separate file. So do you know any way to make it work with cache being ON.
Thanks in advance,
Hui
See issue 457 yihui/knitr#457 and also the section "Even more stuff for cache?" in this page.
Originally posted on 2013-01-15 03:38:39
Guest *Hyao* @ 2013-01-15 16:46:37 originally posted:
It is exactly what I need. Thanks a lot!
Guest *Aleksey* @ 2013-10-10 19:57:04 originally posted:
Hi Yihui,
Many thanks for the package, it's a great thing.
I am having problems with caching - I cannot understand why I get cached output while cache=F in a chunk. Is my understanding correct that if a chunk uses cache=F, then all caching is off, and the chunk should be reevaluated every time the document is knit'ted?
My problem is, the simple 2-chunk example below works fine: 'xx' obtained via sample() is updated every time the document is run. However, caching in the 'test' chunk does not work as expected in my real document with 30 or so chunks. Actually, it works if the 'test' chunk is somewhere in the beginning of the document, but if I place it towards the end, then it does not: I always get the same numbers in 'xx' no matter how many times I rerun the document. Needless to say, big documents is where you really need caching to work correctly.
Curiously, even editing the 'test' chuck, e.g. adding xx+1 does not change xx, while the change in the chunk is echoed correctly. The only 2 things that seem to force caching off is (a) deleting cache, and (b) changing the line that produces xx, e.g. xx = sample(1:10, 4).
Am I missing something? Is there a way to troubleshoot this behavior?
<<setup, echo="F," cache="FALSE">>=
cache = TRUE # chuck cache
Default knitr chunk options
opts_chunk$set(background='gray90', cache=cache, fig.align='center', prompt=FALSE, fig.path='__figures/', cache.path='__cache/', tidy=TRUE, echo=TRUE, tidy.opts=list(width.cutoff=95, keep.blank.line=TRUE), size='small')
@
<<test, cache="F">>=
xx = sample(1:10, 3)
xx
@
On a side note, is there a difference in cache=F and cache=FALSE? There does not appear to be a difference in behavior, just looking at the knitr's progress I see that FALSE is interpreted as logical:
15:06:04.346: label: test (with options)
15:06:04.349: List of 1
15:06:04.352: $ cache: logi FALSE
while F as a symbol:
15:16:53.563: label: test (with options)
15:16:53.566: List of 1
15:16:53.569: $ cache: symbol F
This does not seem to change the caching behavior, just curious - does it matter for anything?
Many thanks
Aleksey
Guest *Aleksey* @ 2013-10-10 20:00:44 originally posted:
Yihui,
Sorry, somehow the chunk code above got quotes when I pasted it in. I am trying again. I am not sure why, but FALSE and F get quoted once I post to this site (they are NOT in my chunk code), and an extra ',' in the echo option gets somehow added...
<<setup, echo="F," cache="FALSE">>=
cache = TRUE # chuck cache
Default knitr chunk options
opts_chunk$set(background='gray90', cache=cache, fig.align='center', prompt=FALSE, fig.path='__figures/', cache.path='__cache/', tidy=TRUE, echo=TRUE, tidy.opts=list(width.cutoff=95, keep.blank.line=TRUE), size='small')
@
<<test, cache="F">>=
xx = sample(1:10, 3)
xx
@
Aleksey
Hi, cache=FALSE is the correct way of turning off cache. Once cache is turned off, your code will be re-evaluated every time. I do not understand your question, because it is certainly expected that sample(1:10, 3) produces different results every time.
Originally posted on 2013-10-21 04:02:45
Guest *giovanni* @ 2014-06-29 12:10:19 originally posted:
Hi Yihui,
your package is great, i wish i had been less lazy and looked up sweave and knitr before.
Anyway, I wanted to inform you about a problem with the cache that gave me a headache. I use Rstudio 0.98.501 (the latest), knitr 1.6 (the latest) and R3.02 (not the latest, but I have a reason for this). I stupidly set cache = TRUE in some chunks. But in spite of all i tried one chunk kept not being evaluated.
The chunk is the following
<<pca, echo="TRUE," tidy="TRUE," fig.show="asis" ,="" out.width="10cm" ,="" out.height="10cm" ,="" fig.align="center" ,="" cache="FALSE," fig.cap="Screeplot of the baseball data" ,="" fig.pos="H">>=
The R code is irrelevant, I guess.
I tried many things (beside setting cache=FALSE), deleted the cache directory, changed several options (including setting tidy=FALSE, as you suggest) but nothing, the old plot kept appearing in the document. The fantastic thing is that the plot file kept reappearing after I wiped all the files in the figure directory. I have not set evaluate=FALSE globally.
The only thing that worked (I know that now it sounds obvious) was to rename the chunk to a different name. I am surprised that nobody else mentioned this problem,or am I doing something wrong? If not, it seems quite an annoying bug.At least let me know where the hidden cache is? :-)
Thanks for all your work and dedication.
Guest *giovanni* @ 2014-06-30 05:30:19 originally posted:
ps I forgot to mention that the "pca" chunck kept not being evaluated even after i changed name to the file!
also the bogus ="" elements were not there
This sounds indeed weird. Sorry, but I cannot do anything without a minimal reproducible example.
Originally posted on 2014-07-06 16:18:56
Guest *Dean Attali* @ 2015-02-04 11:21:34 originally posted:
Hi Yihui, is there a way to make a cached chunk depend on an object, but not on the whole chunk from which the object was created?
Guest *Lucas Gallindo* @ 2016-06-16 14:13:19 originally posted:
I'm clueless about caching on R or knitr. I have a very simple piece of code:
<>=
Echoing the random seed and RNG for reproducibility
.Random.seed
RNGkind()
@
That fails with an ".Random.seed" object not found message. What I'm doing wrong?
Guest *Gregor Thomas* @ 2016-06-22 23:45:51 originally posted:
This doesn't seem to be about caching at all, but maybe you want to be setting a seed instead? Get rid of .Random.seed and replace with set.seed(123).
Guest *Lucas Gallindo* @ 2016-06-27 17:22:38 originally posted:
I don't really want to set the seed for the algorithms, I want to use a R default. I want to save the standard seed for later reference.
Googling refereed me to cache issues - knitr and Sweave do some cachingmanipulationtracking of random seed values (apparently, without this tracking, the seed is reset in every code chunk).
(Why I don't want to set a seed: Not every seed maximize the RNG period and the more elaborate algorithms use very complex seeds)
Guest *Lucas Gallindo* @ 2016-06-27 17:23:56 originally posted:
PS.: Running that code sample on RStudio, outside of pdf compilation, yelds the expected results.
Guest *Don Klinkenberg* @ 2017-07-21 14:04:49 originally posted:
Hi Yihui
I use cache as in an Rmd document, but it seems as if the code is re-evaluated every time, also when I did not change anything. I conclude that from the fact that the name of the cached file changes.
Don
This is the complete Rmd document:
knitr::opts_chunk$set(cache = TRUE, autodep = TRUE)
I start with a simple example:
testdata <- rep(0,100)
print(testdata)
Guest *Don Klinkenberg* @ 2017-07-21 15:13:47 originally posted:
A reply to myself: I've finally found that this problem is also addressed in issue #1226 (yihui/knitr#1226).
Though not really solved, it does work if you don't use the 'Knit' button, but use the command
rmarkdown::render("vignettes/bar.Rmd")
Don
Guest *John Heilbron* @ 2018-05-08 00:38:48 originally posted:
Yihui,
Thanks for the package. I've made several child files in Rmd and have run those and stored contents in a cache so that the pdfs can be reproduced at the touch of a button. I want to read these child files into a parent file (and I can do that) but it seems that the parent file executes the child files and creates a separate cache of its own. I'm wondering if there's some way to direct the parent file to use the cache of the child files so that it doesn't take so long to execute. (Some of the child files took a long time to execute the first time I ran them and I'd rather not have to deal with that all over again.)
Thanks,
John
Thank you very much for your awesome work on this package!
Does caching feature also work with language engines other than R?
Thanks,
Petr
Yes, it does.
Sign in to join the discussion
Sign in with GitHub