Re: [R] Lexical scoping is not what I expect

2013-07-01 Thread peter dalgaard
On Jun 30, 2013, at 14:35 , Duncan Murdoch wrote: On 13-06-29 11:58 PM, Greg Snow wrote: If you want to write really confusing code it is possible to do: `1` - 2 `1` + 1 and things like that, but it is probably a good idea not to. This is actually pretty simple, as the White Knight

Re: [R] Lexical scoping is not what I expect

2013-06-30 Thread Rolf Turner
On 30/06/13 15:58, Greg Snow wrote: If you want to write really confusing code it is possible to do: `1` - 2 `1` + 1 and things like that, but it is probably a good idea not to. Fortune? cheers, Rolf __ R-help@r-project.org mailing

Re: [R] Lexical scoping is not what I expect

2013-06-30 Thread Duncan Murdoch
On 13-06-29 11:58 PM, Greg Snow wrote: If you want to write really confusing code it is possible to do: `1` - 2 `1` + 1 and things like that, but it is probably a good idea not to. This is actually pretty simple, as the White Knight could tell you. `1` is what the name of 2 is called.

Re: [R] Lexical scoping is not what I expect

2013-06-30 Thread David Winsemius
On Jun 30, 2013, at 5:35 AM, Duncan Murdoch wrote: On 13-06-29 11:58 PM, Greg Snow wrote: If you want to write really confusing code it is possible to do: `1` - 2 `1` + 1 and things like that, but it is probably a good idea not to. This is actually pretty simple, as the White

Re: [R] Lexical scoping is not what I expect

2013-06-29 Thread William Dunlap
-project.org; John Fox Subject: Re: [R] Lexical scoping is not what I expect I just realized this was also possible: assign('TRUE', FALSE) TRUE [1] TRUE get('TRUE') [1] FALSE but it is probably a different story. Regards, Yihui -- Yihui Xie xieyi...@gmail.com Phone: 206-667-4385 Web

Re: [R] Lexical scoping is not what I expect

2013-06-29 Thread Greg Snow
If you want to write really confusing code it is possible to do: `1` - 2 `1` + 1 and things like that, but it is probably a good idea not to. On Fri, Jun 28, 2013 at 7:30 PM, Rolf Turner rolf.tur...@xtra.co.nz wrote: On 29/06/13 02:54, John Fox wrote: Dear Duncan and Steve, Since

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread S Ellison
I too find R's lexical scoping rules straightforward. However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. Except of course that almost every function relies on lexical scoping to some extent! This could get messy,

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread Duncan Murdoch
On 28/06/2013 9:28 AM, S Ellison wrote: I too find R's lexical scoping rules straightforward. However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. Except of course that almost every function relies on lexical scoping

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread John Fox
Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Duncan Murdoch Sent: Friday, June 28, 2013 9:40 AM To: S Ellison Cc: r-help@r-project.org Subject: Re: [R] Lexical scoping is not what I expect On 28/06/2013 9:28 AM, S Ellison wrote: I

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread Duncan Murdoch
Subject: Re: [R] Lexical scoping is not what I expect On 28/06/2013 9:28 AM, S Ellison wrote: I too find R's lexical scoping rules straightforward. However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. Except

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread Prof Brian Ripley
@r-project.org Subject: Re: [R] Lexical scoping is not what I expect On 28/06/2013 9:28 AM, S Ellison wrote: I too find R's lexical scoping rules straightforward. However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. Except

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread John Fox
, 2013 11:07 AM To: John Fox Cc: 'S Ellison'; r-help@r-project.org Subject: Re: [R] Lexical scoping is not what I expect On 28/06/2013 10:54 AM, John Fox wrote: Dear Duncan and Steve, Since Steve's example raises it, I've never understood why it's legal to change the built-in global

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread John Fox
Dear Brian, -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Prof Brian Ripley Sent: Friday, June 28, 2013 11:16 AM To: r-help@r-project.org Subject: Re: [R] Lexical scoping is not what I expect On 28/06/2013 15:54, John

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread Rolf Turner
On 29/06/13 02:54, John Fox wrote: Dear Duncan and Steve, Since Steve's example raises it, I've never understood why it's legal to change the built-in global constants in R, including T and F. That just seems to me to set a trap for users. Why not treat these as reserved symbols, like TRUE,

Re: [R] Lexical scoping is not what I expect

2013-06-28 Thread Yihui Xie
I just realized this was also possible: assign('TRUE', FALSE) TRUE [1] TRUE get('TRUE') [1] FALSE but it is probably a different story. Regards, Yihui -- Yihui Xie xieyi...@gmail.com Phone: 206-667-4385 Web: http://yihui.name Fred Hutchinson Cancer Research Center, Seattle On Fri, Jun 28,

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Terry Therneau
I second Ellison sentiments of almost never. One main reason is readability on later viewing. Yes, as Duncan says global variables can sometimes be handy and make functions quick to write, but using a formal argument in the call is always clearer. Terry Therneau On 06/27/2013 05:00 AM,

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Duncan Murdoch
On 13-06-27 8:18 AM, Terry Therneau wrote: I second Ellison sentiments of almost never. One main reason is readability on later viewing. Yes, as Duncan says global variables can sometimes be handy and make functions quick to write, but using a formal argument in the call is always clearer.

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Terry Therneau
Duncan, I disagree with Duncan was not at all the intent of my note, but on rereading it does have that flavor. Chastisement accepted. Due to the documentation angle I'd simply change your original maybe to sometimes maybe. A bit more caution but the same message. Terry T. On 06/27/2013

Re: [R] Lexical scoping is not what I expect

2013-06-27 Thread Hadley Wickham
I too find R's lexical scoping rules straightforward. However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. Except of course that almost every function relies on lexical scoping to some extent! Do you want: f - function(a, b)

Re: [R] Lexical scoping is not what I expect

2013-06-26 Thread S Ellison
-Original Message- It may be helpful not to worry about the technical details, just to look at the source code defining the function: if it is defined in a place where a variable can be seen, it can see that variable. I too find R's lexical scoping rules straightforward.

Re: [R] Lexical scoping is not what I expect

2013-06-26 Thread Duncan Murdoch
On 13-06-26 6:57 AM, S Ellison wrote: -Original Message- It may be helpful not to worry about the technical details, just to look at the source code defining the function: if it is defined in a place where a variable can be seen, it can see that variable. I too find R's lexical

Re: [R] Lexical scoping is not what I expect

2013-06-25 Thread Duncan Murdoch
On 13-06-24 9:22 PM, David Kulp wrote: Indeed, I misread / misunderstood. I think it's a difficult concept that's hard to explain and the example wasn't great. But thanks all for straightening me out! It seems like a really natural definition to me, but I'm used to it. Once you get used to it

[R] Lexical scoping is not what I expect

2013-06-24 Thread David Kulp
According to http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf and other examples online, I am to believe that R resolves variables using lexical scoping by following the frames up the call stack. However, that's not working for me. For example, the following code, taken

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread Rui Barradas
Hello, The object 'a' exists if function f() not in the global environment where g() is defined. R is in fact going up, but to the global environment and not finding 'a'. Try, as an example, the following. f - function(x) { g - function(y) { y + a } a - 5 g(x) }

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread Gabor Grothendieck
On Mon, Jun 24, 2013 at 4:27 PM, David Kulp dk...@fiksu.com wrote: According to http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf and other examples online, I am to believe that R resolves variables using lexical scoping by following the frames up the call stack.

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread arun
 f1- function(x){env- parent.frame();env$a-5; g(x)} f1(2) #[1] 7  f1(7) #[1] 12  f1(5) #[1] 10 A.K. From: David Kulp dk...@fiksu.com To: r-help@r-project.org r-help@r-project.org Sent: Monday, June 24, 2013 4:27 PM Subject: [R] Lexical scoping is not what I

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread Rolf Turner
I hope that Robert Gentleman is currently getting a thrill. :-) [See fortune(lexical scoping).] cheers, Rolf Turner __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread Marc Schwartz
On Jun 24, 2013, at 3:27 PM, David Kulp dk...@fiksu.com wrote: According to http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf and other examples online, I am to believe that R resolves variables using lexical scoping by following the frames up the call stack.

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread Duncan Murdoch
On 13-06-24 4:27 PM, David Kulp wrote: According to http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf and other examples online, I am to believe that R resolves variables using lexical scoping by following the frames up the call stack. You appear to have misread it.

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread David Kulp
Indeed, I misread / misunderstood. I think it's a difficult concept that's hard to explain and the example wasn't great. But thanks all for straightening me out! — David Kulp On Mon, Jun 24, 2013 at 6:44 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 13-06-24 4:27 PM, David Kulp

Re: [R] Lexical scoping is not what I expect

2013-06-24 Thread William Dunlap
tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Kulp Sent: Monday, June 24, 2013 6:22 PM To: Duncan Murdoch Cc: r-help@r-project.org Subject: Re: [R] Lexical scoping is not what I expect Indeed, I misread