knitr icon indicating copy to clipboard operation
knitr copied to clipboard

autodep regression by #2321

Open heavywatal opened this issue 1 year ago • 8 comments
trafficstars

The option autodep=TRUE does not work since #2321 even with the simplest example as follows:

```{r, setup}
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(autodep = TRUE)
```

```{r, src}
x = 1
```

```{r, dst}
y = 10 * x
y
```

Modifying x in src chunk should invalidate the cache of the dependent chunk dst, but is ignored in the latest version. I have tried different versions with pak::pkg_install("yihui/knitr"). Here are the results:

  • OK: "yihui/[email protected]"
  • OK: "yihui/knitr@74bcff85455a9f92a3099abecba1a8d70ab0cfa4" 74bcff85
  • NG: "yihui/knitr@f36e52cf39abba3be261e8e957c92587c63fa599" f36e52cf
  • NG: "yihui/[email protected]"
  • NG: "yihui/knitr" HEAD
> xfun::session_info('knitr')
R version 4.4.0 (2024-04-24)
Platform: x86_64-apple-darwin20
Running under: macOS Sonoma 14.5

Locale: C / en_US.UTF-8 / C / C / C / C

Package version:
  evaluate_0.23   grDevices_4.4.0 graphics_4.4.0  highr_0.11
  knitr_1.46.5    methods_4.4.0   stats_4.4.0     tools_4.4.0
  utils_4.4.0     xfun_0.44       yaml_2.3.8

By filing an issue to this repo, I promise that

  • [x] I have fully read the issue guide at https://yihui.org/issue/.
  • [x] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

heavywatal avatar May 28 '24 07:05 heavywatal

FYI, adding cache-globals does not help invalidation.

```{r, dst2}
#| cache-globals:
#|   - x
z = 100 * x
z
```

heavywatal avatar May 28 '24 07:05 heavywatal

@knokknok @atusy Could you take a look? Thanks!

yihui avatar May 28 '24 15:05 yihui

I will

atusy avatar May 29 '24 06:05 atusy

Don't you need an initial dep_auto() to set-up the system?

knokknok avatar May 29 '24 14:05 knokknok

Sorry for being silent. I take a look at this issue.

atusy avatar Oct 09 '24 14:10 atusy

Thank you @atusy. Here is my current understanding:

  1. Both autodep = TRUE and knitr::dep_auto() are required in knitr>=v1.46.
  2. knitr::dep_auto() has long been there, but I did not know (or just forgot) its existence before @knokknok's suggestion because it is not documented in https://yihui.org/knitr/options/ and autodep = TRUE worked fine without it in knitr<=v1.45.
  3. knitr::dep_auto() emits warning when it is executed for the first time:
    Warning in parse_objects(paths[1L]): file .cache/__objects not found
    Warning in parse_objects(paths[2L]): file .cache/__globals not found
    

Now my setup chunk looks like this:

#| cache: false
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(autodep = TRUE)
if (dir.exists(knitr::opts_chunk$get("cache.path"))) knitr::dep_auto()  # NEW!

It seems a little redundant. How about modifying knitr::dep_auto()?

  • to set autodep = TRUE (and cache = TRUE) internally
  • not to emit warning when __objects and __globals are not found

heavywatal avatar Oct 09 '24 16:10 heavywatal

@heavywatal

Thank you, too. Your understanding is right.

I am trying to automate knitr::dep_auto(labels = all_labels()).

to set autodep = TRUE (and cache = TRUE) internally

I am against this because I remember a book (R Markdown Cookbook?) recommends cache = TRUE per chunks, not globally.

atusy avatar Oct 10 '24 15:10 atusy

I partially solved the issue by automatically calling dep_auto(labels = all_labels()) when the chunk option autodep = TRUE is found for the first time.

However, this implementation does not work well when there is a child document. I will try fix it, too.

atusy avatar Oct 16 '24 01:10 atusy

It seems a little redundant. How about modifying knitr::dep_auto()?

  • to set autodep = TRUE (and cache = TRUE) internally
  • not to emit warning when __objects and __globals are not found

Both items are done now. You don't need to run dep_auto() by yourself, and there won't be warnings at the first time when dep_auto() is called. Thanks!

yihui avatar Nov 05 '24 04:11 yihui

I forgot to mention that the cache system in litedown is much simpler and smarter than knitr's. The dependency structure of code chunks is automatically inferred from static analysis of the code, so in most cases, there's no need to use options like autodep or functions like dep_auto() in litedown, and things just work out of the box. There are several advantages of litedown's cache compared to knitr's, which I haven't had time to document yet.

yihui avatar Nov 05 '24 15:11 yihui

Hi @yihui , any chance this could be ported back to knitr (for latex users)?

knokknok avatar Nov 07 '24 14:11 knokknok

litedown's cache system has been factored out to xfun::cache_exec() so it can be reused elsewhere. In theory, we could use it in knitr, but I'm afraid it's not a few minutes' work...

yihui avatar Nov 07 '24 14:11 yihui

Confirmed it works as expected now with v1.49. Thank you, @atusy, @knokknok, and @yihui! (Sorry for my too late response, but I just cannot get away without expressing my gratitude.)

heavywatal avatar Mar 15 '25 10:03 heavywatal