r - grid.layout doesn't like respect and compound units -


using unit.pmax default comparison of widths/heights in gtable proving harder i'd hoped; after several hours of head scratching i've narrowed down situation:

library(grid)  w <- list(unit(1,"null"), unit(1,"null")) class(w) <-  c("unit.list", "unit") h <- unit(1, "in") gl1 <- grid.layout(1, 2, widths = w, heights = h,                    respect = true) grid.newpage() grid.show.layout(gl1) # fine  w2 <- w w2[[1]] <- unit.pmax(unit(1,"null"), unit(1,"null")) gl2 <- grid.layout(1, 2, widths = w2, heights = h,                    respect = false) grid.newpage() grid.show.layout(gl2)# fine  gl3 <- grid.layout(1, 2, widths = w2, heights = h,                    respect = true) grid.newpage() grid.show.layout(gl3)  ## error in grid.call.graphics(l_setviewport, vp, true) :  ##  non-finite location and/or size viewport 

so combination of unit.pmax(unit(1,"null"), unit(1,"null")) , respect = true makes grid complain. in case you're wondering, situation come in ggplot2 facet_grid , theme(aspect.ratio = ...).

i can vaguely picture unit.pmax() should simplify null units before attempting use respect parameter, don't know means. in practice though, prevents me improving gtable's cbind/rbind.

any workaround?

edit: i'm not sure how provide minimal example ggplot2, other installing my fork , running

ggplot(data.frame(x=1:8, y=1:8, f=gl(2,4)), aes(x, y)) +    geom_point() +     facet_grid(f~.) +    theme(aspect.ratio=3) # error in grid.call.graphics(l_setviewport, vp, true) :  #  non-finite location and/or size viewport 

so unit.pmax() fails in case, while current comparison method compare.unit(,,pmax) fails in other situations, such as,

p1 = qplot(1, 1); p2 = qplot(1,1) cbind(ggplotgrob(p1), ggplotgrob(p2), size="max") # error in mmm < each : comparison of these types not implemented 

it's not optimal, if else fails, rewrite unit.pmax make wish did.

the following function acts unit.pmax() except that, whenever it's asked find maximimum of 2 or more unit objects, in "null" units, returns value of "largest" one, rather expression of form max(x,y,...). (see second code block below example.)

unit.pmax2 <-  function (...)  {     select.i <- function(unit, i) {         unit[i, top = false]     }     x <- list(...)     numargs <- length(x)     if (numargs == 0l)          stop("no arguments @ least 1 expected")     maxlength <- 0l     (i in seq_len(numargs)) if (length(x[[i]]) > maxlength)          maxlength <- length(x[[i]])             ## result <- max(unit.list.from.list(lapply(x, select.i, 1l)))     ul <- grid:::unit.list.from.list(lapply(x, select.i, 1l))                 ##     result <- if(all(sapply(ul, attr, "unit")=="null")) {                     ##                   ul[which.max(ul)]} else {max(ul)}                           ##     if (maxlength > 1l)          (i in 2l:maxlength) {             ## result <- unit.c(result, max(unit.list.from.list(lapply(x,              ##             select.i, i))))             ul <- grid:::unit.list.from.list(lapply(x, select.i, i))          ##             temp <- if(all(sapply(ul, attr, "unit")=="null")) {               ##                         ul[which.max(ul)]} else {max(ul)}                     ##             result <- unit.c(result, temp)                                    ##         }     result } 

to see difference between unit.pmax() , unit.pmax2(), compare:

a <- list(unit(1,"null"), unit(1,"null"), unit(1,"null")) b <- list(unit(1,"null"), unit(4,"null"), unit(1,"null")) c <- list(unit(1,"null"), unit(2,"null"), unit(1,"inch"))  class(a) <- class(b) <- class(c) <- c("unit.list", "unit")  unit.pmax(a, b, c) # [1] max(1null, 1null, 1null) max(1null, 4null, 2null) max(1null, 1null, 1inch)     unit.pmax2(a, b, c) # [1] 1null                    4null                    max(1null, 1null, 1inch) 

testing out shows works. (note need replace w2[[1]] <- ... w2[1] <- ... avoid complaint when respect = true.)

library(grid)  w2 <- list(unit(1,"null"), unit(1,"null")) class(w2) <-  c("unit.list", "unit") h <- unit(1, "in")  w2[1] <- unit.pmax2(unit(1,"null"), unit(1,"null")) ## w2[[1]] <- unit.pmax(unit(1,"null"), unit(1,"null"))  ## comparison gl3 <- grid.layout(1, 2, widths = w2, heights = h,                    respect = true) grid.newpage() grid.show.layout(gl3) 

enter image description here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -