Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache and pdfcrop question #105

Closed
davidcarslaw opened this issue Jan 24, 2012 · 12 comments
Closed

cache and pdfcrop question #105

davidcarslaw opened this issue Jan 24, 2012 · 12 comments
Labels
bug Bugs
Milestone

Comments

@davidcarslaw
Copy link

I'm using knitr_0.1.5 on a document and in my SweaveOpts I have pdfcrop=TRUE and cache=TRUE. I also have knit_hooks$set(pdfcrop = hook_pdfcrop) early on.

When I first knit the document it correctly evaluates all chunks and crops them. If I immediately knit the document again it evaluates all the chunks again (not expected) - but does not crop them. On a third pass it does not evaluate anything (which is expected). Then if I change a chunk it evaluates it but does not crop the pdf...

A typical chunk would be like:

<<mychunk,fig.width=12,fig.height=5,out.width=1\textwidth>>=
ggplot(dat, aes(site, ws)) +
    geom_boxplot(aes(fill = data), outlier.shape = NA) 
@

So - something a bit odd somewhere. Earlier versions (pre 0.1 I think...) worked OK as far as I can tell.

Thanks

David

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

Looks like there is a little bug for cleaning up the options when exiting knit(); I'm looking into it. Thanks!

@yihui yihui closed this as completed in 9f36873 Jan 25, 2012
@yihui
Copy link
Owner

yihui commented Jan 25, 2012

should be fixed now; please test. Thanks!

@davidcarslaw
Copy link
Author

Thanks - just tested again and I see the same behaviour as described above...using v 0.1.6

@davidcarslaw
Copy link
Author

Also, I noticed that if I set cache=TRUE in SweaveOpts, but have a chunk that has cache=FALSE; that chunk is not evaluated every time I knit, which presumably it is meant to do? Anyway, thanks for looking into these issues!

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

No, cache=FALSE means to run the chunk absolutely every time.

This sounds like a serious issue, and I will look into it again.

yihui added a commit that referenced this issue Jan 25, 2012
… are NULL

this will cause problems in cache because the chunk options are not what I expected (#105)
@yihui
Copy link
Owner

yihui commented Jan 25, 2012

How about the latest version now? I tested and did not find either cached chunks to be evaluated twice, or uncached chunks to be skipped.

Note before you test, you need to remove the old cache files and start from a new R session to make sure everything is clean.

@davidcarslaw
Copy link
Author

I just tried in a fresh R session with the cache cleared and that solves one of the problems i.e. it always runs the cache=FALSE chunks. However, the first pass is OK, second pass re-evaluates everything again (without cropping figs) and the third pass is OK (but does not crop any changed fig chunks). I'll have to see if I can extract a minimal Rnw file that shows this behaviour.

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

Yes, I appreciate if you could show me a minimal example.

@davidcarslaw
Copy link
Author

Right. I copied your minimal example and replaced the SweaveOpts with that below (which I use in my document)

\SweaveOpts{fig.path=figure/manual-,cache.path=cache/test-,fig.align=center,external=TRUE,fig.show=hold,size=small,warning=FALSE,pdfcrop=TRUE,background=1;1;1,echo=TRUE,cache=TRUE}

Also added knit_hooks$set(pdfcrop = hook_pdfcrop) in you setup chunk.

In a fresh R session/no cache I knit it and it crops. Correct.

knit again and it evaluates all but the setup chunk again (I waited a minute just so the time-stamp would change...)

change one of the 'boring-plots' chunks, knit again it runs the chunk but does not crop.

Hope that helps!

And if it matters this is R 2.14.1 on 64bit Windows.

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

OK, I believe the culprit is getOption('width'). Read the section on cache in the main manual: https://github.com/downloads/yihui/knitr/knitr-manual.pdf (especially footnote 5).

If options('width') is changed, the cache will be rebuilt. Now it should be easy to understand the weird phenomenon on the three times:

  1. options(width = 90), results cached
  2. cache loaded, the setup chunk is skipped, so options('width') takes the default value 75 now (i.e. changed), and cache has to be rebuilt;
  3. setup skipped again; the second chunk has no changes, so skipped too

As the second to last paragraph in the Cache section mentioned, you should be careful with chunks that have side-effects -- in most cases, you must not cache them.

@davidcarslaw
Copy link
Author

Ah! I see...that is quite a subtle effect and it does explain all the behaviour I see. I am careful with my main chunks, but that early set-up chunk can easily be forgotten. Given the importance of that first set-up chunk in potentially affecting the whole document it might be worth adding cache=FALSE in your example documents as an aid memoir and to prevent others accidently doing what I did and basing a document on one of yours? Anyway, great to have nailed that - so thanks very much!

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

No branches or pull requests

2 participants