Skip to content

Commit

Permalink
add an argument fully to knit_exit() to make it possible to exit th…
Browse files Browse the repository at this point in the history
…e child document only (#1810)
  • Loading branch information
yihui committed Jan 26, 2021
1 parent 222325a commit ba1ae4c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
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.30.8
Version: 1.30.9
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Adam", "Vogt", role = "ctb"),
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

## MAJOR CHANGES

- Previously `knit_exit()` in a child document would not terminate the processing of its parent document. Now it will (thanks, @davidchall, #1810).
- Previously `knit_exit()` in a child document would not terminate the knitting process of its parent document. Now it will terminate the whole process by default. You can use `knit_exit(fully = FALSE)` to exit the child document only (thanks, @davidchall, #1810).

## MINOR CHANGES

Expand Down
10 changes: 7 additions & 3 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ process_file = function(text, output) {
wd = getwd()
for (i in 1:n) {
if (!is.null(.knitEnv$terminate)) {
if (!child_mode()) {
if (!child_mode() || !.knitEnv$terminate_fully) {
# reset the internal variable `terminate` in the top parent
res[i] = one_string(.knitEnv$terminate)
knit_exit(NULL)
knit_exit(NULL, NULL)
}
break # must have called knit_exit(), so exit early
}
Expand Down Expand Up @@ -408,15 +408,19 @@ knit_child = function(..., options = NULL, envir = knit_global()) {
#' \code{knit()} so far. By default, this is \samp{\end{document}} for LaTeX
#' output, and \samp{</body></html>} for HTML output, to make the output
#' document complete. For other types of output, it is an empty string.
#' @param fully Whether to fully exit the knitting process if \code{knit_exit()}
#' is called from a child document. If \code{FALSE}, only exit the knitting
#' process of the child document.
#' @return Invisible \code{NULL}. An internal signal is set up (as a side
#' effect) to notify \code{knit()} to quit as if it had reached the end of the
#' document.
#' @export
#' @examples # see https://github.com/yihui/knitr-examples/blob/master/096-knit-exit.Rmd
knit_exit = function(append) {
knit_exit = function(append, fully = TRUE) {
if (missing(append)) append = if (out_format(c('latex', 'sweave', 'listings')))
'\\end{document}' else if (out_format('html')) '</body>\n</html>' else ''
.knitEnv$terminate = append # use this terminate variable to notify knit()
.knitEnv$terminate_fully = fully
invisible()
}

Expand Down
6 changes: 5 additions & 1 deletion man/knit_exit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba1ae4c

Please sign in to comment.