cache.rebuild with cache=2

21 views
Skip to first unread message

Oleg Mayba

unread,
Feb 16, 2016, 2:44:18 PM2/16/16
to knitr
I believe that cache.rebuild does not replace the old cache for chunks with cache=2.

Here's an example. Run these two chunks once:

<<testChunk1, echo=TRUE, eval=TRUE, cache=FALSE>>=
x <- 2
@

<<testChunk2, echo=TRUE, eval=TRUE, cache=2>>=
print(date())
x+3
@

Now, modify the value of 'x' in first chunk and require that cache for second chunk be rebuilt:

<<testChunk1, echo=TRUE, eval=TRUE, cache=FALSE>>=
x <- 20
@

<<testChunk2, echo=TRUE, eval=TRUE, cache=2, cache.rebuild=TRUE>>=
print(date())
x+3
@

The correct value of 23 is now displayed in the output document in the second chunk (along with current date). BUT! if you now re-run the file again and remove the cache.rebuild:

<<testChunk1, echo=TRUE, eval=TRUE, cache=FALSE>>=
x <- 20
@

<<testChunk2, echo=TRUE, eval=TRUE, cache=2>>=
print(date())
x+3
@

then the old value of 5 (and the old date!) will show up in the output document in the second chunk. This does NOT happen if cache=3.

I think the reason are these lines in the block_exec() function (lines 258-261 in block.R)

 if (options$cache < 3) {
      if (!cache.exists) block_cache(options, res.orig, objs)
    } else block_cache(options, output, objs)
  }

It looks like for cache=2, the cache is written out if it does not exist, but is NOT re-written if a) it exists, b) no changes are made to code/options, c) cache.rebuild=TRUE.

I would have expected cache.rebuild=TRUE to trigger cache re-writing here and am wondering if this is a design feature (i.e., something else breaks if cache is actually re-written for cache=2 use cases).

I came across this when switching my default cache value to cache=2 in order to use hook_purl functionality.

Thanks!

Oleg.

Yihui Xie

unread,
Feb 19, 2016, 1:30:45 PM2/19/16
to Oleg Mayba, knitr
I think that is a bug, and I just fixed it:
https://github.com/yihui/knitr Thanks!

BTW, next time if you are that deep in the knitr source code, you may
just send a pull request to fix the issue on Github :)

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
> --
> You received this message because you are subscribed to the Google Groups
> "knitr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to knitr+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Oleg Mayba

unread,
Feb 22, 2016, 12:36:04 PM2/22/16
to knitr, oleg....@gmail.com
Thanks for the quick response, Yihui!

I'm not a Github user (yet), but I'll follow your suggestion the next time!

Oleg.

Oleg Mayba

unread,
Feb 25, 2016, 4:50:53 PM2/25/16
to knitr, oleg....@gmail.com
Hi, Yihui

One more question: do you know when the new version will be available on CRAN? The system I work on automatically provides the latest CRAN versions of many packages, including knitr.

Thanks again!

Oleg.

Yihui Xie

unread,
Feb 26, 2016, 1:02:30 PM2/26/16
to Oleg Mayba, knitr
I don't have a fixed release plan. It is basically whenever I feel
like a new release is necessary. I guess the earliest time for the
next release is two weeks later.

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages