Skip to content

Commit

Permalink
Rename .env to the (#156)
Browse files Browse the repository at this point in the history
This is the convention we've adopted in the tidyverse because `the` does a nice job of implying that the object is a singleton.
  • Loading branch information
hadley authored Jun 20, 2024
1 parent 14c9609 commit 21ebd59
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions R/evaluate-package.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#' @keywords internal
"_PACKAGE"

the <- new.env(parent = emptyenv())

## usethis namespace: start
## usethis namespace: end
NULL
12 changes: 6 additions & 6 deletions R/flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
#' either a direct `evaluate()` call or in \pkg{knitr} code chunks).
#' @export
flush_console = function() {
if (!is.null(.env$output_handler)) {
.env$output_handler()
if (!is.null(the$output_handler)) {
the$output_handler()
}
invisible()
}

.env = new.env()
.env$output_handler <- NULL

the$output_handler <- NULL

set_output_handler <- function(handler) {
old <- .env$output_handler
.env$output_handler <- handler
old <- the$output_handler
the$output_handler <- handler
invisible(old)
}

Expand Down
6 changes: 3 additions & 3 deletions R/inject-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
inject_funs <- function(...) {
funs <- list(...)
funs <- funs[names(funs) != '']
old <- .env$inject_funs
.env$inject_funs <- Filter(is.function, funs)
old <- the$inject_funs
the$inject_funs <- Filter(is.function, funs)

invisible(old)
}

local_inject_funs <- function(envir, frame = parent.frame()) {
funs <- .env$inject_funs
funs <- the$inject_funs
if (length(funs) == 0) {
return()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("flush_console() is a null op by default", {
test_that("can set and restore output handler", {
f <- function() message("Hi")
old <- set_output_handler(function() message("Hi"))
expect_equal(.env$output_handler, f)
expect_equal(the$output_handler, f)
expect_equal(old, NULL)

expect_message(flush_console(), "Hi")
Expand Down

0 comments on commit 21ebd59

Please sign in to comment.