[R-pkg-devel] mget with Inherits Not Finding Variable in Caller
Berry Boessenkool
berryboe@@enkoo| @end|ng |rom hotm@||@com
Wed Dec 1 09:25:46 CET 2021
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 <r-package-devel-bounces using r-project.org> on behalf of Simon Urbanek <simon.urbanek using R-project.org>
Sent: Wednesday, December 1, 2021 00:25
To: Dario Strbenac <dstr7320 using uni.sydney.edu.au>
Cc: r-package-devel using r-project.org <r-package-devel using r-project.org>
Subject: Re: [R-pkg-devel] mget with Inherits Not Finding Variable in Caller
Dario,
> On Dec 1, 2021, at 12:00 PM, Dario Strbenac <dstr7320 using uni.sydney.edu.au> 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
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
______________________________________________
R-package-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel
[[alternative HTML version deleted]]
More information about the R-package-devel
mailing list