From c889fa2feb081afb6261279f70d23bb6e488715a Mon Sep 17 00:00:00 2001 From: Paul Murrell Date: Wed, 23 Oct 2019 12:28:28 +1300 Subject: [PATCH 1/2] proposed change to avoid peeking at 'grid' unit internals --- R/ggplotly.R | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 6f3e638e6d..806523afe2 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -1136,23 +1136,29 @@ 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") { + if (grid::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 From c708f76e2c0f89374147443eb4825c5ed69eece9 Mon Sep 17 00:00:00 2001 From: Paul Murrell Date: Wed, 23 Oct 2019 13:19:13 +1300 Subject: [PATCH 2/2] improve previous commit --- R/ggplotly.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 806523afe2..e64c6248a4 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -1138,7 +1138,8 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height", mm2pixels <- function(u) { u <- verifyUnit(u) if (getRversion() >= "4.0.0") { - if (grid::unitType(u) != "mm") { + unitType <- get("unitType", envir=asNamespace("grid")) + if (unitType(u) != "mm") { stop("Unit must be in millimeters") } } else {