dear Bert,
                  I could predict most of them, but,of course, you have not 
wasted my time!

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Bert Gunter <bgunter.4...@gmail.com>
Sent: Wednesday, April 5, 2023 4:36 AM
To: akshay kulkarni <akshay...@hotmail.com>
Cc: R help Mailing list <r-help@r-project.org>
Subject: Re: [R] on lexical scoping....

The following *might* be of use to you. If you can predict what the various 
function invocations will do, I think you have a reasonable grasp of how 
lexical scoping works in R (contrary or supplementary opinions welcome).  It is 
the sort of thing you will find in the references also. If this is all obvious, 
sorry for wasting your time.
#######################
search()
ls()
dat <- list(x =2)
attach(dat,2)
search()
f <- function(){
   g <- function() x
   x <- 3
   g}
h <- f()
g <- function()x
ls()
h()
g()
detach(dat)
h()
g()

##########################
## Here is what this gives starting with an empty .GlobalEnv.
##################################

> search()
 [1] ".GlobalEnv"        "package:tools"     "package:lattice"   "tools:rstudio"
 [5] "package:stats"     "package:graphics"  "package:grDevices" "package:utils"
 [9] "package:datasets"  "package:methods"   "Autoloads"         "package:base"
> ls()
character(0)
> dat <- list(x =2)
> attach(dat,2)
> search()
 [1] ".GlobalEnv"        "dat"               "package:tools"     
"package:lattice"
 [5] "tools:rstudio"     "package:stats"     "package:graphics"  
"package:grDevices"
 [9] "package:utils"     "package:datasets"  "package:methods"   "Autoloads"
[13] "package:base"
> f <- function(){
+    g <- function() x
+    x <- 3
+    g}
> h <- f()
> g <- function()x
> ls()
[1] "dat" "f"   "g"   "h"
> h()
[1] 3
> g()
[1] 2
> detach(dat)
> h()
[1] 3
> g()
Error in g() : object 'x' not found

-- Bert


On Tue, Apr 4, 2023 at 6:56 AM akshay kulkarni 
<akshay...@hotmail.com<mailto:akshay...@hotmail.com>> wrote:
Dear Members,
                             I have the following code typed at the console 
prompt:

y   <-   x*10

X has not been defined and the above code throws an object not found error. 
That is, the global environment does not contain x. Why doesn't it look further 
in the environment stack, like that of packages? There are thousands of 
packages that contain the variable named  x. Of course, that happens if the 
above code is in a function (or does it?).

What concept of R is at work in this dichotomy?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to