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

fix autodep regression by #2321 (closes #2344) #2377

Merged
merged 9 commits into from
Nov 5, 2024
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.48.9
Version: 1.48.10
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- In-chunk references of the form `<<label>>` should not be resolved if `label` is not found in the document (thanks, @jennybc @gadenbuie, #2360).

- The chunk option `autodep = TRUE` stopped working due to a regression from #2321 (thanks, @heavywatal #2344, @atusy #2377).

- `asis_output()` was not passed to the `output` hook (thanks, @cderv, #2332).

- Avoid partial matching of the `Date/Publication` field when generating `citation('knitr')`, otherwise R will emit a warning when `options(warnPartialMatchDollar = TRUE)` (thanks, @fkohrt, #2361).
Expand Down
7 changes: 6 additions & 1 deletion R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ eng_r = function(options) {
objs, cache_globals(options$cache.globals, code), options$label,
options$cache.path
)
dep_auto(labels = options$label)
if (isTRUE(opts_knit$get('autodep.initialized'))) {
dep_auto(labels = options$label)
} else {
dep_auto(labels = all_labels())
opts_knit$set(autodep.initialized = TRUE)
}
}
if (options$cache < 3) {
if (options$cache.rebuild || !cache.exists) block_cache(options, res.orig, objs)
Expand Down
4 changes: 1 addition & 3 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ dep_auto = function(path = opts_chunk$get('cache.path'), labels = all_labels())
}
# parse objects in dependency files
parse_objects = function(path) {
if (!file.exists(path)) {
warning('file ', path, ' not found'); return()
}
if (!file.exists(path)) return()
lines = strsplit(read_utf8(path), '\t')
if (length(lines) < 2L) return() # impossible for dependson
objs = lapply(lines, `[`, -1L)
Expand Down
79 changes: 79 additions & 0 deletions tests/testit/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
library(testit)

dep_list$restore()
knit_code$restore()


assert('find_symbols() identifies all symbols', {
(find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z'))
})
Expand Down Expand Up @@ -30,3 +34,78 @@ assert('dep_prev() sets dependencies on previous chunks', {
})
dep_list$restore()
knit_code$restore()

assert('dep_auto() solves dependencies', {
# dependency is empty now
(dep_list$get() %==% list())

# base rmd text
rmd0 = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = %s',
'```',
'```{r, autodep=TRUE, cache=TRUE}',
'print(x)',
'```'
)

td = tempfile()
dir.create(td, showWarnings = FALSE, recursive = TRUE)

rmd1 = sprintf(rmd0, 'runif(1)')
rmd2 = sprintf(rmd0, '"a"')

# without child document
in_dir(td, {
# with cache, the result should reproduce
knit1 = knit(text = rmd1, quiet = TRUE)
(knit(text = rmd1, quiet = TRUE) %==% knit1)

# on updating `x`, the printed result should change
knit2 = knit(text = rmd2, quiet = TRUE)
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
(print2 %==% '## [1] "a"')
})
})
dep_list$restore()
knit_code$restore()

assert('dep_auto() solves dependencies of child documents', {
# dependency is empty now
(dep_list$get() %==% list())

# base rmd text
rmd0 = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = %s',
'```',
'```{r, autodep=TRUE, cache=TRUE}',
'print(x)',
'```'
)
rmd1 = sprintf(rmd0, 'runif(1)')
rmd2 = sprintf(rmd0, '"a"')

td = tempfile()
dir.create(td, showWarnings = FALSE, recursive = TRUE)

# with child document
parent = c(
'```{r, child="child.Rmd"}',
'```'
)
in_dir(td, {
# with cache, the result should reproduce
writeLines(rmd1, 'child.Rmd')
knit1 = knit(text = parent, quiet = TRUE)
(knit(text = parent, quiet = TRUE) %==% knit1)

# on updating `x`, the printed result should change
writeLines(rmd2, 'child.Rmd')
knit2 = knit(text = parent, quiet = TRUE)
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
(print2 %==% '## [1] "a"')
})
})
dep_list$restore()
knit_code$restore()