Skip to content

Commit 7987280

Browse files
authored
Merge pull request #1646 from pmur002/master
proposed change to avoid peeking at 'grid' unit internals
2 parents 5e1feb3 + c708f76 commit 7987280

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

R/ggplotly.R

+21-14
Original file line numberDiff line numberDiff line change
@@ -1136,23 +1136,30 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
11361136
# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
11371137
# from R, but it seems 96 is a reasonable assumption.
11381138
mm2pixels <- function(u) {
1139-
u <- verifyUnit(u)
1140-
if (attr(u, "unit") != "mm") {
1141-
stop("Unit must be in millimeters")
1142-
}
1143-
(as.numeric(u) * 96) / 25.4
1139+
u <- verifyUnit(u)
1140+
if (getRversion() >= "4.0.0") {
1141+
unitType <- get("unitType", envir=asNamespace("grid"))
1142+
if (unitType(u) != "mm") {
1143+
stop("Unit must be in millimeters")
1144+
}
1145+
} else {
1146+
if (attr(u, "unit") != "mm") {
1147+
stop("Unit must be in millimeters")
1148+
}
1149+
}
1150+
(as.numeric(u) * 96) / 25.4
11441151
}
1145-
1152+
11461153
verifyUnit <- function(u) {
1147-
# the default unit in ggplot2 is millimeters (unless it's element_text())
1148-
if (is.null(attr(u, "unit"))) {
1149-
u <- if (inherits(u, "element")) {
1150-
grid::unit(u$size %||% 0, "points")
1151-
} else {
1152-
grid::unit(u %||% 0, "mm")
1154+
## the default unit in ggplot2 is millimeters (unless it's element_text())
1155+
if (!grid::is.unit(u)) {
1156+
u <- if (inherits(u, "element")) {
1157+
grid::unit(u$size %||% 0, "points")
1158+
} else {
1159+
grid::unit(u %||% 0, "mm")
1160+
}
11531161
}
1154-
}
1155-
u
1162+
u
11561163
}
11571164

11581165
# detect a blank theme element

0 commit comments

Comments
 (0)