@@ -1136,23 +1136,29 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
1136
1136
# we need to know PPI/DPI of the display. I'm not sure of a decent way to do that
1137
1137
# from R, but it seems 96 is a reasonable assumption.
1138
1138
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
+ if (grid :: unitType(u ) != " mm" ) {
1142
+ stop(" Unit must be in millimeters" )
1143
+ }
1144
+ } else {
1145
+ if (attr(u , " unit" ) != " mm" ) {
1146
+ stop(" Unit must be in millimeters" )
1147
+ }
1148
+ }
1149
+ (as.numeric(u ) * 96 ) / 25.4
1144
1150
}
1145
-
1151
+
1146
1152
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" )
1153
+ # # the default unit in ggplot2 is millimeters (unless it's element_text())
1154
+ if (! grid :: is.unit(u )) {
1155
+ u <- if (inherits(u , " element" )) {
1156
+ grid :: unit(u $ size %|| % 0 , " points" )
1157
+ } else {
1158
+ grid :: unit(u %|| % 0 , " mm" )
1159
+ }
1153
1160
}
1154
- }
1155
- u
1161
+ u
1156
1162
}
1157
1163
1158
1164
# detect a blank theme element
0 commit comments