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

Romain Francois romain.francois at dbmail.com
Mon May 25 16:54:26 CEST 2009


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.

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



More information about the R-devel mailing list