From baptiste.auguie at googlemail.com  Sun Sep 27 12:49:36 2009
From: baptiste.auguie at googlemail.com (baptiste auguie)
Date: Sun, 27 Sep 2009 12:49:36 +0200
Subject: [Rd] puzzle with drawDetails for a class derived from a gTree
Message-ID: <de4e29f50909270349v2422eadcne40d228ea98da25e@mail.gmail.com>

Dear all,

I've tried all sorts of variations discussed in "R graphics" by Paul
Murrell, but I still can't understand how to write a drawDetails
method for a class derived from a gTree.
Below is a minimal, dummy example where two strings are plotted in two
separate viewports. I require the creation of the strings to be
evaluated inside the drawDetails function because my real example will
need to calculate a grob size on-the-fly (the viewport size as well,
for which I have no clue where to place the code).

The tests below present two issues:

- editing the gp component of the gTree only affects one child for some reason

- the resulting gTree doesn't appear to contain children, they seem
"hidden" from the user in the drawDetails method


Any advice will be much appreciated!

Best regards,

Baptiste

## two function that create the text grobs
make.test.right <- function(lab, col) {
 textGrob(lab, gp=gpar(col=col), name="right", vp="rightvp")
}

make.test.left <- function(lab, col) {
  textGrob(lab, gp=gpar(col=col), name="left", vp="leftvp")
}

grid.test <- function(right="right text", left="left text", col = "red",
                      edits=NULL, draw=TRUE,
                      name=NULL, gp=gpar()) {

## layout for the two children
  vp <- viewport(layout=grid.layout(1, 2))

 ## viewports, one for each child
  cvp <- vpList(viewport(layout.pos.row=1, layout.pos.col=1, name="leftvp"),
                viewport(layout.pos.row=1, layout.pos.col=2, name="rightvp"))


  x <- gTree(right=right, left=left, col=col,
             childrenvp=cvp,
             edits=edits,
             name=name, gp=gp, vp=vp,
             cl="test")
  if(draw)
    grid.draw(x)
  invisible(x)
}


drawDetails.test <- function(x, recording=TRUE) {

  x <- applyEdits(x,  x$edits)

## create the two grobs
  left <- make.test.left(x$left, x$col)
  right <- make.test.right(x$right, x$col)

## add them to the gTree
  x <- addGrob(x, right)
  x <- addGrob(x, left)

## borrowed from grid.xaxis
## draw the children only

  for(child in childNames(x))
    grid.draw(getGrob(x, child))

}

grid.test("a", "b", edits=gEdit(left="left text", col="blue"),  name="test")
# works as expected

grid.newpage()

grid.test(left="left text", right="right text",   name="test")

grid.edit("test",left="test") # OK
grid.edit("test", col="blue") # OK

grid.edit("test", right="big too?", gp=gpar(cex=2))
# here only the left grob is altered???

grid.edit("test::right",  gp=gpar(cex=2))  ## fails
## as "test" doesn't have children,  apparently

# similarly
grid.gedit("right", right="testing")
# 'gPath' (right) not found


sessionInfo()
R version 2.9.2 (2009-08-24)
i386-apple-darwin8.11.1

locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  grid      methods
[8] base



[Rd] puzzle with drawDetails for a class derived from a gTree
baptiste auguie