From romain.francois at dbmail.com  Thu Apr 23 15:49:15 2009
From: romain.francois at dbmail.com (Romain Francois)
Date: Thu, 23 Apr 2009 15:49:15 +0200
Subject: [Rd] Custom browser prompt instead of Browse[1]>
Message-ID: <49F071DB.2090406@dbmail.com>

Hello,

Would it be possible to have a custom prompt when browser()'ing.

I have made a simple implementation of this (which is attached), the 
basic idea is that instead of the hardcoded sprintf( "Browser[%d]> ", 
browselevel), a call to the getBrowsePrompt is made, and obviously the 
function is:

 > getBrowsePrompt
function (level = 1, env = .GlobalEnv) {
    sprintf("Browse[%d]> ", level)
}
<environment: namespace:base>

 > debug( rnorm )
 > rnorm( 10 )
debugging in: rnorm(10)
debug: .Internal(rnorm(n, mean, sd))
Browse[1]>
exiting from: rnorm(10)
 [1] -0.496598526 -0.006482431  1.491833990 -2.602605734 -0.275479145
 [6] -1.143580117  0.146797854 -0.529420397  0.823817647 -0.256676050

but then it can be masked so that a more informative prompt is given, 
like this for example (showing the call stack)

 > getBrowsePrompt
function( level = 1, env = .GlobalEnv){
 calls <- sys.calls()
 stack <- sapply( calls[-1], function(x) tryCatch(as.character(x[[1]]), 
error=function(e) "?") )
 sprintf( "Browse[%d] %s > ", level, paste( stack, collapse = " -> ") )
}
 > f()
debugging in: rnorm(10)
debug: .Internal(rnorm(n, mean, sd))
Browse[1] rnorm -> getBrowsePrompt >
exiting from: rnorm(10)
 [1] -0.53854862 -1.42674850 -0.48391168 -0.23446819 -0.36863380  0.53803626
 [7]  1.70176078  0.82984068  1.05101379 -0.03944557

Or this: printing the content of the environment we are browsing :

 > getBrowsePrompt <- function( level = 1, env = .GlobalEnv ){
+   print( ls.str( envir = env) )
+   base:::getBrowsePrompt( level, env )
+ }
 > f()
debugging in: rnorm(10)
debug: .Internal(rnorm(n, mean, sd))
mean :  num 0
n :  num 10
sd :  num 1
Browse[1]>
exiting from: rnorm(10)
 [1]  1.1112007  0.4921306  0.1747196 -0.2518565 -0.9342039  0.5930085
 [7] -0.5961234  0.1153541 -0.6189056  0.1670318

This probably should rely on an option instead of relying on masking 
functions. Also this opens a back door to the debugging system of R, but 
I am not sure this is entirely a bad thing. (see 
http://www.statistik.lmu.de/~eugster/soc09/#p5)

On the same note, what about having a function for the prompt, so that 
(for example) we could show the current working directory, the memory 
usage, ...


Romain

-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr


-------------- next part --------------
A non-text attachment was scrubbed...
Name: browser.diff
Type: text/x-patch
Size: 2030 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20090423/06a03820/attachment.bin>


[Rd] Custom browser prompt instead of Browse[1]>
Romain Francois