Skip to content

Commit

Permalink
make it possible to customize the knitting environment via knit_globa…
Browse files Browse the repository at this point in the history
…l(envir) (#2358)

Co-authored-by: Yihui Xie <xie@yihui.name>
  • Loading branch information
abhsarma and yihui authored Nov 1, 2024
1 parent 47267f5 commit c81043b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 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.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"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<<label>>` should not be resolved if `label` is not found in the document (thanks, @jennybc @gadenbuie, #2360).
Expand Down
19 changes: 10 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -561,18 +561,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
Expand Down
19 changes: 9 additions & 10 deletions man/knit_global.Rd

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

0 comments on commit c81043b

Please sign in to comment.