[R] Odp: Object Browser

jverzaniNWBKZ jverzani at gmail.com
Fri Nov 23 18:01:16 CET 2012


If gvarbrowser isn't quite to your liking, it isn't so hard to build your own
variable browser. This uses a slight modification of Petr's function and
wraps in a gWidgets interface. Modify his function to display what info you
want and add a handler to the tbl object to have some action associated with
a user selection:


ls.objects <- function (pos = 1, pattern, order.by) 
{ 
    napply <- function(names, fn) sapply(names, function(x) fn(get(x, 
        pos = pos))) 
    names <- ls(pos = pos, pattern = pattern) 
    obj.class <- napply(names, function(x) as.character(class(x))[1]) 
    obj.mode <- napply(names, mode) 
    obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class) 
    obj.size <- napply(names, object.size) 
#    obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2])) 
#    vec <- is.na(obj.dim)[, 1] & (obj.type != "function") 
#    obj.dim[vec, 1] <- napply(names, length)[vec] 
    out <- data.frame(names, obj.type, obj.size, stringsAsFactors=FALSE)#,
obj.dim) 
    names(out) <- c("Name", "Type", "Size")#, "Rows", "Columns") 
    if (!missing(order.by)) 
        out <- out[order(out[[order.by]]), ] 
    out 
} 


library(gWidgets)
options(guiToolkit="RGtk2")

w <- gwindow()
g <- ggroup(cont=w, horizontal=FALSE)
bg <- ggroup(cont=g)
glabel("Pattern:", cont=bg)
flt <- gedit("", cont=bg, expand=TRUE)
tbl <- gtable(ls.objects(), cont=g, expand=TRUE)
bg <- ggroup(cont=g)
addSpring(bg)
ref <- gbutton("refresh", cont=bg)

update_gui <- function(...) {
  pat <- svalue(flt)
  tbl[] <- ls.objects(pattern=pat)
  TRUE                                  # for task callback
}

lapply(list(flt, ref), addHandlerChanged, handler=update_gui)

This will refresh when the user updates the pattern to filter by or the
refresh button. 

One can have it refresh when the global environment changes by adding a task
callback:

addTaskCallback(update_gui)

But this can slow things up if the workspace is large. The RStudio object
browser has a very fast way to see if there are changes to the objects it is
monitoring but this is done in C++ code.





--
View this message in context: http://r.789695.n4.nabble.com/Object-Browser-tp2594912p4650575.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list