Skip to content

Commit 7e8e66b

Browse files
committed
remove R6 dependencies and have orca_serve be function that returns closures
1 parent f788ddd commit 7e8e66b

File tree

4 files changed

+58
-63
lines changed

4 files changed

+58
-63
lines changed

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ Imports:
4444
crosstalk,
4545
purrr,
4646
data.table,
47-
promises,
48-
R6
47+
promises
4948
Suggests:
5049
MASS,
5150
maps,

R/orca.R

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
8787
#' \strong{Usage}
8888
#'
8989
#' \code{
90-
#' orca_serve$new(
90+
#' orca_serve(
9191
#' port = 5151, mathjax = FALSE, safe = FALSE, request_limit = NULL, keep_alive = TRUE,
9292
#' window_max_number = NULL, quiet = FALSE, debug = FALSE, ...
9393
#' )
@@ -143,7 +143,7 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
143143
#'
144144
#' \dontrun{
145145
#' # launch the server
146-
#' server <- orca_serve$new()
146+
#' server <- orca_serve()
147147
#'
148148
#' # export as many graphs as you'd like
149149
#' server$export(qplot(1:10), "test1.pdf")
@@ -161,56 +161,54 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
161161
#' unlink("test2.pdf")
162162
#' }
163163

164-
orca_serve <- R6::R6Class(
165-
"orcaServe",
166-
public = list(
167-
process = NULL,
168-
port = NULL,
169-
initialize = function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit = NULL,
170-
keep_alive = TRUE, window_max_number = NULL, quiet = FALSE,
171-
debug = FALSE, xvfb = FALSE, ...) {
172-
173-
# make sure we have the required infrastructure
174-
orca_available()
175-
try_library("processx", "orca_serve")
176-
177-
# use main bundle since any plot can be thrown at the server
178-
plotlyjs <- plotlyMainBundle()
179-
plotlyjs_file <- file.path(plotlyjs$src$file, plotlyjs$script)
180-
181-
args <- c(
182-
"serve",
183-
"-p", port,
184-
"--plotly", plotlyjs_file,
185-
if (safe) "--safe-mode",
186-
#if (orca_version() >= "1.1.1") "--graph-only",
187-
if (keep_alive) "--keep-alive",
188-
if (debug) "--debug",
189-
if (quiet) "--quiet"
190-
)
191-
192-
if (!is.null(request_limit))
193-
args <- c(args, "--request-limit", request_limit)
194-
195-
if (!is.null(window_max_number))
196-
args <- c(args, "--window-max-number", window_max_number)
197-
198-
if (!is.null(tryNULL(mapbox_token())))
199-
args <- c(args, "--mapbox-access-token", mapbox_token())
200-
201-
if (isTRUE(mathjax))
202-
args <- c(args, "--mathjax", file.path(mathjax_path(), "MathJax.js"))
203-
204-
if (xvfb) {
205-
self$process <- processx::process$new("xvfb-run", c("orca", args), ...)
206-
} else {
207-
self$process <- processx::process$new("orca", args, ...)
208-
}
209-
210-
self$port <- port
164+
165+
orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit = NULL,
166+
keep_alive = TRUE, window_max_number = NULL, quiet = FALSE,
167+
debug = FALSE, xvfb = FALSE, ...) {
168+
169+
# make sure we have the required infrastructure
170+
orca_available()
171+
try_library("processx", "orca_serve")
172+
173+
# use main bundle since any plot can be thrown at the server
174+
plotlyjs <- plotlyMainBundle()
175+
plotlyjs_file <- file.path(plotlyjs$src$file, plotlyjs$script)
176+
177+
args <- c(
178+
"serve",
179+
"-p", port,
180+
"--plotly", plotlyjs_file,
181+
if (safe) "--safe-mode",
182+
#if (orca_version() >= "1.1.1") "--graph-only",
183+
if (keep_alive) "--keep-alive",
184+
if (debug) "--debug",
185+
if (quiet) "--quiet"
186+
)
187+
188+
if (!is.null(request_limit))
189+
args <- c(args, "--request-limit", request_limit)
190+
191+
if (!is.null(window_max_number))
192+
args <- c(args, "--window-max-number", window_max_number)
193+
194+
if (!is.null(tryNULL(mapbox_token())))
195+
args <- c(args, "--mapbox-access-token", mapbox_token())
196+
197+
if (isTRUE(mathjax))
198+
args <- c(args, "--mathjax", file.path(mathjax_path(), "MathJax.js"))
199+
200+
process <- if (xvfb) {
201+
processx::process$new("xvfb-run", c("orca", args), ...)
202+
} else {
203+
processx::process$new("orca", args, ...)
204+
}
205+
206+
list(
207+
port = port,
208+
close = function() {
209+
process$kill()
211210
},
212211
export = function(p, file = "plot.png", format = tools::file_ext(file), scale = NULL, width = NULL, height = NULL) {
213-
214212
# request/response model works similarly to plotly_IMAGE()
215213
bod <- list(
216214
figure = plotly_build(p)$x[c("data", "layout")],
@@ -220,19 +218,17 @@ orca_serve <- R6::R6Class(
220218
scale = scale
221219
)
222220
res <- httr::POST(
223-
paste0("http://127.0.0.1:", self$port),
221+
paste0("http://127.0.0.1:", port),
224222
body = to_JSON(bod)
225223
)
226224
httr::stop_for_status(res)
227225
httr::warn_for_status(res)
228226
con <- httr::content(res, as = "raw")
229227
writeBin(con, file)
230-
},
231-
close = function() {
232-
self$process$kill()
233228
}
234229
)
235-
)
230+
}
231+
236232

237233
orca_available <- function() {
238234
if (Sys.which("orca") == "") {

man/orca_serve.Rd

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper-vdiffr.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (enable_vdiffr) {
2121
# make sure orca cli is available
2222
orca_available()
2323
# try and start up the node process
24-
orcaImageServer <- try(orca_serve$new(port, xvfb = Sys.getenv("USE_XVFB", FALSE)), silent = TRUE)
24+
orcaImageServer <- try(orca_serve(port, xvfb = as.logical(Sys.getenv("USE_XVFB", FALSE))), silent = TRUE)
2525
if (inherits(orcaImageServer, 'try-error')) {
2626
stop(
2727
"Tried to open orca server on port '", port, "', but it's not available. ",
@@ -55,9 +55,11 @@ if (enable_vdiffr) {
5555
# force the vdiffr shiny app to open in a real browser
5656
# (some svg files seem to not render properly in RStudio)
5757
options(shiny.launch.browser = TRUE)
58+
59+
message("Visual testing is enabled.")
5860
} else {
5961

60-
skip("Visual testing is not enabled/")
62+
message("Visual testing is not enabled.")
6163

6264
}
6365

0 commit comments

Comments
 (0)