1- # ' Static image export via orca
1+ # ' Static image exporting
22# '
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)
3+ # ' Export plotly objects to static images (e.g., pdf, png, jpeg, svg, etc) via the
4+ # ' [orca command-line utility](https://github.com/plotly/orca#installation).
5+ # '
6+ # ' The `orca()` function is designed for exporting one plotly graph whereas `orca_serve()`
7+ # ' is meant for exporting many graphs at once. The former starts and stops an external (nodejs)
8+ # ' 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
10+ # ' the external (background) process.
511# '
612# ' @param p a plotly object.
713# ' @param file output filename.
2228# ' hang during image generating are skipped.
2329# ' @export
2430# ' @author Carson Sievert
25- # ' @seealso [orca_serve]
31+ # ' @md
32+ # ' @rdname orca
2633# ' @examples
2734# '
2835# ' \dontrun{
2936# ' p <- plot_ly(z = ~volcano) %>% add_surface()
3037# ' orca(p, "surface-plot.svg")
38+ # '
39+ # ' #' # launch the server
40+ # ' server <- orca_serve()
41+ # '
42+ # ' # export as many graphs as you'd like
43+ # ' server$export(qplot(1:10), "test1.pdf")
44+ # ' server$export(plot_ly(x = 1:10, y = 1:10), "test2.pdf")
45+ # '
46+ # ' # the underlying process is exposed as a field, so you
47+ # ' # have full control over the external process
48+ # ' server$process$is_alive()
49+ # '
50+ # ' # convenience method for closing down the server
51+ # ' server$close()
52+ # '
53+ # ' # remove the exported files from disk
54+ # ' unlink("test1.pdf")
55+ # ' unlink("test2.pdf")
3156# ' }
3257# '
3358
@@ -71,96 +96,36 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file),
7196
7297# ' Orca image export server
7398# '
74- # ' Compared to [orca], [orca_serve] is more efficient at exporting many plotly
75- # ' graphs because the former must startup/shutdown an external process for every image.
76- # ' The server (background) process is launched upon initialization of a [orca_serve] class
77- # ' (i.e., when the `new()` method is called). The `export()` method accepts any valid plotly
78- # ' object as input and spits out an image file to disk. To kill the background server process,
79- # ' use `close()`.
80- # '
81- # ' @usage NULL
82- # ' @format NULL
83- # '
84- # ' @section Initialization:
85- # ' A new 'orcaServe'-object is initialized using the new() method on the generator:
86- # '
87- # ' \strong{Usage}
88- # '
89- # ' \code{
90- # ' orca_serve(
91- # ' port = 5151, mathjax = FALSE, safe = FALSE, request_limit = NULL, keep_alive = TRUE,
92- # ' window_max_number = NULL, quiet = FALSE, debug = FALSE, ...
93- # ' )
94- # ' }
95- # '
96- # ' \strong{Arguments}
97- # '
98- # ' \describe{
99- # ' \item{\code{port}}{Sets the server's port number.}
100- # ' \item{\code{mathjax}}{
101- # ' whether or not to include MathJax (required to render [TeX]).
102- # ' If `TRUE`, the PLOTLY_MATHJAX_PATH environment variable must be set and point
103- # ' to the location of MathJax (this variable is also used to render [TeX] in
104- # ' interactive graphs, see [config]).
105- # ' }
106- # ' \item{\code{safe}}{
107- # ' Turns on safe mode: where figures likely to make browser window hang during image generating are skipped.
108- # ' }
109- # ' \item{\code{request_limit}}{
110- # ' Sets a request limit that makes orca exit when reached.
111- # ' }
112- # ' \item{\code{keep_alive}}{
113- # ' Turn on keep alive mode where orca will (try to) relaunch server if process unexpectedly exits.
114- # ' }
115- # ' \item{\code{window_max_number}}{
116- # ' Sets maximum number of browser windows the server can keep open at a given time.
117- # ' }
118- # ' \item{\code{quiet}}{Suppress all logging info.}
119- # ' \item{\code{debug}}{Starts app in debug mode.}
120- # ' \item{\code{xvfb}}{Whether to run orca via X virtual framebuffer. May be necessary in a headless environment}
121- # ' }
99+ # ' @inheritParams orca
100+ # ' @param port Sets the server's port number.
101+ # ' @param keep_alive Turn on keep alive mode where orca will (try to) relaunch server if process unexpectedly exits.
102+ # ' @param window_max_number Sets maximum number of browser windows the server can keep open at a given time.
103+ # ' @param request_limit Sets a request limit that makes orca exit when reached.
104+ # ' @param quiet Suppress all logging info.
105+ # ' @param xvfb Whether to run orca via X virtual framebuffer. May be necessary in a headless environment
122106# '
123107# ' @section Methods:
124108# '
109+ # ' The `orca_serve()` function returns an object with two methods:
110+ # '
125111# ' \describe{
126112# ' \item{\code{export(p, file = "plot.png", format = tools::file_ext(file), scale = NULL, width = NULL, height = NULL)}}{
127- # ' Export a static image of a plotly graph. Arguments found here are the same as those found in [ orca].
113+ # ' Export a static image of a plotly graph. Arguments found here are the same as those found in ` orca()`
128114# ' }
129115# ' \item{\code{close()}}{Close down the orca server and kill the underlying node process.}
130116# ' }
131117# '
132118# ' @section Fields:
133119# '
120+ # ' The `orca_serve()` function returns an object with two fields:
121+ # '
134122# ' \describe{
135123# ' \item{\code{port}}{The port number that the server is listening to.}
136124# ' \item{\code{process}}{An R6 class for controlling and querying the underlying node process.}
137125# ' }
138126# '
139127# ' @export
140- # ' @author Carson Sievert
141- # ' @seealso [orca]
142- # ' @examples
143- # '
144- # ' \dontrun{
145- # ' # launch the server
146- # ' server <- orca_serve()
147- # '
148- # ' # export as many graphs as you'd like
149- # ' server$export(qplot(1:10), "test1.pdf")
150- # ' server$export(plot_ly(x = 1:10, y = 1:10), "test2.pdf")
151- # '
152- # ' # the underlying process is exposed as a field, so you
153- # ' # have full control over the external process
154- # ' server$process$is_alive()
155- # '
156- # ' # convenience method for closing down the server
157- # ' server$close()
158- # '
159- # ' # remove the exported files from disk
160- # ' unlink("test1.pdf")
161- # ' unlink("test2.pdf")
162- # ' }
163-
128+ # ' @rdname orca
164129
165130orca_serve <- function (port = 5151 , mathjax = FALSE , safe = FALSE , request_limit = NULL ,
166131 keep_alive = TRUE , window_max_number = NULL , quiet = FALSE ,
@@ -205,9 +170,8 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
205170
206171 list (
207172 port = port ,
208- close = function () {
209- process $ kill()
210- },
173+ process = process ,
174+ close = function () process $ kill(),
211175 export = function (p , file = " plot.png" , format = tools :: file_ext(file ), scale = NULL , width = NULL , height = NULL ) {
212176 # request/response model works similarly to plotly_IMAGE()
213177 bod <- list (
@@ -218,7 +182,7 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
218182 scale = scale
219183 )
220184 res <- httr :: POST(
221- paste0(" http://127.0.0.1 :" , port ),
185+ paste0(" http://localhost :" , port ),
222186 body = to_JSON(bod )
223187 )
224188 httr :: stop_for_status(res )
0 commit comments