@@ -1136,30 +1136,32 @@ 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.
11381138mm2pixels <- function (u ) {
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
1139+ u <- verifyUnit(u )
1140+ if (getUnitType(u ) != " mm" ) {
1141+ stop(" Unit must be in millimeters" )
1142+ }
1143+ (as.numeric(u ) * 96 ) / 25.4
11511144}
11521145
11531146verifyUnit <- function (u ) {
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- }
1161- }
1162- u
1147+ if (grid :: is.unit(u )) return (u )
1148+
1149+ # # the default unit in ggplot2 is millimeters (unless it's element_text())
1150+ if (inherits(u , " element" )) {
1151+ grid :: unit(u $ size %|| % 0 , " points" )
1152+ } else {
1153+ grid :: unit(u %|| % 0 , " mm" )
1154+ }
1155+ }
1156+
1157+ # Use public API for getting the unit's type, if available
1158+ # https://github.com/ropensci/plotly/pull/1646#issue-331268260
1159+ getUnitType <- function (u ) {
1160+ if (getRversion() > = " 4.0.0" ) {
1161+ get(" unitType" , envir = asNamespace(" grid" ))(u )
1162+ } else {
1163+ attr(u , " unit" )
1164+ }
11631165}
11641166
11651167# detect a blank theme element
0 commit comments