Bug 14443 - grid::unit.pmax and grid::unit.pmin return length 3 list when (all) arguments are length 1
grid::unit.pmax and grid::unit.pmin return length 3 list when (all) arguments...
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Graphics
R 2.12.0 patched
ix86 (32-bit) Windows 32-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-11-24 21:08 UTC by Brian Diggs
Modified: 2010-11-25 21:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brian Diggs 2010-11-24 21:08:28 UTC
Description:

When using the unit.pmax or unit.pmin functions from the grid package where the arguments are all of length 1, the result is of length 3 (same element repeated 3 times).  If any argument is of length greater than 1, the result is of the proper length.

Code to reproduce:

unit.pmax(unit(1, "cm"))
unit.pmin(unit(1, "cm"))

Actual output:

> unit.pmax(unit(1, "cm"))
[1] max(1cm) max(1cm) max(1cm)
> unit.pmin(unit(1, "cm"))
[1] min(1cm) min(1cm) min(1cm)

Expected output:

> unit.pmax(unit(1, "cm"))
[1] max(1cm) 
> unit.pmin(unit(1, "cm"))
[1] min(1cm) 

Discussion:

In unit.pmax (unit.pmin), maxlength, the largest of the lengths of any of the arguments, is determined.  The first element of the result is computed separately. Then a loop (i in 2:maxlength) fills in the rest of the results.  However, when maxlength is 1, the loop is (i in 2:1) which adds two more elements when none should be added.

A possible (though untested) fix would be to replace the for loop (in unit.pmax):

for (i in 2:maxlength) result <- unit.c(result, max(unit.list.from.list(lapply(x, select.i, i))))

with a while loop:

i <- 2
while (i <= maxlength) {
result <- unit.c(result, max(unit.list.from.list(lapply(x, select.i, i))))
i <- i + 1
}

This is, in essence a variation on the 1:0 bug.  As such, it could also be fixed by a variation on the usual idiom to avoid that by changing the 2:maxlength to seq(length=maxlength)[-1]
Comment 1 Brian Diggs 2010-11-24 22:08:35 UTC
Forgot my sessionInfo():

> sessionInfo()
R version 2.12.0 Patched (2010-11-04 r53526)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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


Also have the same results in the standard R 2.12.0 release:

> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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


Additionally, I failed to mention that the grid package must be loaded; add to the beginning of the reproducible example:

library("grid")
Comment 2 Brian Ripley 2010-11-25 21:14:57 UTC
Fixed in 2.12.0 patched.