| Title: | A 'HTTP' Server Graphics Device |
|---|---|
| Description: | A graphics device for R that is accessible via network protocols. This package was created to make it easier to embed live R graphics in integrated development environments and other applications. The included 'HTML/JavaScript' client (plot viewer) aims to provide a better overall user experience when dealing with R graphics. The device asynchronously serves graphics via 'HTTP' and 'WebSockets'. |
| Authors: | Florian Rupprecht [aut, cre] (ORCID: <https://orcid.org/0000-0002-1795-8624>), Kun Ren [ctb], Jeroen Ooms [ctb] (ORCID: <https://orcid.org/0000-0002-4035-0289>), Bill Evans [ctb] |
| Maintainer: | Florian Rupprecht <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 2.1.4 |
| Built: | 2026-11-28 13:39:54 UTC |
| Source: | https://github.com/nx10/httpgd |
Asynchronous HTTP server graphics device.
Maintainer: Florian Rupprecht [email protected] (ORCID)
Other contributors:
Kun Ren [email protected] [contributor]
Jeroen Ooms [email protected] (ORCID) [contributor]
Bill Evans [email protected] [contributor]
Useful links:
Report bugs at https://github.com/nx10/httpgd/issues
This function initializes a httpgd graphics device and starts a local webserver, that allows for access via HTTP and WebSockets. A link will be printed by which the web client can be accessed using a browser.
hgd( host = getOption("httpgd.host", "127.0.0.1"), port = getOption("httpgd.port", 0), cors = getOption("httpgd.cors", FALSE), token = getOption("httpgd.token", TRUE), silent = getOption("httpgd.silent", FALSE), width = getOption("httpgd.width", 720), height = getOption("httpgd.height", 576), zoom = getOption("httpgd.zoom", 1), bg = getOption("httpgd.bg", "white"), pointsize = getOption("httpgd.pointsize", 12), system_fonts = getOption("httpgd.system_fonts", list()), user_fonts = getOption("httpgd.user_fonts", list()), reset_par = getOption("httpgd.reset_par", FALSE), title = getOption("httpgd.title", "httpgd") )hgd( host = getOption("httpgd.host", "127.0.0.1"), port = getOption("httpgd.port", 0), cors = getOption("httpgd.cors", FALSE), token = getOption("httpgd.token", TRUE), silent = getOption("httpgd.silent", FALSE), width = getOption("httpgd.width", 720), height = getOption("httpgd.height", 576), zoom = getOption("httpgd.zoom", 1), bg = getOption("httpgd.bg", "white"), pointsize = getOption("httpgd.pointsize", 12), system_fonts = getOption("httpgd.system_fonts", list()), user_fonts = getOption("httpgd.user_fonts", list()), reset_par = getOption("httpgd.reset_par", FALSE), title = getOption("httpgd.title", "httpgd") )
host |
Server hostname. Set to |
port |
Server port. If this is set to |
cors |
Toggles Cross-Origin Resource Sharing (CORS) header.
When set to |
token |
(Optional) security token. When set, all requests
need to include a token to be allowed. (Either in a request header
( |
silent |
When set to |
width |
Initial plot width (pixels). |
height |
Initial plot height (pixels). |
zoom |
Initial plot zoom level (For example: 2 corresponds to 200%, 0.5 would be 50%.). |
bg |
Background color. |
pointsize |
Graphics device point size. |
system_fonts |
Named list of font names to be aliased with
fonts installed on your system. If unspecified, the R default
families |
user_fonts |
Named list of fonts to be aliased with font files
provided by the user rather than fonts properly installed on the
system. The aliases can be fonts from the fontquiver package,
strings containing a path to a font file, or a list containing
|
reset_par |
If set to |
title |
Document title shown in the browser tab of connected web
clients. Useful to tell multiple R processes apart. Can be changed later
with |
All font settings and descriptions are adopted from the excellent 'svglite' package.
No return value, called to initialize graphics device.
## Not run: hgd() # Initialize graphics device and start server hgd_browse() # Or copy the displayed link in the browser # Plot something x <- seq(0, 3 * pi, by = 0.1) plot(x, sin(x), type = "l") dev.off() # alternatively: hgd_close() ## End(Not run)## Not run: hgd() # Initialize graphics device and start server hgd_browse() # Or copy the displayed link in the browser # Plot something x <- seq(0, 3 * pi, by = 0.1) plot(x, sin(x), type = "l") dev.off() # alternatively: hgd_close() ## End(Not run)
This function will only work after starting a device with hgd().
hgd_browse(..., which = dev.cur(), browser = getOption("browser"))hgd_browse(..., which = dev.cur(), browser = getOption("browser"))
... |
Parameters passed to |
which |
Which device (ID). |
browser |
Program to be used as HTML browser. |
URL.
## Not run: hgd() hgd_browse() # open browser hist(rnorm(100)) dev.off() ## End(Not run)## Not run: hgd() hgd_browse() # open browser hist(rnorm(100)) dev.off() ## End(Not run)
This achieves the same effect as grDevices::dev.off(),
but will only close the device if it has the httpgd type.
hgd_close(which = dev.cur(), all = FALSE)hgd_close(which = dev.cur(), all = FALSE)
which |
Which device (ID). |
all |
Should all running httpgd devices be closed. |
Number and name of the new active device (after the specified device has been shut down).
## Not run: hgd() hgd_browse() # open browser hist(rnorm(100)) hgd_close() hgd() hgd() hgd() hgd_close(all = TRUE) ## End(Not run)## Not run: hgd() hgd_browse() # open browser hist(rnorm(100)) hgd_close() hgd() hgd() hgd() hgd_close(all = TRUE) ## End(Not run)
Access status information of a httpgd graphics device.
This function will only work after starting a device with hgd().
hgd_details(which = dev.cur())hgd_details(which = dev.cur())
which |
Which device (ID). |
List of status variables with the following named items:
$host: Server hostname,
$port: Server port,
$token: Security token,
$title: Browser tab title (see hgd_title()),
$hsize: Plot history size (how many plots are accessible),
$upid: Update ID (changes when the device has received new information),
$active: Is the device the currently activated device.
## Not run: hgd() hgd_details() plot(1, 1) hgd_details() dev.off() ## End(Not run)## Not run: hgd() hgd_details() plot(1, 1) hgd_details() dev.off() ## End(Not run)
This is mainly used internally by httpgd, but exposed for testing purposes.
hgd_generate_token(len)hgd_generate_token(len)
len |
Token length (number of characters). |
Random token string.
hgd_generate_token(6)hgd_generate_token(6)
Sets the document title shown in the browser tab of web clients connected
to a httpgd graphics device. Connected clients are updated immediately,
without having to reload the page. The initial title can be set with the
title argument of hgd(). When called without a title argument, the
current title is returned.
This function will only work after starting a device with hgd().
hgd_title(title, which = dev.cur())hgd_title(title, which = dev.cur())
title |
New title string. If missing, the current title is returned. |
which |
Which device (ID). |
The current title (when title is missing), otherwise the new
title, invisibly.
## Not run: hgd(title = "Analysis A") hgd_title("Analysis B") # connected clients update immediately hgd_title() # returns "Analysis B" dev.off() ## End(Not run)## Not run: hgd(title = "Analysis A") hgd_title("Analysis B") # connected clients update immediately hgd_title() # returns "Analysis B" dev.off() ## End(Not run)
Generate URLs to the plot viewer or to plot SVGs.
This function will only work after starting a device with hgd().
hgd_url( endpoint = "live", which = dev.cur(), host = NA, port = NA, explicit = FALSE, omit_token = FALSE, ... )hgd_url( endpoint = "live", which = dev.cur(), host = NA, port = NA, explicit = FALSE, omit_token = FALSE, ... )
endpoint |
API endpoint. The default, |
which |
Which device (ID). |
host |
Replaces hostname. Can also be a base URL with scheme and
optional path prefix (for example |
port |
Replaces port. |
explicit |
Adds a |
omit_token |
Should the security token be omitted from the URL. |
... |
Other query parameters that will be appended to the URL.
These override the |
Note: If the included client is used, set ws=0 (or websockets=0) to
poll instead of using a WebSocket, or sidebar=0 (or history=0) to hide
the plot history sidebar.
The client finds the server relative to the page it was loaded from, so it
also works behind reverse proxies that serve it under a path prefix. See
vignette("b04_remote-access", package = "httpgd").
URL.
## Not run: hgd() my_url <- hgd_url() hgd_url(0) # last plot hgd_url(1, renderer = "png") # first plot hgd_url(unigd::ugd_id(), width = 800, height = 600) dev.off() ## End(Not run)## Not run: hgd() my_url <- hgd_url() hgd_url(0) # last plot hgd_url(1, renderer = "png") # first plot hgd_url(unigd::ugd_id(), width = 800, height = 600) dev.off() ## End(Not run)
Global option viewer needs to be set to a function that accepts the client
URL as a parameter.
hgd_view()hgd_view()
This function will only work after starting a device with hgd().
viewer function return value.
## Not run: hgd() hgd_view() hist(rnorm(100)) dev.off() ## End(Not run)## Not run: hgd() hgd_view() hist(rnorm(100)) dev.off() ## End(Not run)
This function may be used to rapidly develop visualizations by replacing a workflow of reloading and executing code manually.
hgd_watch( watch = list.files(pattern = "\\.R$", ignore.case = TRUE), on_change = function(changed_files) { print(changed_files) }, interval = 1, on_start = hgd_browse, on_exit = NULL, on_error = print, reset_par = TRUE, ... )hgd_watch( watch = list.files(pattern = "\\.R$", ignore.case = TRUE), on_change = function(changed_files) { print(changed_files) }, interval = 1, on_start = hgd_browse, on_exit = NULL, on_error = print, reset_par = TRUE, ... )
watch |
Paths that are watched for changes (see |
on_change |
Will be called when a file changes. |
interval |
Time interval in which changes are detected (in seconds). |
on_start |
Will be called after the httpgd server is started
(may be set to |
on_exit |
Will be called before the server is closed
(may be set to |
on_error |
Will be called when on_change throws an error
(may be set to |
reset_par |
If set to |
... |
Additional parameters passed to |
No return value, called for side effects.
## Not run: # --- my_code.R --- plot(rnorm(100), col = "red") # --- Other file / interactive --- hgd_watch( watch = "my_code.R", # When my_code.R changes... on_change = function(...) { source("my_code.R") # ...call updated plot function. } ) ## End(Not run)## Not run: # --- my_code.R --- plot(rnorm(100), col = "red") # --- Other file / interactive --- hgd_watch( watch = "my_code.R", # When my_code.R changes... on_change = function(...) { source("my_code.R") # ...call updated plot function. } ) ## End(Not run)