[R] browser() question

Michael Weylandt michael.weylandt at gmail.com
Wed Jul 11 14:18:24 CEST 2012


Incidentally, the OP might want to take a look at ?rle. 

Michael

On Jul 11, 2012, at 3:42 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:

> Hi,
> 
> On Wednesday, July 11, 2012, darnold wrote:
> 
>> All,
>> 
>> How come i=1 in the first case, but i=2 in the second case. The second case
>> seems to work, but the first case does not.
> 
> 
> 
> It dos work: whne your code gets to browser() R calls that function and
> passes it the argument given. If you expect that argument to control when
> browser() is called, you need to reread ?browser and rethink how you expect
> R to work.
> 
> In the second instance, the conditional is checked before R gets to the
> browser() command, and it is only called if the condition is met.
> 
> So both examples work as the R designers intend, that's just not how you
> thought it should work.
> 
> 
> Sarah
> 
> 
> 
>> David.
>> 
>>> findruns <- function(x,k) {
>> +   n <- length(x)
>> +   runs <- NULL
>> +   for (i in 1:(n-k+1)) {
>> +     if (all(x[i:(i+k-1)]==1)) runs <- c(runs,i)
>> +     browser(i>1)
>> +   }
>> +   return(runs)
>> + }
>>> x=c(1,1,0,0,1,1,1)
>>> findruns(x,2)
>> Called from: findruns(x, 2)
>> Browse[1]> i
>> [1] 1
>> Browse[1]> Q
>>> findruns <- function(x,k) {
>> +   n <- length(x)
>> +   runs <- NULL
>> +   for (i in 1:(n-k+1)) {
>> +     if (all(x[i:(i+k-1)]==1)) runs <- c(runs,i)
>> +     if (i>1) browser()
>> +   }
>> +   return(runs)
>> + }
>>> findruns(x,2)
>> Called from: findruns(x, 2)
>> Browse[1]> i
>> [1] 2
>> Browse[1]>
>> 
> 
> 
> -- 
> Sarah Goslee
> http://www.stringpage.com
> http://www.sarahgoslee.com
> http://www.functionaldiversity.org
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list