Skip to content

Commit 31d027d

Browse files
authored
Merge pull request #1274 from ropensci/orca
start on a proper wrapper around orca
2 parents 1759873 + b922d85 commit 31d027d

File tree

6 files changed

+155
-66
lines changed

6 files changed

+155
-66
lines changed

Diff for: DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ Suggests:
6565
sf,
6666
maptools,
6767
rgeos,
68-
RSelenium,
6968
png,
70-
IRdisplay
69+
IRdisplay,
70+
processx
7171
Remotes:
7272
tidyverse/ggplot2
7373
LazyData: true

Diff for: NAMESPACE

+1
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)

Diff for: R/export.R

+6-31
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 [orca] 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.
@@ -9,43 +11,16 @@
911
#' Valid extensions include 'jpeg' | 'png' | 'webp' | 'svg' | 'pdf'
1012
#' @param selenium used only when `p` is a WebGL plot or the output
1113
#' format is 'webp' or 'svg'. Should be an object of class "rsClientServer"
12-
#' returned by `RSelenium::rsDriver` (see examples).
14+
#' returned by `RSelenium::rsDriver`.
1315
#' @param ... if `p` is non-WebGL and the output file format is
1416
#' jpeg/png/pdf arguments are passed along to `webshot::webshot()`.
1517
#' Otherwise, they are ignored.
1618
#' @export
1719
#' @author Carson Sievert
18-
#' @examples
19-
#' # The webshot package handles non-WebGL conversion to jpeg/png/pdf
20-
#' \dontrun{
21-
#' export(plot_ly(economics, x = ~date, y = ~pce))
22-
#' export(plot_ly(economics, x = ~date, y = ~pce), "plot.pdf")
23-
#'
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-
#'
35-
#' # If you can't get a selenium server running, another option is to
36-
#' # use Plotly.downloadImage() via htmlwidgets::onRender()...
37-
#' # Downloading images won't work inside RStudio, but you can set the viewer
38-
#' # option to NULL to prompt your default web browser
39-
#' options(viewer = NULL)
40-
#' plot_ly(economics, x = ~date, y = ~pce, z = ~pop) %>%
41-
#' htmlwidgets::onRender(
42-
#' "function(el, x) {
43-
#' var gd = document.getElementById(el.id);
44-
#' Plotly.downloadImage(gd, {format: 'png', width: 600, height: 400, filename: 'plot'});
45-
#' }"
46-
#' )
47-
#'}
20+
#'
4821
export <- function(p = last_plot(), file = "plotly.png", selenium = NULL, ...) {
22+
.Deprecated("orca")
23+
4924
# infer the file type
5025
fileType <- tolower(tools::file_ext(file))
5126
if (!fileType %in% c('jpeg', 'png', 'webp', 'svg', 'pdf')) {

Diff for: R/orca.R

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#' Static image export via orca
2+
#'
3+
#' The function makes a system call to 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+
#' \dontrun{
27+
#' p <- plot_ly(z = ~volcano) %>% add_surface()
28+
#' orca(p, "surface-plot.png")
29+
#' orca(p, "surface-plot.svg")
30+
#' orca(p, "surface-plot.pdf")
31+
#' }
32+
#'
33+
34+
orca <- function(p, file = "plot.png", format = tools::file_ext(file),
35+
scale = NULL, width = NULL, height = NULL, mathjax = FALSE,
36+
parallel_limit = NULL, verbose = FALSE, debug = FALSE,
37+
safe = FALSE) {
38+
39+
if (Sys.which("orca") == "") {
40+
stop(
41+
"The orca command-line utility is required to use the `orca()` function.\n\n",
42+
"Follow the installation instructions here -- https://github.com/plotly/orca#installation",
43+
call. = FALSE
44+
)
45+
}
46+
47+
b <- plotly_build(p)
48+
49+
# find the relevant plotly.js bundle
50+
plotlyjs <- plotlyjsBundle(b)
51+
plotlyjs_file <- file.path(plotlyjs$src$file, plotlyjs$script)
52+
53+
args <- c(
54+
"graph", to_JSON(b$x[c("data", "layout")]),
55+
"-o", file,
56+
"--format", format,
57+
"--plotlyjs", plotlyjs_file,
58+
if (debug) "--debug",
59+
if (verbose) "--verbose",
60+
if (safe) "--safe-mode"
61+
)
62+
63+
if (!is.null(scale)) args <- c(args, "--scale", scale)
64+
if (!is.null(width)) args <- c(args, "--width", width)
65+
if (!is.null(height)) args <- c(args, "--height", height)
66+
if (!is.null(parallel_limit)) args <- c(args, "--parallel-limit", parallel_limit)
67+
if (!is.na(mapbox_token())) args <- c(args, "--mapbox-access-token", mapbox_token())
68+
if (isTRUE(mathjax)) args <- c(args, "--mathjax", mathjax_path())
69+
70+
# TODO: point to local topojson? Should this only work if plot_geo(standalone = TRUE)?
71+
try_library("processx", "orca")
72+
invisible(processx::run("orca", args, echo = TRUE, spinner = TRUE))
73+
}
74+
75+
76+
mathjax_path <- function() {
77+
if (is_rstudio()) {
78+
try_library("rmarkdown", "orca")
79+
return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")())
80+
}
81+
path <- Sys.getenv("PLOTLY_MATHJAX_PATH", Sys.getenv("RMARKDOWN_MATHJAX_PATH", NA))
82+
if (!is.na(path)) return(normalizePath(path, mustWork = TRUE))
83+
stop(
84+
"Please set either the RMARKDOWN_MATHJAX_PATH or PLOTLY_MATHJAX_PATH ",
85+
"environment variable to the location of MathJax. ",
86+
"On Linux systems you can also install MathJax using your system package manager.",
87+
call. = FALSE
88+
)
89+
}

Diff for: man/export.Rd

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

Diff for: man/orca.Rd

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

0 commit comments

Comments
 (0)