From 88921d1c79a1ce0b7402a41e97e418ee9c072160 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:04:15 +0900 Subject: [PATCH 1/8] fix #2344: call dep_auto() automatically --- R/block.R | 7 ++++++- R/output.R | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/R/block.R b/R/block.R index 4c46fbaedc..6c867cdf55 100644 --- a/R/block.R +++ b/R/block.R @@ -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) diff --git a/R/output.R b/R/output.R index ed32c44a56..b5790d9b0d 100644 --- a/R/output.R +++ b/R/output.R @@ -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) @@ -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 From 8b06f7b32ad5241ae39326c1632ee2140ecb06b3 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:04:35 +0900 Subject: [PATCH 2/8] test #2344: call dep_auto() automatically --- tests/testit/test-cache.R | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/testit/test-cache.R b/tests/testit/test-cache.R index e4854ca359..38bc5c9cc7 100644 --- a/tests/testit/test-cache.R +++ b/tests/testit/test-cache.R @@ -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()', '```') @@ -30,3 +35,79 @@ 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() + From a30b587148d35813168e5e99e14d52e229003619 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:09:54 +0900 Subject: [PATCH 3/8] test #2344: additional tests on dep_auto() --- tests/testit/test-cache.R | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/testit/test-cache.R b/tests/testit/test-cache.R index 38bc5c9cc7..34893ddf39 100644 --- a/tests/testit/test-cache.R +++ b/tests/testit/test-cache.R @@ -111,3 +111,43 @@ assert('dep_auto() solves dependencies of child documents', { dep_list$restore() knit_code$restore() +assert('dep_auto() solves dependencies of parent and child documents', { + # 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() From 74d1327024e15c1b12712277d3ab9a954a5f6e9a Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:06:53 +0900 Subject: [PATCH 4/8] test!(cache): do not care cache dependencies among parent and child documents --- tests/testit/test-cache.R | 41 --------------------------------------- 1 file changed, 41 deletions(-) diff --git a/tests/testit/test-cache.R b/tests/testit/test-cache.R index 34893ddf39..eba55384d2 100644 --- a/tests/testit/test-cache.R +++ b/tests/testit/test-cache.R @@ -110,44 +110,3 @@ assert('dep_auto() solves dependencies of child documents', { }) dep_list$restore() knit_code$restore() - -assert('dep_auto() solves dependencies of parent and child documents', { - # 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() From e13300ae7f86a7c3f9d9839533a544bdd02edabc Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 4 Nov 2024 22:20:16 -0600 Subject: [PATCH 5/8] (ab)use opts_knit to store the state instead of creating a new object --- R/block.R | 4 ++-- R/output.R | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/R/block.R b/R/block.R index 81545d38e3..640173bd35 100644 --- a/R/block.R +++ b/R/block.R @@ -341,11 +341,11 @@ eng_r = function(options) { objs, cache_globals(options$cache.globals, code), options$label, options$cache.path ) - if (knit_state$get("autodep")) { + if (isTRUE(opts_knit$get('autodep.initialized'))) { dep_auto(labels = options$label) } else { dep_auto(labels = all_labels()) - knit_state$set("autodep", TRUE) + opts_knit$set(autodep.initialized = TRUE) } } if (options$cache < 3) { diff --git a/R/output.R b/R/output.R index b5790d9b0d..ed32c44a56 100644 --- a/R/output.R +++ b/R/output.R @@ -127,7 +127,6 @@ 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) @@ -438,11 +437,6 @@ 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 From 6438bce169970de907d826ff6bbbf9b16c1ec3a3 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 4 Nov 2024 22:20:55 -0600 Subject: [PATCH 6/8] looks like a superfluous change --- tests/testit/test-cache.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testit/test-cache.R b/tests/testit/test-cache.R index eba55384d2..9b598ed15c 100644 --- a/tests/testit/test-cache.R +++ b/tests/testit/test-cache.R @@ -9,7 +9,6 @@ assert('find_symbols() identifies all symbols', { }) 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()', '```') From 3aa1818ef612f7aac1570e64e36b01c643420d10 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 4 Nov 2024 22:27:59 -0600 Subject: [PATCH 7/8] add news --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 07a2c54200..c119098672 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Abhraneel", "Sarma", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index 387443fb19..9b2ff4f1c8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,8 @@ - In-chunk references of the form `<