Skip to content

Commit 3089a66

Browse files
Revert "Check for plotly's orca not just orca"
This reverts commit aafd524.
1 parent aafd524 commit 3089a66

File tree

1 file changed

+52
-56
lines changed

1 file changed

+52
-56
lines changed

R/orca.R

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,89 @@
1-
#' Static image exporting
2-
#'
1+
#' Static image exporting
2+
#'
33
#' Export plotly objects to static images (e.g., pdf, png, jpeg, svg, etc) via the
44
#' [orca command-line utility](https://github.com/plotly/orca#installation).
5-
#'
5+
#'
66
#' The `orca()` function is designed for exporting one plotly graph whereas `orca_serve()`
77
#' is meant for exporting many graphs at once. The former starts and stops an external (nodejs)
88
#' process everytime it is called whereas the latter starts up a process when called, then
9-
#' returns an `export()` method for exporting graphs as well as a `close()` method for stopping
9+
#' returns an `export()` method for exporting graphs as well as a `close()` method for stopping
1010
#' the external (background) process.
11-
#'
11+
#'
1212
#' @param p a plotly object.
1313
#' @param file output filename.
1414
#' @param format the output format (png, jpeg, webp, svg, pdf, eps).
1515
#' @param scale Sets the image scale. Applies to all output images.
16-
#' @param width Sets the image width. If not set, defaults to `layout.width` value.
16+
#' @param width Sets the image width. If not set, defaults to `layout.width` value.
1717
#' Applies to all output images.
18-
#' @param height Sets the image height. If not set, defaults to `layout.height` value.
18+
#' @param height Sets the image height. If not set, defaults to `layout.height` value.
1919
#' Applies to all output images.
2020
#' @param mathjax whether or not to include MathJax (required to render [TeX]).
21-
#' If `TRUE`, the PLOTLY_MATHJAX_PATH environment variable must be set and point
22-
#' to the location of MathJax (this variable is also used to render [TeX] in
21+
#' If `TRUE`, the PLOTLY_MATHJAX_PATH environment variable must be set and point
22+
#' to the location of MathJax (this variable is also used to render [TeX] in
2323
#' interactive graphs, see [config]).
2424
#' @param parallel_limit Sets the limit of parallel tasks run.
2525
#' @param verbose Turn on verbose logging on stdout.
2626
#' @param debug Starts app in debug mode and turn on verbose logs on stdout.
27-
#' @param safe Turns on safe mode: where figures likely to make browser window
27+
#' @param safe Turns on safe mode: where figures likely to make browser window
2828
#' hang during image generating are skipped.
2929
#' @param more_args additional arguments to pass along to system command. This is useful
3030
#' for specifying display and/or electron options, such as `--enable-webgl` or `--disable-gpu`.
31-
#' @param ... for `orca()`, additional arguments passed along to `processx::run`. For
31+
#' @param ... for `orca()`, additional arguments passed along to `processx::run`. For
3232
#' `orca_serve()`, additional arguments passed along to `processx::process`.
3333
#' @export
3434
#' @author Carson Sievert
3535
#' @md
3636
#' @rdname orca
3737
#' @examples
38-
#'
38+
#'
3939
#' \dontrun{
4040
#' # NOTE: in a headless environment, you may need to set `more_args="--enable-webgl"`
4141
#' # to export webgl correctly
4242
#' p <- plot_ly(z = ~volcano) %>% add_surface()
4343
#' orca(p, "surface-plot.svg")
44-
#'
44+
#'
4545
#' #' # launch the server
4646
#' server <- orca_serve()
47-
#'
47+
#'
4848
#' # export as many graphs as you'd like
4949
#' server$export(qplot(1:10), "test1.pdf")
5050
#' server$export(plot_ly(x = 1:10, y = 1:10), "test2.pdf")
51-
#'
51+
#'
5252
#' # the underlying process is exposed as a field, so you
5353
#' # have full control over the external process
5454
#' server$process$is_alive()
55-
#'
55+
#'
5656
#' # convenience method for closing down the server
5757
#' server$close()
58-
#'
58+
#'
5959
#' # remove the exported files from disk
6060
#' unlink("test1.pdf")
6161
#' unlink("test2.pdf")
6262
#' }
63-
#'
63+
#'
6464

65-
orca <- function(p, file = "plot.png", format = tools::file_ext(file),
65+
orca <- function(p, file = "plot.png", format = tools::file_ext(file),
6666
scale = NULL, width = NULL, height = NULL, mathjax = FALSE,
67-
parallel_limit = NULL, verbose = FALSE, debug = FALSE,
67+
parallel_limit = NULL, verbose = FALSE, debug = FALSE,
6868
safe = FALSE, more_args = NULL, ...) {
69-
69+
7070
orca_available()
71-
71+
7272
b <- plotly_build(p)
73-
73+
7474
# find the relevant plotly.js bundle
7575
plotlyjs <- plotlyjsBundle(b)
7676
plotlyjs_path <- file.path(plotlyjs$src$file, plotlyjs$script)
7777
# package field means src file path should be relative to pkg dir
7878
if (!is.null(plotlyjs$package)) {
7979
plotlyjs_path <- system.file(plotlyjs_path, package = plotlyjs$package)
8080
}
81-
81+
8282
tmp <- tempfile(fileext = ".json")
8383
cat(to_JSON(b$x[c("data", "layout")]), file = tmp)
84-
84+
8585
args <- c(
86-
"graph", tmp,
86+
"graph", tmp,
8787
"-o", file,
8888
"--format", format,
8989
"--plotlyjs", plotlyjs_path,
@@ -92,67 +92,67 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
9292
if (safe) "--safe-mode",
9393
more_args
9494
)
95-
95+
9696
if (!is.null(scale)) args <- c(args, "--scale", scale)
9797
if (!is.null(width)) args <- c(args, "--width", width)
9898
if (!is.null(height)) args <- c(args, "--height", height)
9999
if (!is.null(parallel_limit)) args <- c(args, "--parallel-limit", parallel_limit)
100100
if (!is.null(tryNULL(mapbox_token()))) args <- c(args, "--mapbox-access-token", mapbox_token())
101101
if (isTRUE(mathjax)) args <- c(args, "--mathjax", file.path(mathjax_path(), "MathJax.js"))
102-
102+
103103
# TODO: point to local topojson? Should this only work if plot_geo(standalone = TRUE)?
104104
try_library("processx", "orca")
105105
invisible(processx::run("orca", args, echo = TRUE, spinner = TRUE, ...))
106106
}
107107

108108
#' Orca image export server
109-
#'
109+
#'
110110
#' @inheritParams orca
111111
#' @param port Sets the server's port number.
112112
#' @param keep_alive Turn on keep alive mode where orca will (try to) relaunch server if process unexpectedly exits.
113113
#' @param window_max_number Sets maximum number of browser windows the server can keep open at a given time.
114114
#' @param request_limit Sets a request limit that makes orca exit when reached.
115115
#' @param quiet Suppress all logging info.
116-
#'
116+
#'
117117
#' @section Methods:
118-
#'
118+
#'
119119
#' The `orca_serve()` function returns an object with two methods:
120-
#'
120+
#'
121121
#' \describe{
122122
#' \item{\code{export(p, file = "plot.png", format = tools::file_ext(file), scale = NULL, width = NULL, height = NULL)}}{
123123
#' Export a static image of a plotly graph. Arguments found here are the same as those found in `orca()`
124124
#' }
125125
#' \item{\code{close()}}{Close down the orca server and kill the underlying node process.}
126126
#' }
127-
#'
127+
#'
128128
#' @section Fields:
129-
#'
129+
#'
130130
#' The `orca_serve()` function returns an object with two fields:
131-
#'
131+
#'
132132
#' \describe{
133133
#' \item{\code{port}}{The port number that the server is listening to.}
134134
#' \item{\code{process}}{An R6 class for controlling and querying the underlying node process.}
135135
#' }
136-
#'
136+
#'
137137
#' @export
138138
#' @rdname orca
139139

140140
orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit = NULL,
141-
keep_alive = TRUE, window_max_number = NULL, quiet = FALSE,
141+
keep_alive = TRUE, window_max_number = NULL, quiet = FALSE,
142142
debug = FALSE, more_args = NULL, ...) {
143-
143+
144144
# make sure we have the required infrastructure
145145
orca_available()
146146
try_library("processx", "orca_serve")
147-
147+
148148
# use main bundle since any plot can be thrown at the server
149149
plotlyjs <- plotlyMainBundle()
150150
plotlyjs_path <- file.path(plotlyjs$src$file, plotlyjs$script)
151151
# package field means src file path should be relative to pkg dir
152152
if (!is.null(plotlyjs$package)) {
153153
plotlyjs_path <- system.file(plotlyjs_path, package = plotlyjs$package)
154154
}
155-
155+
156156
args <- c(
157157
"serve",
158158
"-p", port,
@@ -164,21 +164,21 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
164164
if (quiet) "--quiet",
165165
more_args
166166
)
167-
167+
168168
if (!is.null(request_limit))
169169
args <- c(args, "--request-limit", request_limit)
170-
170+
171171
if (!is.null(window_max_number))
172172
args <- c(args, "--window-max-number", window_max_number)
173-
174-
if (!is.null(tryNULL(mapbox_token())))
173+
174+
if (!is.null(tryNULL(mapbox_token())))
175175
args <- c(args, "--mapbox-access-token", mapbox_token())
176-
177-
if (isTRUE(mathjax))
176+
177+
if (isTRUE(mathjax))
178178
args <- c(args, "--mathjax", file.path(mathjax_path(), "MathJax.js"))
179-
179+
180180
process <- processx::process$new("orca", args, ...)
181-
181+
182182
list(
183183
port = port,
184184
process = process,
@@ -193,7 +193,7 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
193193
scale = scale
194194
)
195195
res <- httr::POST(
196-
paste0("http://127.0.0.1:", port),
196+
paste0("http://127.0.0.1:", port),
197197
body = to_JSON(bod)
198198
)
199199
httr::stop_for_status(res)
@@ -204,28 +204,24 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
204204
)
205205
}
206206

207-
correct_orca <- function() {
208-
orca_help <- processx::run("orca", "-h")
209-
grepl("plotly", orca_help[["stdout"]], ignore.case = TRUE)
210-
}
211207

212208
orca_available <- function() {
213-
if (Sys.which("orca") == "" || !correct_orca()) {
209+
if (Sys.which("orca") == "") {
214210
stop(
215211
"The orca command-line utility is required for this functionality.\n\n",
216212
"Please follow the installation instructions here -- https://github.com/plotly/orca#installation",
217213
call. = FALSE
218214
)
219215
}
220-
216+
221217
TRUE
222218
}
223219

224220
orca_version <- function() {
225221
orca_available()
226222
# default to initial release if we can't correctly parse version
227223
tryCatch(
228-
as.package_version(system("orca --version", intern = TRUE)),
224+
as.package_version(system("orca --version", intern = TRUE)),
229225
error = function(e) "1.0.0"
230226
)
231227
}

0 commit comments

Comments
 (0)