Skip to content

Commit 3bf9f04

Browse files
committed
open a bitmap device instead of push/popping viewports
1 parent 85843e0 commit 3bf9f04

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

R/ggplotly.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,12 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
115115
# https://github.com/hadley/ggplot2/blob/0cd0ba/R/plot-build.r#L18-L92
116116
# ------------------------------------------------------------------------
117117

118-
# create a viewport so we can convert relative sizes correctly
119-
# if height/width not specified, it is estimated from current viewport (npc)
120-
widthVP <- width %||% unitConvert(grid::unit(1, "npc"), "pixels", "width")
121-
heightVP <- height %||% unitConvert(grid::unit(1, "npc"), "pixels", "height")
122-
# assume 96 dots per inch...mm2pixels also does this....
123-
vp <- grid::viewport(
124-
width = grid::unit(widthVP * 72.27 / 96, "points"),
125-
height = grid::unit(heightVP * 72.27 / 96, "points")
126-
)
127-
grid::pushViewport(vp, recording = FALSE)
118+
# open a new graphics device, so we can convert relative sizes correctly
119+
# if height/width is not specified, estimate it from the current device
120+
deviceWidth <- width %||% unitConvert(grid::unit(1, "npc"), "pixels", "width")
121+
deviceHeight <- height %||% unitConvert(grid::unit(1, "npc"), "pixels", "height")
122+
tmpPlotFile <- tempfile(fileext = ".png")
123+
grDevices::png(tmpPlotFile, width = deviceWidth, height = deviceHeight)
128124

129125

130126
p <- ggfun("plot_clone")(p)
@@ -682,7 +678,10 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
682678
gglayout$width <- width
683679
gglayout$height <- height
684680

685-
grid::popViewport()
681+
# we're now done with converting units, turn off the device,
682+
# and remove the temporary file
683+
grDevices::dev.off()
684+
unlink(tmpPlotFile)
686685

687686
l <- list(
688687
data = setNames(traces, NULL),

0 commit comments

Comments
 (0)