Skip to content

Commit 710a243

Browse files
atusyyihui
andauthored
fix #2344: an autodep regression due to #2321 (#2377)
Co-authored-by: Yihui Xie <xie@yihui.name>
1 parent 15da0ed commit 710a243

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: knitr
22
Type: Package
33
Title: A General-Purpose Package for Dynamic Report Generation in R
4-
Version: 1.48.9
4+
Version: 1.48.10
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person("Abhraneel", "Sarma", role = "ctb"),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

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

15+
- The chunk option `autodep = TRUE` stopped working due to a regression from #2321 (thanks, @heavywatal #2344, @atusy #2377).
16+
1517
- `asis_output()` was not passed to the `output` hook (thanks, @cderv, #2332).
1618

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

R/block.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ eng_r = function(options) {
341341
objs, cache_globals(options$cache.globals, code), options$label,
342342
options$cache.path
343343
)
344-
dep_auto(labels = options$label)
344+
if (isTRUE(opts_knit$get('autodep.initialized'))) {
345+
dep_auto(labels = options$label)
346+
} else {
347+
dep_auto(labels = all_labels())
348+
opts_knit$set(autodep.initialized = TRUE)
349+
}
345350
}
346351
if (options$cache < 3) {
347352
if (options$cache.rebuild || !cache.exists) block_cache(options, res.orig, objs)

R/cache.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ dep_auto = function(path = opts_chunk$get('cache.path'), labels = all_labels())
169169
}
170170
# parse objects in dependency files
171171
parse_objects = function(path) {
172-
if (!file.exists(path)) {
173-
warning('file ', path, ' not found'); return()
174-
}
172+
if (!file.exists(path)) return()
175173
lines = strsplit(read_utf8(path), '\t')
176174
if (length(lines) < 2L) return() # impossible for dependson
177175
objs = lapply(lines, `[`, -1L)

tests/testit/test-cache.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
library(testit)
22

3+
dep_list$restore()
4+
knit_code$restore()
5+
6+
37
assert('find_symbols() identifies all symbols', {
48
(find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z'))
59
})
@@ -30,3 +34,78 @@ assert('dep_prev() sets dependencies on previous chunks', {
3034
})
3135
dep_list$restore()
3236
knit_code$restore()
37+
38+
assert('dep_auto() solves dependencies', {
39+
# dependency is empty now
40+
(dep_list$get() %==% list())
41+
42+
# base rmd text
43+
rmd0 = c(
44+
'```{r, autodep=TRUE, cache=TRUE}',
45+
'x = %s',
46+
'```',
47+
'```{r, autodep=TRUE, cache=TRUE}',
48+
'print(x)',
49+
'```'
50+
)
51+
52+
td = tempfile()
53+
dir.create(td, showWarnings = FALSE, recursive = TRUE)
54+
55+
rmd1 = sprintf(rmd0, 'runif(1)')
56+
rmd2 = sprintf(rmd0, '"a"')
57+
58+
# without child document
59+
in_dir(td, {
60+
# with cache, the result should reproduce
61+
knit1 = knit(text = rmd1, quiet = TRUE)
62+
(knit(text = rmd1, quiet = TRUE) %==% knit1)
63+
64+
# on updating `x`, the printed result should change
65+
knit2 = knit(text = rmd2, quiet = TRUE)
66+
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
67+
(print2 %==% '## [1] "a"')
68+
})
69+
})
70+
dep_list$restore()
71+
knit_code$restore()
72+
73+
assert('dep_auto() solves dependencies of child documents', {
74+
# dependency is empty now
75+
(dep_list$get() %==% list())
76+
77+
# base rmd text
78+
rmd0 = c(
79+
'```{r, autodep=TRUE, cache=TRUE}',
80+
'x = %s',
81+
'```',
82+
'```{r, autodep=TRUE, cache=TRUE}',
83+
'print(x)',
84+
'```'
85+
)
86+
rmd1 = sprintf(rmd0, 'runif(1)')
87+
rmd2 = sprintf(rmd0, '"a"')
88+
89+
td = tempfile()
90+
dir.create(td, showWarnings = FALSE, recursive = TRUE)
91+
92+
# with child document
93+
parent = c(
94+
'```{r, child="child.Rmd"}',
95+
'```'
96+
)
97+
in_dir(td, {
98+
# with cache, the result should reproduce
99+
writeLines(rmd1, 'child.Rmd')
100+
knit1 = knit(text = parent, quiet = TRUE)
101+
(knit(text = parent, quiet = TRUE) %==% knit1)
102+
103+
# on updating `x`, the printed result should change
104+
writeLines(rmd2, 'child.Rmd')
105+
knit2 = knit(text = parent, quiet = TRUE)
106+
print2 = gsub('\n.*', '', gsub('.*\n##', '##', knit2))
107+
(print2 %==% '## [1] "a"')
108+
})
109+
})
110+
dep_list$restore()
111+
knit_code$restore()

0 commit comments

Comments
 (0)