Skip to content
Prev 393629 / 398503 Next

return value of {....}

Sorry, John. If I understand you correctly, R has no "Global
Variables" in the sense that you seem to indicate. It does have a
"Global environment", but variables referred to in a function but not
found in the function environment are *not* necessarily searched for
in the "Global Environment" -- they are searched for in the function's
enclosing (defining) environment, then in that environment's enclosing
environment if not found there, etc.

Consider:
f <- f <- function(){
   x <- 3
   g <- function() x  ## x not bound to a value in g's environment
   g ## f returns the function g
}
<environment: R_GlobalEnv>
x <- 5 ## defined in the Global environment
g <- f()
## Now what do you think g() returns?

If I have misconstrued what you said, my apologies.
Even if I have not, none of the above is revelatory -- it's all
standard, documented R behavior.

Cheers,
Bert

Cheers,
Bert



while something like d$something <- ... may seem like you're directly
modifying the data it does not actually do so. Most R objects try to
be immutable, that is, the object may not change after creation. This
guarantees that if you have a binding for same object the object won't
change sneakily.

There is a data structure that is in fact mutable which are
environments. For example compare

L <- list()
local({L$a <- 3})
L$a

with

E <- new.env()
local({E$a <- 3})
E$

Valentin Petzel valentin at petzel.at via r-project.org

2:25 AM (5 hours ago)


to avi.e.gross, R-help
On Sun, Jan 15, 2023 at 5:07 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote:
Message-ID: <CAGxFJbQxEw+C6CG8OuEB_C3BH02fm92H+pZt_yD1ou_xaAm3cQ@mail.gmail.com>
In-Reply-To: <MW4PR03MB63634102EDFD608D21BCE633E2C19@MW4PR03MB6363.namprd03.prod.outlook.com>

Thread (32 messages)

akshay kulkarni return value of {....} Jan 9 Valentin Petzel return value of {....} Jan 9 Rui Barradas return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 Andrew Simmons return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 9 Jeff Newmiller return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 10 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 10 akshay kulkarni return value of {....} Jan 10 Richard O'Keefe return value of {....} Jan 10 akshay kulkarni return value of {....} Jan 11 Valentin Petzel return value of {....} Jan 12 Heinz Tuechler return value of {....} Jan 13 Bill Dunlap return value of {....} Jan 13 akshay kulkarni return value of {....} Jan 15 akshay kulkarni return value of {....} Jan 15 akshay kulkarni return value of {....} Jan 15 Richard O'Keefe return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15 Bert Gunter return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15 Bert Gunter return value of {....} Jan 15 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 15 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15