Skip to content

Commit

Permalink
Merge pull request #1646 from pmur002/master
Browse files Browse the repository at this point in the history
proposed change to avoid peeking at 'grid' unit internals
  • Loading branch information
cpsievert authored Oct 23, 2019
2 parents 5e1feb3 + c708f76 commit 7987280
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -1136,23 +1136,30 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
# from R, but it seems 96 is a reasonable assumption.
mm2pixels <- function(u) {
u <- verifyUnit(u)
if (attr(u, "unit") != "mm") {
stop("Unit must be in millimeters")
}
(as.numeric(u) * 96) / 25.4
u <- verifyUnit(u)
if (getRversion() >= "4.0.0") {
unitType <- get("unitType", envir=asNamespace("grid"))
if (unitType(u) != "mm") {
stop("Unit must be in millimeters")
}
} else {
if (attr(u, "unit") != "mm") {
stop("Unit must be in millimeters")
}
}
(as.numeric(u) * 96) / 25.4
}

verifyUnit <- function(u) {
# the default unit in ggplot2 is millimeters (unless it's element_text())
if (is.null(attr(u, "unit"))) {
u <- if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
## the default unit in ggplot2 is millimeters (unless it's element_text())
if (!grid::is.unit(u)) {
u <- if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
}
}
}
u
u
}

# detect a blank theme element
Expand Down

0 comments on commit 7987280

Please sign in to comment.