Skip to content

Commit 432c108

Browse files
authored
Close #2175. Improve installation docs and warnings/errors surrounding save_image() (#2360)
1 parent 665504a commit 432c108

File tree

7 files changed

+51
-25
lines changed

7 files changed

+51
-25
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Imports:
4242
vctrs,
4343
tibble,
4444
lazyeval (>= 0.2.0),
45-
rlang (>= 0.4.10),
45+
rlang (>= 1.0.0),
4646
crosstalk,
4747
purrr,
4848
data.table,

R/export.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Export a plotly graph to a static file
22
#'
3-
#' This function is in the process of being deprecated (use [orca] instead).
3+
#' This function is deprecated, use [save_image] instead.
44
#'
55
#' @details For SVG plots, a screenshot is taken via `webshot::webshot()`.
66
#' Since `phantomjs` (and hence `webshot`) does not support WebGL,
@@ -19,7 +19,7 @@
1919
#' @author Carson Sievert
2020
#'
2121
export <- function(p = last_plot(), file = "plotly.png", selenium = NULL, ...) {
22-
.Deprecated("orca")
22+
.Deprecated("save_image")
2323

2424
# infer the file type
2525
fileType <- tolower(tools::file_ext(file))

R/kaleido.R

+37-12
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#' @section Installation:
99
#'
1010
#' `kaleido()` requires [the kaleido python
11-
#' package](https://github.com/plotly/Kaleido/) to be usable via the \pkg{reticulate} package. Here is a recommended way to do the installation:
11+
#' package](https://github.com/plotly/Kaleido/) to be usable via the
12+
#' \pkg{reticulate} package. If you're starting from scratch, you install
13+
#' eveything you need with the following R code:
1214
#'
1315
#' ```
14-
#' install.packages('reticulate')
15-
#' reticulate::install_miniconda()
16-
#' reticulate::conda_install('r-reticulate', 'python-kaleido')
17-
#' reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')
18-
#' reticulate::use_miniconda('r-reticulate')
16+
#' install.packages("reticulate")
17+
#' library(reticulate)
18+
#' use_python(install_python())
19+
#' py_install(c("kaleido", "plotly"))
1920
#' ```
2021
#'
2122
#' @param ... not currently used.
@@ -65,16 +66,40 @@ save_image <- function(p, file, ..., width = NULL, height = NULL, scale = NULL)
6566
#' @rdname save_image
6667
#' @export
6768
kaleido <- function(...) {
68-
if (!rlang::is_installed("reticulate")) {
69-
stop("`kaleido()` requires the reticulate package.")
70-
}
71-
if (!reticulate::py_available(initialize = TRUE)) {
72-
stop("`kaleido()` requires `reticulate::py_available()` to be `TRUE`. Do you need to install python?")
69+
rlang::check_installed("reticulate")
70+
71+
call_env <- rlang::caller_env()
72+
73+
if (!reticulate::py_available()) {
74+
rlang::abort(c("`{reticulate}` wasn't able to find a Python environment.",
75+
i = "If you have an existing Python installation, use `reticulate::use_python()` to inform `{reticulate}` of it.",
76+
i = "To have `{reticulate}` install Python for you, `reticulate::install_python()`."
77+
), call = call_env)
7378
}
7479

80+
tryCatch(
81+
reticulate::import("plotly"),
82+
error = function(e) {
83+
rlang::abort(c(
84+
"The `plotly` Python package is required for static image exporting.",
85+
i = "Please install it via `reticulate::py_install('plotly')`."
86+
), call = call_env)
87+
}
88+
)
89+
90+
kaleido <- tryCatch(
91+
reticulate::import("kaleido"),
92+
error = function(e) {
93+
rlang::abort(c(
94+
"The `kaleido` Python package is required for static image exporting.",
95+
i = "Please install it via `reticulate::py_install('kaleido')`."
96+
), call = call_env)
97+
}
98+
)
99+
75100
py <- reticulate::py
76101
scope_name <- paste0("scope_", new_id())
77-
py[[scope_name]] <- reticulate::import("kaleido")$scopes$plotly$PlotlyScope(
102+
py[[scope_name]] <- kaleido$scopes$plotly$PlotlyScope(
78103
plotlyjs = plotlyMainBundlePath()
79104
)
80105

R/orca.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Static image exporting via orca
22
#'
3-
#' Superseded by [kaleido()].
3+
#' This function is deprecated, use [save_image()] instead.
44
#'
55
#' @param p a plotly object.
66
#' @param file output filename.
@@ -60,7 +60,7 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
6060
parallel_limit = NULL, verbose = FALSE, debug = FALSE,
6161
safe = FALSE, more_args = NULL, ...) {
6262

63-
.Deprecated("kaleido")
63+
.Deprecated("save_image")
6464

6565
orca_available()
6666

man/export.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/orca.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/save_image.Rd

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)