From simon.urbanek at r-project.org  Mon May 25 17:31:52 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Mon, 25 May 2009 17:31:52 +0200
Subject: [Rd] [R] step by step debugger in R?
In-Reply-To: <4A1AB122.2080402@dbmail.com>
References: <b1f16d9d0905211600ke8a595ct1971b6e9a560dff4@mail.gmail.com>	<4A167A71.6070601@statistik.tu-dortmund.de>	<b1f16d9d0905220759qbf699b8hc5559b789e0ae43d@mail.gmail.com>
	<4A16C235.9050908@stats.uwo.ca> <4A16E236.1060905@dbmail.com>
	<4A16FBD1.9050909@fhcrc.org> <4A17A6B7.7090404@dbmail.com>
	<4A182A77.1050503@fhcrc.org> <4A1902C9.3000705@dbmail.com>
	<5E9E03AF-797D-4CA2-9DD4-B5F8A1D01713@r-project.org>
	<4A1AB122.2080402@dbmail.com>
Message-ID: <8AEACE3D-8B20-4741-AAE2-8C7A0A21D30F@r-project.org>


On May 25, 2009, at 4:54 PM, Romain Francois wrote:

> Simon Urbanek wrote:
>>
>> [snip]
>>>
>>> I need to read more about embedding R (as in section 8 of WRE). I  
>>> know you can supply your own implementation of the REPL, but I am  
>>> not sure this includes the one that goes on once trapped into the  
>>> browser.
>>
>> Yes - it would be quite useless otherwise ;)  there are many  
>> examples of GUIs that use it (including the built-in ones [Windows,  
>> MAc, ..] or external ones e.g JGR).
>>
>> Cheers,
>> S
>>
> Hi Simon,
>
> Do you mean the rReadConsole callback ? I managed to make some minor  
> modifications to the rtest.java example that comes with JRI to  
> somewhat emulate automatically call some code (ls.str()) in this  
> example at the browser prompt, before giving the prompt to the user.
>
>   static boolean browse_first = true ;       public String  
> rReadConsole(Rengine re, String prompt, int addToHistory) {
>       System.out.print(prompt);
>               if( prompt.startsWith( "Browse[") ){
>                       if( browse_first ){
>                           System.out.println( "\n>>>> re.eval( \"  
> print( ls.str() )\" ); " ) ;
>                           re.eval( "print( ls.str() )" ) ;
>                           browse_first = false ;
>                           System.out.println( "\n>>>> return  
> \"ls.str()\"" ) ;
>                           return "ls.str()\n" ;
>                       } else{
>                           browse_first = true ;
>                       }
>                                     }
> ...
> }
>
> It seems to work and could get me somewhere, although it has a "it  
> works, but it does not feel right" taste. Basically the code  
> pretends the user typed "ls.str\n" at the browse prompt, so that the  
> R evaluator evaluates it, and then comes back to the browse prompt.
>
> There is also the re.eval( "print( ls.str() )" ) part which was my  
> first attempt, but apparently this gets evaluated in the global  
> environment, which is no good. I can get around that by returning  
> some sort of "record the sys.frames and sys.calls somewhere and do  
> something with them" function, but I was wondering if you meant  
> something else.
>

Well, it's entirely up to you - the REPL is working. I wasn't  
suggesting you have to use JRI for the debugger, I was just pointing  
out that browsing is treated as a regular prompt on the REPL, so any  
embedding has access to it.
The JRI eval() command has nothing to do with this directly - you can  
evaluate in any environment, just not specifying anything will throw  
you in the global environment - it's really up to you (it just  
abstracts out the direct access to parse and eval part of R - you can  
(ab)use it any way you see fit).

Cheers,
Simon


> Romain
>
> Here is the transcript of a simple session of ./run rtest (with the  
> small adjustement above)
>
> > f <- function( x= 5) browser()
> rBusy(1)
> rBusy(0)
> > f()
> rBusy(1)
> Called from: f()
> rBusy(0)
> Browse[1]>
> >>>> re.eval( " print( ls.str() )" );
> a :  chr "hello"
> b : 'data.frame':    3 obs. of  2 variables:
> $ a: num  1.2 2.3 4.5
> $ b: num  1.4 2.6 4.2
> bool :  logi [1:3] TRUE FALSE FALSE
> f : function (x = 5) iris : 'data.frame':    150 obs. of  5 variables:
> $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1  
> 1 1 1 1 1 1 ...
>
> >>>> return "ls.str()"
> rBusy(1)
> x :  num 5
> rBusy(0)
> Browse[1]>
> rBusy(0)
> >
>
>
>
> -- 
> Romain Francois
> Independent R Consultant
> +33(0) 6 28 91 30 30
> http://romainfrancois.blog.free.fr
>
>
>



[Rd] [R] step by step debugger in R?
Simon Urbanek