# NOT RUN {
## function to find offspring
offspring <- function(path, offspring.data=NULL) {
if(length(path) > 0)
directory <- paste(getwd(), .Platform$file.sep,
paste(path,collapse=.Platform$file.sep),
sep="")
else
directory <- getwd()
files <- file.info(dir(path=directory, full.names=TRUE))[,c(1,2,3)]
files <- data.frame(filename=dir(path=directory),
isdir=files[,2],
size=as.integer(files[,1]),
mode=as.character(files[,3]),
stringsAsFactors=FALSE)
return(files)
}
hasOffspring <- function(children,offspring.data=NULL, ...) {
return(children$isdir)
}
icon.FUN <- function(children,offspring.data=NULL, ...) {
x <- rep("file", length=nrow(children))
x[children$isdir] <- "directory"
return(x)
}
## shows isdir directory, as hasOffspring is specified
w <- gwindow("test with isdir showing")
gtree(offspring, hasOffspring, icon.FUN = icon.FUN, container=w)
## does not show isdir directory, as hasOffspring=NULL and
## second column is a logical
w <- gwindow("tree test no dir column")
tr <- gtree(offspring, hasOffspring=NULL, icon.FUN = icon.FUN, container=w)
## Show a fixed list using a dynamic tree
l <- list(a=list(
aa=1,
ab=2,
ac=list(ac1=1)
),
b=list(
ba=list(
baa=1,
bab=list(
baba=1
)
)
))
offspring <- function(path, ...) {
print(path)
ll <- l
if(length(path) > 0) {
for(i in path)
ll <- ll[[i]]
}
out <- data.frame(name=names(ll),
hasOffspring=!sapply(ll, is.atomic),
value=as.character(sapply(ll, function(i) ifelse(is.atomic(i), i, ""))),
stringsAsFactors=FALSE)
out
}
w <- gwindow("Tree from list")
tr <- gtree(offspring=offspring, container=w)
addHandlerDoubleclick(tr, handler=function(h,...) {
print(svalue(h$obj)) # the key
print(h$obj[]) # vector of keys
})
# }
Run the code above in your browser using DataLab