Bug 16791 - grid::grid.show.layout reports units with excessive accuracy
Summary: grid::grid.show.layout reports units with excessive accuracy
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R-devel (trunk)
Hardware: All All
: P5 minor
Assignee: R-core
Depends on:
Reported: 2016-03-30 07:57 UTC by Richard Cotton
Modified: 2016-10-04 01:55 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Richard Cotton 2016-03-30 07:57:26 UTC
When grid layouts contain units with many decimal places they aren't easy to read.  This example is adapted from the one in example(grid.show.layout).

h <- unit(rep(14/13, 4), c("lines", "lines", "lines", "null"))
w <- unit(c(6/7, 10/11), "inches")
grid.show.layout(grid.layout(4,2, heights=h, widths=w))

Displaying many decimal places is unlikely to be beneficial in most cases, so limiting to 3 or 4 signficant figures seems sensible.

Here is my suggested fix.

Firstly, define a format method for units.

format.unit <- function(x, ...)
  class(x) <- NULL
  paste0(format(x, ...), attr(x, "unit"))  

Secondly, in grid.show.layout, replace calls to as.character.unit with format.unit.  For example, the line:

grid.text(as.character(l$heights[i, top = FALSE])


grid.text(format(l$heights[i, top = FALSE], digits = 4)

I don't know if you want to have the number of digits fixed or let it be passed as an argument to grid.show.layout.
Comment 1 Paul Murrell 2016-04-07 23:37:44 UTC
A format.unit() sounds reasonable, though it will have to be a bit more sophisticated than the suggestion.  For example ...

> format(unit(1, "npc") - unit(1, "mm"))
[1] "-" "1" "1"

Will put this on my TODO list
Comment 2 Paul Murrell 2016-10-04 01:55:26 UTC
A format() method for units has been added to r-devel (r71444) and grid.show.layout() uses format() instead of as.character()