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
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 (knit_state$get("autodep")) {
dep_auto(labels = options$label)
} else {
dep_auto(labels = all_labels())
knit_state$set("autodep", TRUE)
}
}
if (options$cache < 3) {
if (options$cache.rebuild || !cache.exists) block_cache(options, res.orig, objs)
Expand Down
6 changes: 6 additions & 0 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ knit = function(
input, output = NULL, tangle = FALSE, text = NULL, quiet = FALSE,
envir = parent.frame(), encoding = 'UTF-8'
) {
knit_state$restore()

in.file = !missing(input) && is.character(input) # is input provided?
oconc = knit_concord$get(); on.exit(knit_concord$set(oconc), add = TRUE)
Expand Down Expand Up @@ -437,6 +438,11 @@ knit_exit = function(append, fully = TRUE) {

knit_log = new_defaults() # knitr log for errors, warnings and messages

# miscellaneous states on knitting. gets resetted on every call to knit()
knit_state = new_defaults(list(
autodep = FALSE
))

#' Wrap evaluated results for output
#'
#' This function is mainly for internal use: it is called on each part of the
Expand Down
121 changes: 121 additions & 0 deletions tests/testit/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
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'))
})

knit_lazy = function(lazy = TRUE) {
if (TRUE) return(TRUE)
in_dir(tempdir(), {
txt = c(sprintf('```{r test, cache=TRUE, cache.lazy=%s}', lazy),
'x1 = Sys.time()', '```')
Expand All @@ -30,3 +35,119 @@ 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()

assert('dep_auto() solves dependencies of parent and child documents', {
yihui marked this conversation as resolved.
Show resolved Hide resolved
# dependency is empty now
(dep_list$get() %==% list())

# base rmd text
parent = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = runif(1)',
'```',
'```{r, child="child.Rmd"}',
'```',
'```{r, autodep=TRUE, cache=TRUE}',
'print(x)',
'```'
)
child = c(
'```{r, autodep=TRUE, cache=TRUE}',
'x = %s',
'```'
)

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

# with child document
in_dir(td, {
# with cache, the result should reproduce
writeLines(sprintf(child, 'runif(1)'), 'child.Rmd')
knit1 = knit(text = parent, quiet = TRUE)
(knit(text = parent, quiet = TRUE) %==% knit1)

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