This is not optimal, but if all else fails, you can simply rewrite unit.pmax so that it does what you want.
The following function acts just like unit.pmax() , except that whenever it requests the maximum maximum of two or more unit objects, all in units of "null" , it returns its value of "largest", not the expression forms max(x,y,...) . (See second code example below).
unit.pmax2 <- function (...) { select.i <- function(unit, i) { unit[i, top = FALSE] } x <- list(...) numargs <- length(x) if (numargs == 0L) stop("no arguments where at least one expected") maxlength <- 0L for (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) for (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 the difference between unit.pmax() and 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 showed that it works. (Note that you also need to replace w2[[1]] <- ... with w2[1] <- ... to avoid a complaint if 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")) ## For comparison gl3 <- grid.layout(1, 2, widths = w2, heights = h, respect = TRUE) grid.newpage() grid.show.layout(gl3)
