Learn R Programming

gWidgets (version 0.0-25)

gtree: Constructor for widget to display heirarchical dta

Description

This widget allows tree-like data to be presented. Each node on the tree should be a data frame with the same column structure. The first column is treated like a key, and should be unique. Offspring are specified through a function of the keys which are ancestors. This function returns the data frame to be displayed. Values in the tree can be selected with the mouse. This value can be retrieved through a method, or a handler can be assigned to double click events.

Usage

gtree(offspring = NULL, hasOffspring = NULL, offspring.data = NULL,
col.types = NULL, icon.FUN = NULL, chosencol = 1, multiple = FALSE,
handler = NULL, action = NULL, container = NULL, ..., toolkit = guiToolkit())

Arguments

Details

In an abstract sense, these trees are specified by a function which produces the value at a given node from the ancestry of the given node, and a function specifying if a node has offspring.

The offspring function determines the displayed data for a certain node. It has signature (path, offspring.data), where the path consists of the ancestors and offspring.data is an optional value passed in when the tree object is constructed. This function returns a data frame. Its first column should consist of unique values, as it is treated like a key.

The hasOffspring function is called on the return value of offspring. It should return a logical indicating which rows have offspring. If this argument is not present, then the second column of the return values of offspring are consulted. If these are logical, then they are used to determine if offspring are present. Otherwise, no offspring are assumed.

The icon.FUN function is called on the return value of offspring. If present, it should return a vector of stock icon names.

The svalue method returns the current key. If index is set to a column number, that column's value will be returned.

The "[" method refers to the vector of keys for the selected object. The addhandlerdoubleclick handler can be set to respond to a double click event.

Examples

Run this code
## function to find offspring
  offspring = function(path, user.data=NULL) {
    if(length(path) > 0) 
      directory = paste(getwd(),"/",paste(path,sep="/", collapse=""),sep="",collapse="")
    else
      directory = getwd()
    files = file.info(dir(path=directory))[,c(2,1,3)]
     
    files = cbind(rownames(files), files)
    names(files)[1] = "filename"
    return(files)
 }
 hasOffspring = function(children,user.data=NULL, ...) {
   return(children$isdir)
 }
 icon.FUN = function(children,user.data=NULL, ...) {
   x = rep("file",length= nrow(children))
   x[children$isdir] = "directory"
   return(x)
  }

  gtree(offspring, hasOffspring, icon.FUN = icon.FUN, container=TRUE)

Run the code above in your browser using DataLab