Closed stancari closed 4 months ago
In this case, you have to manually specify the variables to be cached, using the cache.vars
option, which is provided exactly for this type of scenario: https://yihui.org/knitr/options/#cache
Technically, knitr uses codetools to detect local variables in a code chunk, and the automatic detection has no knowledge about the %<>%
operator, so it can only detect variables created via normal assignments, i.e., x
and Data
but not DF
.
codetools::findLocalsList(parse(text = '
x = x + 1
Data <- Data |> mutate(v = 2*u)
DF %<>% mutate(v = 2*u)
'))
Makes sense. Thank you.
Hi Yihui, thanks for your extraordinary work.
It looks like knitr does not recognize the magrittr assignment pipe operator %<>% when deciding which new objects created in a chunk need to be cached.
In the following minimal example, the number x and the tibble Data are correctly calculated, while DF (which should be identical to Data) is not.
After invalidating the cache (by changing the initial value of x, for instance, or the length of u) everything is OK. Running the same code a second time, x and Data are calculated correctly, while DF is not, giving an error:
Minimal example:
R version 4.3.1 (2023-06-16) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Ventura 13.6.3, RStudio 2023.6.1.524
Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
Package version: evaluate_0.23 graphics_4.3.1 grDevices_4.3.1 highr_0.10 knitr_1.45 methods_4.3.1
stats_4.3.1 tools_4.3.1 utils_4.3.1 xfun_0.41 yaml_2.3.8