From pdalgd at gmail.com  Thu Oct 27 10:10:10 2016
From: pdalgd at gmail.com (Peter Dalgaard)
Date: Thu, 27 Oct 2016 10:10:10 +0200
Subject: [Rd] using with inside loop breaks next
In-Reply-To: <CAPp_+=dh_8cUxK6GxT6oNnK4HvZ1wBqZ+QWggicKiDmuzaGbxQ@mail.gmail.com>
References: <CAPp_+=dh_8cUxK6GxT6oNnK4HvZ1wBqZ+QWggicKiDmuzaGbxQ@mail.gmail.com>
Message-ID: <39AD6E38-BA48-4545-BC31-E366D40B306E@gmail.com>

(a)/(c) mostly, I think. The crux is that "next" is unhappy about being evaluated in a different environment than the containing loop. Witness this:


> for (i in 1:10) {if (i == 5) evalq(next); print(i)}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
> for (i in 1:10) {if (i == 5) evalq(next, new.env()); print(i)}
[1] 1
[1] 2
[1] 3
[1] 4
Error in eval(substitute(expr), envir, enclos) : 
  no loop for break/next, jumping to top level
> for (i in 1:10) {if (i == 5) evalq(next, parent.env(new.env())); print(i)}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

-pd



> On 27 Oct 2016, at 09:51 , Richard Cotton <richierocks at gmail.com> wrote:
> 
> If I want to use with inside a loop, it seems that next gets confused.
> To reproduce:
> 
> for(lst in list(list(a = 1), list(a = 2), list(a = 3)))
> {
>  with(lst, if(a == 2) next else print(a))
> }
> 
> I expect 1 and 3 to be printed, but I see
> 
> [1] 1
> Error in eval(expr, envir, enclos) :
>  no loop for break/next, jumping to top level
> 
> Is this
> a) by design, or
> b) a bug, or
> c) a thing that is rare enough that I should just rewrite my code?
> 
> -- 
> Regards,
> Richie
> 
> Learning R
> 4dpiecharts.com
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] using with inside loop breaks next
Peter Dalgaard