httpgd can be accessed both from R and from HTTP/WebSockets.
| R | HTTP | Description |
| ----------------------------------- | ------------------------------ | ----------------------------------- |
| hgd()
| | Initialize device and start server. |
| hgd_close()
| | Helper: Close device. |
| hgd_url()
| | Helper: URL generation. |
| hgd_browse()
| | Helper: Open browser. |
| ugd_state()
| /state
| Get current server state. |
| ugd_renderers()
| /renderers
| Get list of available renderers. |
| ugd_render()
| /plot
| Get rendered plot (any format). |
| ugd_clear()
| /clear
| Remove all plots. |
| ugd_remove()
| /remove
| Remove a single plot. |
| ugd_id()
| /plots
| Get static plot IDs. |
| | /live
| Live server page. |
While all the APIs can be accessed stateless, the graphics device does have a state defined by.
| Field | Type | Description |
| -------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| upid
| int
| Update id. Changes when plots are removed or when something is drawn. |
| hsize
| int
| Number of plots in the history. |
| active
| bool
| Whether the graphics device is active. When another graphics device is activated, the device will become inactive and not be able to render any plots that are not cached (no resizes). |
To receive state changes as they happen WebSockets can be used. Alternatively /state
may be polled repeatedly.
unigd::ugd_state()
Note: Prior to httpgd 2.0
this function also returned host
, port
and security token
of the server. These fields are now accessed via hgd_details()
.
/state
| Key | Value | Default |
| ------- | ---------------------------- | ------------------------------------------------------- |
| token
| Security token. | (The X-HTTPGD-TOKEN
header can be set alternatively.) |
Will respond with a JSON object.
httpgd accepts WebSocket connections on the same port as the HTTP server. Server state changes will be broadcasted immediately to all connected clients in JSON format.
httpgd includes multiple renderers that can dynamically render plots to different target formats. As new formats may be added as the development on httpgd continues, and some depend on optional system dependencies, a list of available renderers can be obtained during runtime.
The following is a complete list of renderers.
df <- unigd::ugd_renderers() df <- df[order(df$id),] knitr::kable(data.frame( sprintf("`%s`", df$id), sprintf("`%s`", df$mime), df$descr, ifelse(df$text, "Text", "Binary") ), col.names = c("ID", "Mime-Type", "Renderer", "Format"))
unigd::ugd_renderers()
Returns a data frame.
/renderers
| Key | Value | Default |
| ------- | ---------------------------- | ------------------------------------------------------- |
| token
| Security token. | (The X-HTTPGD-TOKEN
header can be set alternatively.) |
Plots can be rendered in various file formats from both R and HTTP. The actual plot construction in R is relatively slow so httpgd caches the plot in the last requested size. Subsequent calls with the same width and height or without a size specified will always be fast. (This way "flipping" through plot pages is very fast.)
Example:
unigd::ugd_render(page = 3, width = 800, height = 600) # Get plot at index 3 with 800*600 unigd::ugd_render() # Get last plot with cached size
page
can either be a number to indicate a plot index or a static plot ID (see: hgd_id()).
This function returns the plot as a string. The file
attribute can be used to save the SVG directly to disk.
Example:
/plot?index=2&width=800&height=600
Parameters:
| Key | Value | Default |
| ---------- | ---------------------------- | ------------------------------------------------------- |
| width
| With in pixels. | Last rendered width. (Initially device width.) |
| height
| Height in pixels. | Last rendered height. (Initially device height.) |
| zoom
| Zoom level. | 1
(No zoom). 0.5
would be 50% and 2
200%. |
| index
| Plot history index. | Newest plot. |
| id
| Static plot ID. | index
will be used. |
| renderer
| Renderer. | svg
. |
| token
| Security token. | (The X-HTTPGD-TOKEN
header can be set alternatively.) |
Note that the HTTP API uses 0-based indexing and the R API 1-based indexing. This is done to conform to R and JavaScript on both ends. (This means the the first plot is accessed with
/plot?index=0
andunigd::ugd_render(page = 1)
.)
Examples:
unigd::ugd_remove(page = 2) # Remove the second page unigd::ugd_clear() # Clear all pages
Examples:
/remove?index=2 /clear
| Key | Value | Default |
| ------- | ---------------------------- | ------------------------------------------------------- |
| index
| Plot history index. | Newest plot. |
| id
| Static plot ID. | index
will be used. |
| token
| Security token. | (The X-HTTPGD-TOKEN
header can be set alternatively.) |
The problem with requesting individual plots by index is, that a plots index will change when earlier plots are removed from the plot history. To circumvent this, each plot also is assigned a static ID.
All APIs that access individual plots can also be called with static IDs instead of indices.
Examples:
unigd::ugd_id(index = 2) # Static ID of the second plot unigd::ugd_id() # Static ID of the last plot
Note: The limit
parameter can be adjusted to obtain multiple or all plot IDs.
Examples:
/plots?index=2 /plots
| Key | Value | Default |
| ------- | ------------------------------ | ------------------------------------------------------- |
| index
| Plot history index. | Newest plot. |
| limit
| Number of subsequent plot IDs. | 1 |
| token
| Security token. | (The X-HTTPGD-TOKEN
header can be set alternatively.) |
Notes:
limit
parameter can be specified to support pagination.A security token can be set when starting the device:
hgd(..., token = "secret")
When set, each API request has to include this token inside the header X-HTTPGD-TOKEN
or as a query param ?token=secret
.
token
is by default set to TRUE
to generate a random 8 character alphanumeric token. If it is set to a number, a random token of that length will be generated. FALSE
deactivates the security token.
CORS is off by default but can be enabled on startup:
hgd(..., cors = TRUE)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.