Skip to content

Commit 80008f5

Browse files
committed
start on a proper wrapper around orca
1 parent 1759873 commit 80008f5

File tree

6 files changed

+151
-24
lines changed

6 files changed

+151
-24
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Suggests:
6767
rgeos,
6868
RSelenium,
6969
png,
70-
IRdisplay
70+
IRdisplay,
71+
processx
7172
Remotes:
7273
tidyverse/ggplot2
7374
LazyData: true

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export(layout)
149149
export(mutate)
150150
export(mutate_)
151151
export(offline)
152+
export(orca)
152153
export(partial_bundle)
153154
export(plot_dendro)
154155
export(plot_geo)

R/export.R

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#' Export a plotly graph to a static file
22
#'
3+
#' This function is in the process of being deprecated. Use the `[orca]()` function instead.
4+
#'
35
#' @details For SVG plots, a screenshot is taken via `webshot::webshot()`.
46
#' Since `phantomjs` (and hence `webshot`) does not support WebGL,
57
#' the RSelenium package is used for exporting WebGL plots.
@@ -21,17 +23,6 @@
2123
#' export(plot_ly(economics, x = ~date, y = ~pce))
2224
#' export(plot_ly(economics, x = ~date, y = ~pce), "plot.pdf")
2325
#'
24-
#' # svg/webp output or WebGL conversion can be done via RSelenium
25-
#' if (requireNamespace("RSelenium")) {
26-
#' rD <- RSelenium::rsDriver(browser = "chrome")
27-
#' export(
28-
#' plot_ly(economics, x = ~date, y = ~pce), "plot.svg", rD
29-
#' )
30-
#' export(
31-
#' plot_ly(economics, x = ~date, y = ~pce, z = ~pop), "yay.svg", rD
32-
#' )
33-
#' }
34-
#'
3526
#' # If you can't get a selenium server running, another option is to
3627
#' # use Plotly.downloadImage() via htmlwidgets::onRender()...
3728
#' # Downloading images won't work inside RStudio, but you can set the viewer
@@ -46,6 +37,11 @@
4637
#' )
4738
#'}
4839
export <- function(p = last_plot(), file = "plotly.png", selenium = NULL, ...) {
40+
message(
41+
"The `export()` function is in the process of being deprecated. ",
42+
"Please use the `orca()` function instead."
43+
)
44+
4945
# infer the file type
5046
fileType <- tolower(tools::file_ext(file))
5147
if (!fileType %in% c('jpeg', 'png', 'webp', 'svg', 'pdf')) {

R/orca.R

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#' Static image export via orca
2+
#'
3+
#' The function requires the orca command-line utility,
4+
#' see the installation instructions [here](https://github.com/plotly/orca#installation)
5+
#'
6+
#' @param p a plotly object.
7+
#' @param file output filename.
8+
#' @param format the output format (png, jpeg, webp, svg, pdf, eps).
9+
#' @param scale Sets the image scale. Applies to all output images.
10+
#' @param width Sets the image width. If not set, defaults to `layout.width` value.
11+
#' Applies to all output images.
12+
#' @param height Sets the image height. If not set, defaults to `layout.height` value.
13+
#' Applies to all output images.
14+
#' @param mathjax whether or not to specify a path to mathjax (required to export LaTeX characters).
15+
#' This should 'just work' in RStudio, but outside RStudio, you may have to set
16+
#' the PLOTLY_MATHJAX_PATH environment variable to the location of MathJax.
17+
#' @param parallel_limit Sets the limit of parallel tasks run.
18+
#' @param verbose Turn on verbose logging on stdout.
19+
#' @param debug Starts app in debug mode and turn on verbose logs on stdout.
20+
#' @param safe Turns on safe mode: where figures likely to make browser window
21+
#' hang during image generating are skipped.
22+
#' @export
23+
#' @author Carson Sievert
24+
#' @examples
25+
#'
26+
#' p <- plot_ly(z = ~volcano) %>% add_surface()
27+
#' orca(p, "surface-plot.png")
28+
#' orca(p, "surface-plot.svg")
29+
#' orca(p, "surface-plot.pdf")
30+
#'
31+
32+
orca <- function(p, file = "plot.png", format = tools::file_ext(file),
33+
scale = NULL, width = NULL, height = NULL, mathjax = FALSE,
34+
parallel_limit = NULL, verbose = FALSE, debug = FALSE,
35+
safe = FALSE) {
36+
37+
if (Sys.which("orca") == "") {
38+
stop(
39+
"The orca command-line utility is required to use the `orca()` function.\n\n",
40+
"Follow the installation instructions here -- https://github.com/plotly/orca#installation",
41+
call. = FALSE
42+
)
43+
}
44+
45+
b <- plotly_build(p)
46+
47+
# find the relevant plotly.js bundle
48+
plotlyjs <- plotlyjsBundle(b)
49+
plotlyjs_file <- file.path(plotlyjs$src$file, plotlyjs$script)
50+
51+
args <- c(
52+
"graph", plotly:::to_JSON(b$x[c("data", "layout")]),
53+
"-o", file,
54+
"--format", format,
55+
"--plotlyjs", plotlyjs_file,
56+
if (debug) "--debug",
57+
if (verbose) "--verbose",
58+
if (safe) "--safe-mode"
59+
)
60+
61+
if (!is.null(scale)) args <- c(args, "--scale", scale)
62+
if (!is.null(width)) args <- c(args, "--width", width)
63+
if (!is.null(height)) args <- c(args, "--height", height)
64+
if (!is.null(parallel_limit)) args <- c(args, "--parallel-limit", parallel_limit)
65+
if (!is.na(mapbox_token())) args <- c(args, "--mapbox-access-token", mapbox_token())
66+
if (isTRUE(mathjax)) args <- c(args, "--mathjax", mathjax_path())
67+
68+
# TODO: point to local topojson? Should this only work if plot_geo(standalone = TRUE)?
69+
try_library("processx", "orca")
70+
invisible(processx::run("orca", args, echo = TRUE, spinner = TRUE))
71+
}
72+
73+
74+
mathjax_path <- function() {
75+
if (is_rstudio()) {
76+
try_library("rmarkdown", "orca")
77+
return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")())
78+
}
79+
path <- Sys.getenv("PLOTLY_MATHJAX_PATH", Sys.getenv("RMARKDOWN_MATHJAX_PATH", NA))
80+
if (!is.na(path)) return(normalizePath(path, mustWork = TRUE))
81+
stop(
82+
"Please set either the RMARKDOWN_MATHJAX_PATH or PLOTLY_MATHJAX_PATH ",
83+
"environment variable to the location of MathJax. ",
84+
"On Linux systems you can also install MathJax using your system package manager.",
85+
call. = FALSE
86+
)
87+
}

man/export.Rd

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/orca.Rd

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

0 commit comments

Comments
 (0)