Re: [R] on lexical scoping....

2023-04-04 Thread Richard O'Keefe
R *does* search the environment stack.

> search()
[1] ".GlobalEnv""package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods"   "Autoloads" "package:base

What you seem to be missing is that a package may contain
bindings that it does not export, as the wording of this
error message reminds us:
> utils::y
Error: 'y' is not an exported object from 'namespace:utils'

So when package/namespace goes onto the environment stack,
it's only the *exported* bindings that become visible.



On Wed, 5 Apr 2023 at 01:56, akshay kulkarni  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 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.


Re: [R] on lexical scoping....

2023-04-04 Thread Jeff Newmiller
Leading off with you can only have two things in an environment definitely 
indicates this should be read with a skeptical eye.

Although the title of "Advanced R" may be more scary than someone writing notes 
on GitHub like a bro, IMHO Adv R is quite readable for anyone interested in 
questions like this with fewer wrong assertions to unlearn later.

On April 4, 2023 4:53:38 PM PDT, Mark Leeds  wrote:
>obviously, everyone has different opinions on what's useful but I always
>found this document quite
>helpful. I think, in the past, someone said that there are some incorrect
>statements in but I'm not sure
>what they are.
>
>https://askming.github.io/study_notes/Stats_Comp/Note-How%20R%20searches%20and%20finds%20stuff.html
>
>
>On Tue, Apr 4, 2023 at 7:06 PM Bert Gunter  wrote:
>
>> 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 
>> 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 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.
>>
>
>   [[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.

-- 
Sent from my phone. Please excuse my brevity.

__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread Mark Leeds
obviously, everyone has different opinions on what's useful but I always
found this document quite
helpful. I think, in the past, someone said that there are some incorrect
statements in but I'm not sure
what they are.

https://askming.github.io/study_notes/Stats_Comp/Note-How%20R%20searches%20and%20finds%20stuff.html


On Tue, Apr 4, 2023 at 7:06 PM Bert Gunter  wrote:

> 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 
> 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 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.
>

[[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.


Re: [R] on lexical scoping....

2023-04-04 Thread Bert Gunter
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 
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 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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Duncan,
 THanks a lot..!!

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Duncan Murdoch 
Sent: Tuesday, April 4, 2023 8:49 PM
To: akshay kulkarni ; Deepayan Sarkar 

Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping

No, there are lots of situations where that doesn't make sense.  You
don't want to have to define local copies of the functions from every
package you use, for example.

I think the takeaway is to learn how R scoping works, and keep things
simple.  That's one reason I tend to avoid "tidyverse" packages.  There
are a lot of really good ideas in those packages, but "tidy evaluation"
is far too complicated and hard to understand, and I think it confuses
people, so they don't understand the really very simple R scoping rules.

Duncan Murdoch

On 04/04/2023 10:59 a.m., akshay kulkarni wrote:
> Dear Duncan,
>   THanks for the reply...!
>
> So the takeaway is that define the symbol in the same environment before
> using it right!?
>
> Thanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> *From:* Duncan Murdoch 
> *Sent:* Tuesday, April 4, 2023 8:21 PM
> *To:* akshay kulkarni ; Deepayan Sarkar
> 
> *Cc:* R help Mailing list 
> *Subject:* Re: [R] on lexical scoping
> You can't change the basic way R searches, but you can ask for a
> different kind of search.  For example, to see if "x" exists, you can use
>
>exists("x")
>
> and it will do the default search, but
>
>exists("x", inherits = FALSE)
>
> will only look in the current environment.  The get() function has a
> similar argument which returns the value
>
> Unfortunately these functions have overly complicated argument lists
> because they are based on functions in S from 30-40 years ago, and it
> had very different scoping rules.  My advice would be to ignore the
> "where" and "frame" arguments, and always use "envir" if you want to say
> where to look.
>
> Duncan Murdoch
>
> On 04/04/2023 10:28 a.m., akshay kulkarni wrote:
>> Dear Deepayan,
>>THanks for the pithy, pointed reply.
>>
>> But isn't it risky? Can I somehow get a warning when x is not defined in the 
>> global environment but takes on a value from one of the loaded packages? any 
>> packages for that?
>>
>> THanking you,
>> Yours sincerely,
>> AKSHAY M KULKARNI
>> 
>> From: Deepayan Sarkar 
>> Sent: Tuesday, April 4, 2023 7:51 PM
>> To: akshay kulkarni 
>> Cc: R help Mailing list 
>> Subject: Re: [R] on lexical scoping
>>
>>
>>
>> On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
>> 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.
>>
>> That is not the correct interpretation of the error. R will happily evaluate
>>
>> y   <-   pi*10
>>
>> even if the global environment does not contain pi. The "environments" where 
>> R will look is given by
>>
>> search()
>>
>> If you manage to find a package that defines 'x' (and exports it), attaching 
>> it will put the package on the search path, and then your call will indeed 
>> no longer give an error.
>>
>> -Deepayan
>>
>> 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 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.
>


[[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 p

Re: [R] on lexical scoping....

2023-04-04 Thread Duncan Murdoch
No, there are lots of situations where that doesn't make sense.  You 
don't want to have to define local copies of the functions from every 
package you use, for example.


I think the takeaway is to learn how R scoping works, and keep things 
simple.  That's one reason I tend to avoid "tidyverse" packages.  There 
are a lot of really good ideas in those packages, but "tidy evaluation" 
is far too complicated and hard to understand, and I think it confuses 
people, so they don't understand the really very simple R scoping rules.


Duncan Murdoch

On 04/04/2023 10:59 a.m., akshay kulkarni wrote:

Dear Duncan,
                          THanks for the reply...!

So the takeaway is that define the symbol in the same environment before 
using it right!?


Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

*From:* Duncan Murdoch 
*Sent:* Tuesday, April 4, 2023 8:21 PM
*To:* akshay kulkarni ; Deepayan Sarkar 


*Cc:* R help Mailing list 
*Subject:* Re: [R] on lexical scoping
You can't change the basic way R searches, but you can ask for a
different kind of search.  For example, to see if "x" exists, you can use

   exists("x")

and it will do the default search, but

   exists("x", inherits = FALSE)

will only look in the current environment.  The get() function has a
similar argument which returns the value

Unfortunately these functions have overly complicated argument lists
because they are based on functions in S from 30-40 years ago, and it
had very different scoping rules.  My advice would be to ignore the
"where" and "frame" arguments, and always use "envir" if you want to say
where to look.

Duncan Murdoch

On 04/04/2023 10:28 a.m., akshay kulkarni wrote:

Dear Deepayan,
    THanks for the pithy, pointed reply.

But isn't it risky? Can I somehow get a warning when x is not defined in the 
global environment but takes on a value from one of the loaded packages? any 
packages for that?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Deepayan Sarkar 
Sent: Tuesday, April 4, 2023 7:51 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping



On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
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.

That is not the correct interpretation of the error. R will happily evaluate

y   <-   pi*10

even if the global environment does not contain pi. The "environments" where R 
will look is given by

search()

If you manage to find a package that defines 'x' (and exports it), attaching it 
will put the package on the search path, and then your call will indeed no 
longer give an error.

-Deepayan

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 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.




__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Duncan,
 THanks for the reply...!

So the takeaway is that define the symbol in the same environment before using 
it right!?

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Duncan Murdoch 
Sent: Tuesday, April 4, 2023 8:21 PM
To: akshay kulkarni ; Deepayan Sarkar 

Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping

You can't change the basic way R searches, but you can ask for a
different kind of search.  For example, to see if "x" exists, you can use

  exists("x")

and it will do the default search, but

  exists("x", inherits = FALSE)

will only look in the current environment.  The get() function has a
similar argument which returns the value

Unfortunately these functions have overly complicated argument lists
because they are based on functions in S from 30-40 years ago, and it
had very different scoping rules.  My advice would be to ignore the
"where" and "frame" arguments, and always use "envir" if you want to say
where to look.

Duncan Murdoch

On 04/04/2023 10:28 a.m., akshay kulkarni wrote:
> Dear Deepayan,
>THanks for the pithy, pointed reply.
>
> But isn't it risky? Can I somehow get a warning when x is not defined in the 
> global environment but takes on a value from one of the loaded packages? any 
> packages for that?
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Deepayan Sarkar 
> Sent: Tuesday, April 4, 2023 7:51 PM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] on lexical scoping
>
>
>
> On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
> 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.
>
> That is not the correct interpretation of the error. R will happily evaluate
>
> y   <-   pi*10
>
> even if the global environment does not contain pi. The "environments" where 
> R will look is given by
>
> search()
>
> If you manage to find a package that defines 'x' (and exports it), attaching 
> it will put the package on the search path, and then your call will indeed no 
> longer give an error.
>
> -Deepayan
>
> 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 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.


[[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.


Re: [R] on lexical scoping....

2023-04-04 Thread Duncan Murdoch
You can't change the basic way R searches, but you can ask for a 
different kind of search.  For example, to see if "x" exists, you can use


 exists("x")

and it will do the default search, but

 exists("x", inherits = FALSE)

will only look in the current environment.  The get() function has a 
similar argument which returns the value


Unfortunately these functions have overly complicated argument lists 
because they are based on functions in S from 30-40 years ago, and it 
had very different scoping rules.  My advice would be to ignore the 
"where" and "frame" arguments, and always use "envir" if you want to say 
where to look.


Duncan Murdoch

On 04/04/2023 10:28 a.m., akshay kulkarni wrote:

Dear Deepayan,
   THanks for the pithy, pointed reply.

But isn't it risky? Can I somehow get a warning when x is not defined in the 
global environment but takes on a value from one of the loaded packages? any 
packages for that?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Deepayan Sarkar 
Sent: Tuesday, April 4, 2023 7:51 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping



On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
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.

That is not the correct interpretation of the error. R will happily evaluate

y   <-   pi*10

even if the global environment does not contain pi. The "environments" where R 
will look is given by

search()

If you manage to find a package that defines 'x' (and exports it), attaching it 
will put the package on the search path, and then your call will indeed no 
longer give an error.

-Deepayan

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 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.


__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread Duncan Murdoch

On 04/04/2023 10:35 a.m., akshay kulkarni wrote:

Dear Duncan,
                          THanks for the reply.

I am looking at the technical point. The behavior you just described, as 
far as I know, is only for functions right? 


No, not at all.  Every function you write in R has an associated 
environment (which you can find using environment(fn)).  That only 
applies to searches taking place while evaluating the function.  The 
starting point is a temporary environment to hold local variables, 
called the "evaluation frame".  The parent of that environment is the 
environment given by environment(fn).


When you type something at the top level, evaluation is done very 
similarly, except that the starting point for the search is the global 
environment.


THre is no documentation
ever, which says that the code looks for x in the search path. Could you 
please point me to some resources where I can find further information 
on lexical scoping for the code "typed" in the console (but not in a 
function)?


See section 3.5.1 in the R Language Definition for a description of the 
relation between the global environment and the search list.  See 
section 2.1.10 in that manual for how R looks up variables.


Duncan Murdoch



THanking you,
Yours sincerely,
AKSHAY M KULKARNI


*From:* Duncan Murdoch 
*Sent:* Tuesday, April 4, 2023 7:48 PM
*To:* akshay kulkarni ; R help Mailing list 


*Subject:* Re: [R] on lexical scoping
On 04/04/2023 9:56 a.m., akshay kulkarni 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?



First, some background:

Packages are associated with multiple environments.  There is the
internal one that the package sees, and the external one that contains
just the exports.

These are sometimes called the "package" environment and the "namespace"
environment, but I don't think those names are used consistently.
(There's another one containing the imports, but for these purposes,
it's indistinguishable from the internal one.)

When a package is loaded by loadNamespace("pkg"), nothing happens in the
global environment:  no new variables are visible.

When it is attached by library("pkg"), a lot more happens.  First, it is
loaded as above, then the search list is modified.  The global
environment is entry 1 in the search list; it stays there, but its
"parent" is set to a copy of the external environment from the new
package, and the parent of that environment is set to the previous
parent, the second entry in the search list.

Okay, so now you search for "x" in the global environment, and it's not
there.  It then goes to the other entries in the search list, which are
typically external environments from various packages.  None of those
packages export "x", so it is not found.

It doesn't matter if those packages use "x" without exporting it,
because R won't look at internal environments in this kind of search.

And it doesn't matter what happens in other packages that are not on the
search list (i.e. not "attached" because you never called library() or
require() on them), because they just aren't in the chain of
environments where R looks.

Duncan Murdoch


__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Ducan,
Very informative! THanks a lot!

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Duncan Murdoch 
Sent: Tuesday, April 4, 2023 8:14 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] on lexical scoping

On 04/04/2023 10:35 a.m., akshay kulkarni wrote:
> Dear Duncan,
>   THanks for the reply.
>
> I am looking at the technical point. The behavior you just described, as
> far as I know, is only for functions right?

No, not at all.  Every function you write in R has an associated
environment (which you can find using environment(fn)).  That only
applies to searches taking place while evaluating the function.  The
starting point is a temporary environment to hold local variables,
called the "evaluation frame".  The parent of that environment is the
environment given by environment(fn).

When you type something at the top level, evaluation is done very
similarly, except that the starting point for the search is the global
environment.

THre is no documentation
> ever, which says that the code looks for x in the search path. Could you
> please point me to some resources where I can find further information
> on lexical scoping for the code "typed" in the console (but not in a
> function)?

See section 3.5.1 in the R Language Definition for a description of the
relation between the global environment and the search list.  See
section 2.1.10 in that manual for how R looks up variables.

Duncan Murdoch

>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
> 
> *From:* Duncan Murdoch 
> *Sent:* Tuesday, April 4, 2023 7:48 PM
> *To:* akshay kulkarni ; R help Mailing list
> 
> *Subject:* Re: [R] on lexical scoping
> On 04/04/2023 9:56 a.m., akshay kulkarni 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?
>>
>
> First, some background:
>
> Packages are associated with multiple environments.  There is the
> internal one that the package sees, and the external one that contains
> just the exports.
>
> These are sometimes called the "package" environment and the "namespace"
> environment, but I don't think those names are used consistently.
> (There's another one containing the imports, but for these purposes,
> it's indistinguishable from the internal one.)
>
> When a package is loaded by loadNamespace("pkg"), nothing happens in the
> global environment:  no new variables are visible.
>
> When it is attached by library("pkg"), a lot more happens.  First, it is
> loaded as above, then the search list is modified.  The global
> environment is entry 1 in the search list; it stays there, but its
> "parent" is set to a copy of the external environment from the new
> package, and the parent of that environment is set to the previous
> parent, the second entry in the search list.
>
> Okay, so now you search for "x" in the global environment, and it's not
> there.  It then goes to the other entries in the search list, which are
> typically external environments from various packages.  None of those
> packages export "x", so it is not found.
>
> It doesn't matter if those packages use "x" without exporting it,
> because R won't look at internal environments in this kind of search.
>
> And it doesn't matter what happens in other packages that are not on the
> search list (i.e. not "attached" because you never called library() or
> require() on them), because they just aren't in the chain of
> environments where R looks.
>
> Duncan Murdoch


[[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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Jeff,
  THanks a lot for the pithy reply...

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Jeff Newmiller 
Sent: Tuesday, April 4, 2023 7:43 PM
To: r-help@r-project.org ; akshay kulkarni 
; R help Mailing list 
Subject: Re: [R] on lexical scoping

Namespaces. Packages only export specific object names from their namespaces. 
But few instances of x would be found there.

Also, function argument lists are not added to the search path until the 
functions are running, and then the search path only goes through the 
environments in which the running functions were defined, not through the call 
stack.

Read Advanced R. [1]

[1] https://adv-r.hadley.nz/environments.html

On April 4, 2023 6:56:04 AM PDT, akshay kulkarni  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 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.

--
Sent from my phone. Please excuse my brevity.

[[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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Bert,
   THanks a lot. I will take a look at those...

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Bert Gunter 
Sent: Tuesday, April 4, 2023 7:48 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping

?search and ?environment

See also "The R Language Definition" manual for similar such questions.

-- Bert

On Tue, Apr 4, 2023 at 6:56 AM akshay kulkarni  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 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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Duncan,
 THanks for the reply.

I am looking at the technical point. The behavior you just described, as far as 
I know, is only for functions right? THre is no documentation ever, which says 
that the code looks for x in the search path. Could you please point me to some 
resources where I can find further information on lexical scoping for the code 
"typed" in the console (but not in a function)?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI


From: Duncan Murdoch 
Sent: Tuesday, April 4, 2023 7:48 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] on lexical scoping

On 04/04/2023 9:56 a.m., akshay kulkarni 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?
>

First, some background:

Packages are associated with multiple environments.  There is the
internal one that the package sees, and the external one that contains
just the exports.

These are sometimes called the "package" environment and the "namespace"
environment, but I don't think those names are used consistently.
(There's another one containing the imports, but for these purposes,
it's indistinguishable from the internal one.)

When a package is loaded by loadNamespace("pkg"), nothing happens in the
global environment:  no new variables are visible.

When it is attached by library("pkg"), a lot more happens.  First, it is
loaded as above, then the search list is modified.  The global
environment is entry 1 in the search list; it stays there, but its
"parent" is set to a copy of the external environment from the new
package, and the parent of that environment is set to the previous
parent, the second entry in the search list.

Okay, so now you search for "x" in the global environment, and it's not
there.  It then goes to the other entries in the search list, which are
typically external environments from various packages.  None of those
packages export "x", so it is not found.

It doesn't matter if those packages use "x" without exporting it,
because R won't look at internal environments in this kind of search.

And it doesn't matter what happens in other packages that are not on the
search list (i.e. not "attached" because you never called library() or
require() on them), because they just aren't in the chain of
environments where R looks.

Duncan Murdoch

[[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.


Re: [R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
Dear Deepayan,
  THanks for the pithy, pointed reply.

But isn't it risky? Can I somehow get a warning when x is not defined in the 
global environment but takes on a value from one of the loaded packages? any 
packages for that?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Deepayan Sarkar 
Sent: Tuesday, April 4, 2023 7:51 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] on lexical scoping



On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
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.

That is not the correct interpretation of the error. R will happily evaluate

y   <-   pi*10

even if the global environment does not contain pi. The "environments" where R 
will look is given by

search()

If you manage to find a package that defines 'x' (and exports it), attaching it 
will put the package on the search path, and then your call will indeed no 
longer give an error.

-Deepayan

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 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.


Re: [R] on lexical scoping....

2023-04-04 Thread Deepayan Sarkar
On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
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.


That is not the correct interpretation of the error. R will happily evaluate

y   <-   pi*10

even if the global environment does not contain pi. The "environments"
where R will look is given by

search()

If you manage to find a package that defines 'x' (and exports it),
attaching it will put the package on the search path, and then your call
will indeed no longer give an error.

-Deepayan

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 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.


Re: [R] on lexical scoping....

2023-04-04 Thread Duncan Murdoch

On 04/04/2023 9:56 a.m., akshay kulkarni 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?



First, some background:

Packages are associated with multiple environments.  There is the 
internal one that the package sees, and the external one that contains 
just the exports.


These are sometimes called the "package" environment and the "namespace" 
environment, but I don't think those names are used consistently. 
(There's another one containing the imports, but for these purposes, 
it's indistinguishable from the internal one.)


When a package is loaded by loadNamespace("pkg"), nothing happens in the 
global environment:  no new variables are visible.


When it is attached by library("pkg"), a lot more happens.  First, it is 
loaded as above, then the search list is modified.  The global 
environment is entry 1 in the search list; it stays there, but its 
"parent" is set to a copy of the external environment from the new 
package, and the parent of that environment is set to the previous 
parent, the second entry in the search list.


Okay, so now you search for "x" in the global environment, and it's not 
there.  It then goes to the other entries in the search list, which are 
typically external environments from various packages.  None of those 
packages export "x", so it is not found.


It doesn't matter if those packages use "x" without exporting it, 
because R won't look at internal environments in this kind of search.


And it doesn't matter what happens in other packages that are not on the 
search list (i.e. not "attached" because you never called library() or 
require() on them), because they just aren't in the chain of 
environments where R looks.


Duncan Murdoch

__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread Bert Gunter
?search and ?environment

See also "The R Language Definition" manual for similar such questions.

-- Bert

On Tue, Apr 4, 2023 at 6:56 AM akshay kulkarni  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 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.

__
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.


Re: [R] on lexical scoping....

2023-04-04 Thread Jeff Newmiller
Namespaces. Packages only export specific object names from their namespaces. 
But few instances of x would be found there.

Also, function argument lists are not added to the search path until the 
functions are running, and then the search path only goes through the 
environments in which the running functions were defined, not through the call 
stack.

Read Advanced R. [1]

[1] https://adv-r.hadley.nz/environments.html

On April 4, 2023 6:56:04 AM PDT, akshay kulkarni  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 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.

-- 
Sent from my phone. Please excuse my brevity.

__
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.


[R] on lexical scoping....

2023-04-04 Thread akshay kulkarni
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 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.


Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Ebert,Timothy Aaron
Originally this post was to just look at execution times for different 
approaches to solving this problem.
Now I have a question:
   I change the code for calculating a1 from c(c1, c2) to data.frame(c(c1,c2)). 
This changes the execution times of all the other variables. What am I missing?

Original
For efficiency, the answer Avi provided is still the best option with these 
data. The append() method is next. Both of these approaches avoid having to 
make a data frame in the wide format. The slowest method is pivot_longer(). 
Note that the order of elements is different in the pivot_longer() approach. If 
the order matters then some of these answers will need sorting to get the 
correct output. Also note that a1 and a2 are vectors, while the others are data 
frames. However, all of these appear correct from our understanding of the 
problem.

library(tidyverse)
library(microbenchmark)
c1 <- c("Tom","Dick")
c2 <- c("Larry","Curly")
res <- microbenchmark(a1 <- c(c1, c2),
  a2 <- append(c1, c2),
  a3 <- {c3 <- data.frame(Name1=c1, Name2=c2)
stack(c3)},
  a4 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=with(c3, c(Name1, Name2)))},
  a5 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=unlist(c3), row.names=NULL)},
  a6 <- {c3 <- data.frame(Name1=c1, Name2=c2)
pivot_longer(c3, cols=everything(),names_to="Names")},
  a7 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=c(c3$Name1,c3$Name2))},
  times=100L)
print(res)

Mean execution times for seven different methods where a1 <- c(c1,c2)
Method  Mean(ms)CLD
a1  1998a  
a2  5749a  
a3  1055501  b 
a4  592548   b 
a5  682491   b 
a6  6962660c 
a7  608337   b


Mean execution times for seven different methods where a1 <- 
data.frame(c(c1,c2))
Method  meancld
a1  272.467   b
a2  5.768   a
a3  907.171   d
a4  561.863 c
a5  581.989 c
a6  6371.465e
a7  552.208 c

-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Tuesday, April 4, 2023 8:21 AM
To: Sparks, John 
Cc: r-help@r-project.org
Subject: Re: [R] Simple Stacking of Two Columns

[External Email]

Just to repeat:
you have

   NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))

and you want

   NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))

There must be something I am missing, because

   NamesLong <- data.frame(Names = c(NamesWide$Name1, NamesWide$Name2))

appears to do the job in the simplest possible manner.  There are all sorts of 
alternatives, such as
   data.frame(Name = as.vector(as.matrix(NamesWide[, 1:2])))

As for stack(), the main problem there was a typo (Names2 for Name2).

> stack(NamesWide)
  values   ind
1Tom Name1
2   Dick Name1
3  Larry Name2
4  Curly Name2

If there were multiple columns, you might do

> stack(NamesWide[,c("Name1","Name2")])$values
[1] "Tom"   "Dick"  "Larry" "Curly"


On Tue, 4 Apr 2023 at 03:09, Sparks, John  wrote:

> Hi R-Helpers,
>
> Sorry to bother you, but I have a simple task that I can't figure out 
> how to do.
>
> For example, I have some names in two columns
>
> NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
>
> and I simply want to get a single column
> NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
> > NamesLong
>   Names
> 1   Tom
> 2  Dick
> 3 Larry
> 4 Curly
>
>
> Stack produces an error
> NamesLong<-stack(NamesWide$Name1,NamesWide$Names2)
> Error in if (drop) { : argument is of length zero
>
> So does bind_rows
> > NamesLong<-dplyr::bind_rows(NamesWide$Name1,NamesWide$Name2)
> Error in `dplyr::bind_rows()`:
> ! Argument 1 must be a data frame or a named atomic vector.
> Run `rlang::last_error()` to see where the error occurred.
>
> I tried making separate dataframes to get around the error in 
> bind_rows but it puts the data in two different columns
> Name1<-data.frame(c("Tom","Dick"))
> Name2<-data.frame(c("Larry","Curly"))
> NamesLong<-dplyr::bind_rows(Name1,Name2)
> > NamesLong
>   c..TomDick.. c..LarryCurly..
> 1  Tom
> 2 Dick
> 3Larry
> 4Curly
>
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
> > NamesLong
>   Name1 Name2
> 1   Tom Larry
> 2  Dick Curly
>
>
> Please help me solve what should be a very simple problem.
>
> Thanks,
> John Sparks
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> 

Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Richard O'Keefe
Just to repeat:
you have

   NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))

and you want

   NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))

There must be something I am missing, because

   NamesLong <- data.frame(Names = c(NamesWide$Name1, NamesWide$Name2))

appears to do the job in the simplest possible manner.  There are all sorts
of alternatives, such as
   data.frame(Name = as.vector(as.matrix(NamesWide[, 1:2])))

As for stack(), the main problem there was a typo (Names2 for Name2).

> stack(NamesWide)
  values   ind
1Tom Name1
2   Dick Name1
3  Larry Name2
4  Curly Name2

If there were multiple columns, you might do

> stack(NamesWide[,c("Name1","Name2")])$values
[1] "Tom"   "Dick"  "Larry" "Curly"


On Tue, 4 Apr 2023 at 03:09, Sparks, John  wrote:

> Hi R-Helpers,
>
> Sorry to bother you, but I have a simple task that I can't figure out how
> to do.
>
> For example, I have some names in two columns
>
> NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
>
> and I simply want to get a single column
> NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
> > NamesLong
>   Names
> 1   Tom
> 2  Dick
> 3 Larry
> 4 Curly
>
>
> Stack produces an error
> NamesLong<-stack(NamesWide$Name1,NamesWide$Names2)
> Error in if (drop) { : argument is of length zero
>
> So does bind_rows
> > NamesLong<-dplyr::bind_rows(NamesWide$Name1,NamesWide$Name2)
> Error in `dplyr::bind_rows()`:
> ! Argument 1 must be a data frame or a named atomic vector.
> Run `rlang::last_error()` to see where the error occurred.
>
> I tried making separate dataframes to get around the error in bind_rows
> but it puts the data in two different columns
> Name1<-data.frame(c("Tom","Dick"))
> Name2<-data.frame(c("Larry","Curly"))
> NamesLong<-dplyr::bind_rows(Name1,Name2)
> > NamesLong
>   c..TomDick.. c..LarryCurly..
> 1  Tom
> 2 Dick
> 3Larry
> 4Curly
>
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
> > NamesLong
>   Name1 Name2
> 1   Tom Larry
> 2  Dick Curly
>
>
> Please help me solve what should be a very simple problem.
>
> Thanks,
> John Sparks
>
>
>
>
>
> [[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.
>

[[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.


Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Kimmo Elo
Hi,

or maybe this?

NamesLong<-data.frame(Names=unlist(NamesWide), row.names = NULL)

HTH,

Kimmo

ma, 2023-04-03 kello 16:23 +, Ebert,Timothy Aaron kirjoitti:
> My first thought was pivot_longer, and stack() is new to me. 
> How about append(c1,c2) as another solution? Or
> data.frame(append(c1,c2)) if you want that form.
> 
> Tim
> 
> -Original Message-
> From: R-help  On Behalf Of Marc
> Schwartz via R-help
> Sent: Monday, April 3, 2023 11:44 AM
> To: Sparks, John ; r-help@r-project.org
> Subject: Re: [R] Simple Stacking of Two Columns
> 
> [External Email]
> 
> Hi,
> 
> You were on the right track using stack(), but you just pass the
> entire data frame as a single object, not the separate columns:
> 
> > stack(NamesWide)
>   values   ind
> 1    Tom Name1
> 2   Dick Name1
> 3  Larry Name2
> 4  Curly Name2
> 
> Note that stack also returns the index (second column of 'ind'
> values), which tells you which column in the source data frame the
> stacked values originated from.
> 
> Thus, if you just want the actual data:
> 
> > stack(NamesWide)$values
> [1] "Tom"   "Dick"  "Larry" "Curly"
> 
> returns a vector, or:
> 
> > stack(NamesWide)[, 1, drop = FALSE]
>   values
> 1    Tom
> 2   Dick
> 3  Larry
> 4  Curly
> 
> which returns a data frame with a single column named 'values'.
> 
> Regards,
> 
> Marc Schwartz
> 
> 
> On April 3, 2023 at 11:08:59 AM, Sparks, John
> (jspa...@uic.edu (mailto:jspa...@uic.edu)) wrote:
> 
> > Hi R-Helpers,
> > 
> > Sorry to bother you, but I have a simple task that I can't figure
> > out how to do.
> > 
> > For example, I have some names in two columns
> > 
> > NamesWide<-
> > data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
> > 
> > and I simply want to get a single column
> > NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
> > > NamesLong
> > Names
> > 1 Tom
> > 2 Dick
> > 3 Larry
> > 4 Curly
> > 
> > 
> > Stack produces an error
> > NamesLong<-stack(NamesWide$Name1,NamesWide$Names2)
> > Error in if (drop) { : argument is of length zero
> > 
> > So does bind_rows
> > > NamesLong<-dplyr::bind_rows(NamesWide$Name1,NamesWide$Name2)
> > Error in `dplyr::bind_rows()`:
> > ! Argument 1 must be a data frame or a named atomic vector.
> > Run `rlang::last_error()` to see where the error occurred.
> > 
> > I tried making separate dataframes to get around the error in
> > bind_rows but it puts the data in two different columns
> > Name1<-data.frame(c("Tom","Dick"))
> > Name2<-data.frame(c("Larry","Curly"))
> > NamesLong<-dplyr::bind_rows(Name1,Name2)
> > > NamesLong
> > c..TomDick.. c..LarryCurly..
> > 1 Tom
> > 2 Dick
> > 3 Larry
> > 4 Curly
> > 
> > gather makes no change to the data
> > NamesLong<-gather(NamesWide,Name1,Name2)
> > > NamesLong
> > Name1 Name2
> > 1 Tom Larry
> > 2 Dick Curly
> > 
> > 
> > Please help me solve what should be a very simple problem.
> > 
> > Thanks,
> > John Sparks
> > 
> > 
> > 
> > 
> > 
> > [[alternative HTML version deleted]]
> > 
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MEaORpaFihsIHu3Iu2GwO15ey%2BvZP3Wxa6UiS3g0PyQ%3D&reserved=0
> > PLEASE do read the posting guide
> > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZwKZkaGoEVMu8Jp%2BbcIj%2FLVi9%2Fwug%2Fi48uarb8yg5KY%3D&reserved=0
> > and provide commented, minimal, self-contained, reproducible code.
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MEaORpaFihsIHu3Iu2GwO15ey%2BvZP3Wxa6UiS3g0PyQ%3D&reserved=0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZwKZkaGoEVMu8Jp%2BbcIj%2FLVi9%