From ba1ae4c9806a69c85e3df0a2723dfa978795bfc2 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 25 Jan 2021 21:39:14 -0600 Subject: [PATCH] add an argument `fully` to knit_exit() to make it possible to exit the child document only (#1810) --- DESCRIPTION | 2 +- NEWS.md | 2 +- R/output.R | 10 +++++++--- man/knit_exit.Rd | 6 +++++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index afe6f2d670..77dd20151f 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.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"), diff --git a/NEWS.md b/NEWS.md index b09964229d..4ccba66828 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/output.R b/R/output.R index ab5ce026d3..ba62b9005d 100644 --- a/R/output.R +++ b/R/output.R @@ -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 } @@ -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{} 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')) '\n' else '' .knitEnv$terminate = append # use this terminate variable to notify knit() + .knitEnv$terminate_fully = fully invisible() } diff --git a/man/knit_exit.Rd b/man/knit_exit.Rd index 750215d5b9..9eb42f7f59 100644 --- a/man/knit_exit.Rd +++ b/man/knit_exit.Rd @@ -4,13 +4,17 @@ \alias{knit_exit} \title{Exit knitting early} \usage{ -knit_exit(append) +knit_exit(append, fully = TRUE) } \arguments{ \item{append}{A character vector to be appended to the results from \code{knit()} so far. By default, this is \samp{\end{document}} for LaTeX output, and \samp{} for HTML output, to make the output document complete. For other types of output, it is an empty string.} + +\item{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.} } \value{ Invisible \code{NULL}. An internal signal is set up (as a side