# Access the associated environment inside a function
self <- NULL
e <- new.env()
# The associated environment needs to have a reference to itself
e$self <- e
e$show_self <- function() return(self)
# The function can only access the global variable `self`
e$show_self()
# Bind the function to the environment `e`
bind_fn_2_env(env = e, e$show_self)
# Both point to the same environment
e$show_self()
e
Run the code above in your browser using DataLab