Skip to content

Commit 1b734af

Browse files
committed
To ensure correct sizing defaults in various context, ggplotly() now *always* uses a Cairo/png/jpeg device
1 parent d0516dc commit 1b734af

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

R/ggplotly.R

+23-25
Original file line numberDiff line numberDiff line change
@@ -172,32 +172,30 @@ gg2list <- function(p, width = NULL, height = NULL,
172172
layerData = 1, originalData = TRUE, source = "A", ...) {
173173

174174
# To convert relative sizes correctly, we use grid::convertHeight(),
175-
# which may open a new *screen* device, if none is currently open.
176-
# To avoid undesirable side effects, we may need to open a
177-
# non-interactive device and close it on exit...
178-
# https://github.com/att/rcloud.htmlwidgets/issues/2
179-
if (is.null(grDevices::dev.list())) {
180-
dev_fun <- if (system.file(package = "Cairo") != "") {
181-
Cairo::Cairo
182-
} else if (capabilities("png")) {
183-
grDevices::png
184-
} else if (capabilities("jpeg")) {
185-
grDevices::jpeg
186-
} else {
187-
stop(
188-
"No graphics device is currently open and no cairo or bitmap device is available.\n",
189-
"A graphics device is required to convert sizes correctly. You have three options:",
190-
" (1) Open a graphics device (with the desired size) using ggplotly()",
191-
" (2) install.packages('Cairo')",
192-
" (3) compile R to use a bitmap device (png or jpeg)",
193-
call. = FALSE
194-
)
195-
}
196-
width <- width %||% grDevices::dev.size("px")[1] %||% 640
197-
height <- height %||% grDevices::dev.size("px")[2] %||% 480
198-
dev_fun(file = tempfile(), width = width, height = height)
199-
on.exit(grDevices::dev.off(), add = TRUE)
175+
# which requires a known output (device) size.
176+
dev_fun <- if (system.file(package = "Cairo") != "") {
177+
Cairo::Cairo
178+
} else if (capabilities("png")) {
179+
grDevices::png
180+
} else if (capabilities("jpeg")) {
181+
grDevices::jpeg
182+
} else {
183+
stop(
184+
"No Cairo or bitmap device is available. Such a graphics device is required to convert sizes correctly in ggplotly().\n\n",
185+
" You have two options:\n",
186+
" (1) install.packages('Cairo')\n",
187+
" (2) compile R to use a bitmap device (png or jpeg)",
188+
call. = FALSE
189+
)
190+
}
191+
# if a device (or RStudio) is already open, use the device size as default size
192+
if (!is.null(grDevices::dev.list()) || is_rstudio()) {
193+
width <- width %||% grDevices::dev.size("px")[1]
194+
height <- height %||% grDevices::dev.size("px")[2]
200195
}
196+
# open the device and make sure it closes on exit
197+
dev_fun(file = tempfile(), width = width %||% 640, height = height %||% 480)
198+
on.exit(grDevices::dev.off(), add = TRUE)
201199

202200
# check the value of dynamicTicks
203201
dynamicValues <- c(FALSE, TRUE, "x", "y")

0 commit comments

Comments
 (0)