I have written a function to build a plot using grid grobs, and a method to get the width of the resulting gTree. The method function is recognised when run from a script but not when I try to load as part of a package. In my package code file I have:
require(grid)
require(reshape)
widthDetails.bHMLegend <- function(x){
minX0 <- min(do.call(unit.c, lapply(x$children, grobX, theta = 180)))
maxX1 <- max(do.call(unit.c, lapply(x$children, grobX, theta = 0)))
return(grobWidth(segmentsGrob(x0 = minX0, x1 = maxX1)))}
bHMLegend <- function(sizeBreaks, title){
gTree(name="BHMLegend", cl="bHMLegend", children = gList(
rectGrob(x = 0.5, y = c(0.5:(length(sizeBreaks)-0.5))*unit(0.5, "cm"),
height = unit(0.5, "cm"), width = unit(0.5, "cm"), just=c("left", "centre")),
textGrob(sizeBreaks, x = unit(0.5, "npc") + unit(0.5, "cm") + unit(1, "char"),
y = seq(0.5, length(sizeBreaks)-0.5, 1)*unit(0.5, "cm"), just=c("left", "centre")),
textGrob(title, x = unit(0.5, "npc") + unit(0.25, "cm"),
y = unit(0.5, "cm")*(length(sizeBreaks)+0.5), just=c("centre", "bottom"))))
}
In my testing script file I have:
require(devtools)
load_all()
tree <- bHMLegend(c(0, 10, 20, 30, 40, 50, 60, 70, 80), title="Legend Title")
convertUnit(grobWidth(tree), "cm")
widthDetails.bHMLegend <- function(x){
minX0 <- min(do.call(unit.c, lapply(x$children, grobX, theta = 180)))
maxX1 <- max(do.call(unit.c, lapply(x$children, grobX, theta = 0)))
return(grobWidth(segmentsGrob(x0 = minX0, x1 = maxX1)))}
convertUnit(grobWidth(tree), "cm")
The first convertUnit() call returns 0cm, but the second time after loading the function directly into the global environment I get the expected answer (~2.42cm). How do I get this to work from the package?
Thanks (edited to include full example code)