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

autodep regression by #2321 #2344

Closed
3 tasks done
heavywatal opened this issue May 28, 2024 · 12 comments · Fixed by #2377
Closed
3 tasks done

autodep regression by #2321 #2344

heavywatal opened this issue May 28, 2024 · 12 comments · Fixed by #2377
Assignees
Labels
bug Bugs

Comments

@heavywatal
Copy link

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/knitr@v1.45"
  • OK: "yihui/knitr@74bcff85455a9f92a3099abecba1a8d70ab0cfa4" 74bcff8
  • NG: "yihui/knitr@f36e52cf39abba3be261e8e957c92587c63fa599" f36e52c
  • NG: "yihui/knitr@v1.46"
  • 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

  • I have fully read the issue guide at https://yihui.org/issue/.
  • 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.
  • 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
Copy link
Author

FYI, adding cache-globals does not help invalidation.

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

@yihui
Copy link
Owner

yihui commented May 28, 2024

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

@atusy
Copy link
Collaborator

atusy commented May 29, 2024

I will

@knokknok
Copy link
Contributor

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

@atusy
Copy link
Collaborator

atusy commented Oct 9, 2024

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

@heavywatal
Copy link
Author

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

@atusy
Copy link
Collaborator

atusy commented Oct 10, 2024

@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 added a commit to atusy/knitr that referenced this issue Oct 16, 2024
atusy added a commit to atusy/knitr that referenced this issue Oct 16, 2024
@atusy
Copy link
Collaborator

atusy commented Oct 16, 2024

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 added a commit to atusy/knitr that referenced this issue Oct 17, 2024
atusy added a commit to atusy/knitr that referenced this issue Oct 17, 2024
atusy added a commit to atusy/knitr that referenced this issue Oct 17, 2024
atusy added a commit to atusy/knitr that referenced this issue Oct 17, 2024
atusy added a commit to atusy/knitr that referenced this issue Oct 17, 2024
@yihui yihui added the bug Bugs label Nov 5, 2024
@yihui yihui closed this as completed in 710a243 Nov 5, 2024
@yihui
Copy link
Owner

yihui commented Nov 5, 2024

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
Copy link
Owner

yihui commented Nov 5, 2024

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.

@knokknok
Copy link
Contributor

knokknok commented Nov 7, 2024

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

@yihui
Copy link
Owner

yihui commented Nov 7, 2024

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs
Projects
None yet
4 participants