I had the same problem recently when developing a package and have some code
here that might help you understand scoping a bit more.
It highlights the subtle difference between parent frame and environment.
I don't want to spam this list, so feel free to ignore ;)
Berry
a <- function(x) {aa <- "stuff"; b(x)}
b <- function(y) {
print(ls(parent.frame())) # parent.frame: aa, x env in which
the function was called - dynamic scoping
print(ls(parent.env(environment())) )# parent.env: a, b enclosing env in
which function was defined - lexical scoping
message(aa) # # error: 'aa' not found - dynGet("aa") would work
}
a(7)
a <- function(x) {aa <- "stuff"; b(x)}
b <- function(y) {env <- parent.frame(); eval(substitute(aa), env)}
a(7)
b <- function(y) {eval.parent(substitute(aa))}
a(7)
b <- function(y) {eval.parent(aa)}
a(7) # aa not found: when passed to eval.parent, it is evaluated first (unless
substituted)
________________________________
From: R-package-devel <[email protected]> on behalf of
Simon Urbanek <[email protected]>
Sent: Wednesday, December 1, 2021 00:25
To: Dario Strbenac <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: [R-pkg-devel] mget with Inherits Not Finding Variable in Caller
Dario,
> On Dec 1, 2021, at 12:00 PM, Dario Strbenac <[email protected]>
> wrote:
>
> Good day,
>
> What I am misunderstanding about the inherits = TRUE option of mget? I expect
> the small example to work.
>
> f <- function(x, .iteration = i) g()
> g <- function() mget(".iteration", inherits = TRUE)
> f(10, 1)
> Error: value for �.iteration� not found
>
That has nothing to do with inherits and is expected - it's identical to
> f <- function(x, .iteration = i) g()
> g <- function() .iteration
> f(10, 1)
Error in g() : object '.iteration' not found
Please note that R is lexically scoped and you defined g in the global
environment so it has no way of seeing inside f. This would work:
> f <- function(x, .iteration = i) {
+ g <- function() .iteration
+ g()
+ }
> f(10, 1)
[1] 1
since then the environment of f is the parent env of g.
If you want dynamic scoping (not what R uses!) you can use dynGet():
> f <- function(x, .iteration = i) g()
> g <- function() dynGet(".iteration")
> f(10,1)
[1] 1
but since that is non-standard the docs warn:
�dynGet()� is somewhat experimental and to be used _inside_
another function. It looks for an object in the callers, i.e.,
the �sys.frame()�s of the function. Use with caution.
Cheers,
Simon
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel