Created
March 6, 2024 03:16
-
-
Save jmbarbone/aaf0c9a949f47590aa18ec2dfd932197 to your computer and use it in GitHub Desktop.
evaluate an R6 object within its enclosed environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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