Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Created March 6, 2024 03:16
Show Gist options
  • Save jmbarbone/aaf0c9a949f47590aa18ec2dfd932197 to your computer and use it in GitHub Desktop.
Save jmbarbone/aaf0c9a949f47590aa18ec2dfd932197 to your computer and use it in GitHub Desktop.
evaluate an R6 object within its enclosed environment
#' Evaluate an R6 object within its enclosed environment
#'
#' @param x An R6 object
#' @param expr An expression to run
#' @export
#' @examples
#' Foo <- R6::R6Class(
#' "Foo",
#' public = list(
#' hello = function() cat("hello\n")
#' ),
#' private = list(
#' goodbye = function() cat("bye\n")
#' )
#' )
#' foo <- Foo$new()
#' foo$hello()
#' try(foo$private$goodbye())
#' enclose(foo, private$goodbye())
enclose <- function(x, expr) {
stopifnot(inherits(x, "R6"))
eval(substitute(expr), get(".__enclos_env__", x))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment