From f7b336bd70b589686a43ec603aad046e6b4fd017 Mon Sep 17 00:00:00 2001 From: Abhraneel Sarma Date: Mon, 9 May 2022 15:19:49 -0500 Subject: [PATCH 1/7] env as argument --- R/block.R | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/R/block.R b/R/block.R index c68655e683..06e2986ad5 100644 --- a/R/block.R +++ b/R/block.R @@ -168,10 +168,9 @@ block_exec = function(options) { #' @param options A list of chunk options. Usually this is just the object #' \code{options} associated with the current code chunk. #' @noRd -eng_r = function(options) { +eng_r = function(options, env = knit_global()) { # eval chunks (in an empty envir if cache) - env = knit_global() - obj.before = ls(globalenv(), all.names = TRUE) # global objects before chunk + obj.before = ls(env, all.names = TRUE) # global objects before chunk keep = options$fig.keep keep.idx = NULL @@ -288,16 +287,16 @@ eng_r = function(options) { 0 })) else 0L - # merge neighbor elements of the same class into one element + # # merge neighbor elements of the same class into one element for (cls in c('source', 'message', 'warning')) res = merge_class(res, cls) if (isTRUE(options$fig.beforecode)) res = fig_before_code(res) - on.exit({ - plot_counter(reset = TRUE) - shot_counter(reset = TRUE) - opts_knit$delete('plot_files') - }, add = TRUE) # restore plot number + # on.exit({ + # plot_counter(reset = TRUE) + # shot_counter(reset = TRUE) + # opts_knit$delete('plot_files') + # }, add = TRUE) # restore plot number output = unlist(sew(res, options)) # wrap all results together res.after = run_hooks(before = FALSE, options, env) # run 'after' hooks From 5a04a6203141950643667cf3dd052987b4bca8b5 Mon Sep 17 00:00:00 2001 From: Abhraneel Sarma Date: Tue, 30 Jul 2024 14:29:00 -0500 Subject: [PATCH 2/7] optional env argument to eng_r --- R/block.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/block.R b/R/block.R index d40ebedac6..077a4df2a6 100644 --- a/R/block.R +++ b/R/block.R @@ -307,16 +307,16 @@ eng_r = function(options, env = knit_global()) { 0 })) else 0L - # # merge neighbor elements of the same class into one element + # merge neighbor elements of the same class into one element for (cls in c('source', 'message', 'warning')) res = merge_class(res, cls) if (isTRUE(options$fig.beforecode)) res = fig_before_code(res) - # on.exit({ - # plot_counter(reset = TRUE) - # shot_counter(reset = TRUE) - # opts_knit$delete('plot_files') - # }, add = TRUE) # restore plot number + on.exit({ + plot_counter(reset = TRUE) + shot_counter(reset = TRUE) + opts_knit$delete('plot_files') + }, add = TRUE) # restore plot number output = unlist(sew(res, options)) # wrap all results together res.after = run_hooks(before = FALSE, options, env) # run 'after' hooks From 8a65d049c944d2909157be2e539925d193f053e8 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 1 Nov 2024 16:37:35 -0500 Subject: [PATCH 3/7] revert the changes --- R/block.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/block.R b/R/block.R index 077a4df2a6..0bf39d8631 100644 --- a/R/block.R +++ b/R/block.R @@ -184,9 +184,9 @@ block_exec = function(options) { #' @param options A list of chunk options. Usually this is just the object #' \code{options} associated with the current code chunk. #' @noRd -eng_r = function(options, env = knit_global()) { +eng_r = function(options) { # eval chunks (in an empty envir if cache) - obj.before = ls(env, all.names = TRUE) # global objects before chunk + obj.before = ls(knit_global(), all.names = TRUE) # global objects before chunk keep = options$fig.keep keep.idx = NULL From a13640c13d39ea702ba9f5fdcf1972e3cc04bb52 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 1 Nov 2024 16:44:50 -0500 Subject: [PATCH 4/7] knit_global() returns the environment, and knit_global(envir) sets a new environment to evaluate the code --- R/utils.R | 19 ++++++++++--------- man/knit_global.Rd | 19 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/R/utils.R b/R/utils.R index 1c7d2b48bd..f8c6288af7 100644 --- a/R/utils.R +++ b/R/utils.R @@ -564,18 +564,19 @@ fig_chunk = function(label, ext = '', number, fig.path = opts_chunk$get('fig.pat fig_path(ext, list(fig.path = fig.path, label = label), number) } -#' The global environment in which code chunks are evaluated +#' The global environment for evaluating code #' -#' This function makes the environment of a code chunk accessible inside a -#' chunk. +#' Get or set the environment in which code chunks are evaluated. #' -#' It returns the \code{envir} argument of \code{\link{knit}}, e.g. if we call -#' \code{\link{knit}()} in the global environment, \code{knit_global()} returns -#' R's global environment by default. You can call functions like -#' \code{\link{ls}()} on this environment. +#' @param envir If \code{NULL}, the function returns the \code{envir} argument +#' of \code{\link{knit}}, otherwise it should be a new environment for +#' evaluating code, in which case the function returns the old environment +#' after setting the new environment. #' @export -knit_global = function() { - .knitEnv$knit_global %n% globalenv() +knit_global = function(envir = NULL) { + old = .knitEnv$knit_global %n% globalenv() + if (!is.null(envir)) .knitEnv$knit_global = envir + old } # Indents a Block diff --git a/man/knit_global.Rd b/man/knit_global.Rd index 732c169662..db8ca17684 100644 --- a/man/knit_global.Rd +++ b/man/knit_global.Rd @@ -2,17 +2,16 @@ % Please edit documentation in R/utils.R \name{knit_global} \alias{knit_global} -\title{The global environment in which code chunks are evaluated} +\title{The global environment for evaluating code} \usage{ -knit_global() +knit_global(envir = NULL) } -\description{ -This function makes the environment of a code chunk accessible inside a -chunk. +\arguments{ +\item{envir}{If \code{NULL}, the function returns the \code{envir} argument +of \code{\link{knit}}, otherwise it should be a new environment for +evaluating code, in which case the function returns the old environment +after setting the new environment.} } -\details{ -It returns the \code{envir} argument of \code{\link{knit}}, e.g. if we call -\code{\link{knit}()} in the global environment, \code{knit_global()} returns -R's global environment by default. You can call functions like -\code{\link{ls}()} on this environment. +\description{ +Get or set the environment in which code chunks are evaluated. } From f2af153066af4a4a00c42aabf160951e600f0f29 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 1 Nov 2024 16:45:32 -0500 Subject: [PATCH 5/7] bump version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 70699d0e0f..3dbfa3d5f5 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.7 +Version: 1.48.8 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"), From 09753e70c0386a28a2c374f4ac57aba0aee599c4 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 1 Nov 2024 16:46:55 -0500 Subject: [PATCH 6/7] correctly revert original changes --- R/block.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/block.R b/R/block.R index 1e6799f352..14e3d0f10c 100644 --- a/R/block.R +++ b/R/block.R @@ -187,7 +187,8 @@ block_exec = function(options) { #' @noRd eng_r = function(options) { # eval chunks (in an empty envir if cache) - obj.before = ls(knit_global(), all.names = TRUE) # global objects before chunk + env = knit_global() + obj.before = ls(globalenv(), all.names = TRUE) # global objects before chunk keep = options$fig.keep keep.idx = NULL From 651e7dd085410e34eea067b6e830fadb0b6a7fb2 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 1 Nov 2024 16:56:11 -0500 Subject: [PATCH 7/7] add news [ci skip] --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9430600b6a..6857d0ed1d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ - Added support for `fig.alt` for LaTeX output, i.e., using `\includegraphics[alt={alt text}]` (thanks, @capnrefsmmat, #2378). +- The environment in which code chunks are evaluated can be changed by passing a custom environment to `knit_glbal()` now (thanks, @abhsarma, #2358). + ## BUG FIXES - In-chunk references of the form `<