[R] on lexical scoping....

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Apr 4 17:19:00 CEST 2023


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 <murdoch.duncan using gmail.com>
> *Sent:* Tuesday, April 4, 2023 8:21 PM
> *To:* akshay kulkarni <akshay_e4 using hotmail.com>; Deepayan Sarkar 
> <deepayan.sarkar using gmail.com>
> *Cc:* R help Mailing list <r-help using r-project.org>
> *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 <deepayan.sarkar using gmail.com>
>> Sent: Tuesday, April 4, 2023 7:51 PM
>> To: akshay kulkarni <akshay_e4 using hotmail.com>
>> Cc: R help Mailing list <r-help using r-project.org>
>> Subject: Re: [R] on lexical scoping....
>> 
>> 
>> 
>> On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using 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 using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>> 
>>        [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list