httpgd
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 SVG graphics via HTTP and WebSockets.
Features
- Fast high quality SVG plots
- Stateless asynchronous HTTP/WebSocket API
- Plot resizing
- Plot history
- HTML/JavaScript client (TypeScript module)
- Multiple concurrent clients
Demo
Installation
devtools::install_github("nx10/httpgd")
See system requirements for troubleshooting.
Usage
Initialize graphics device and start server with:
hgd()
Copy the displayed link in the browser or call
hgd_browse()
to open a browser window automatically.
Plot anything.
x = seq(0, 3 * pi, by = 0.1)
plot(x, sin(x), type = "l")
Every plotting library will work.
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()
Stop the server with:
dev.off()
Keyboard shortcuts
Keys | Result |
---|---|
← → ↑ ↓ | Navigate plot history. |
N | Jump to the newest plot. |
del / D | Delete plot. |
+ / - | Zoom in and out. |
0 | Reset zoom level. |
S | Download plot as SVG. |
P | Download plot as PNG. |
C | Copy plot to clipboard (as PNG). |
T | Clear all plots. |
API & Documentation
httpgd
can be accessed both from R and from HTTP:
Technical documentation for developers wanting to contribute can be found here.
Benchmark
There are currently no other network graphics devices for comparison, httpgd
can be used in offline mode (with hgd(webserver = FALSE)
) to compare it with conventional SVG graphics devices.
The benchmark from svglite has the following results:
library(svglite)
library(httpgd)
set.seed(1234)
x <- runif(1e3)
y <- runif(1e3)
tmp1 <- tempfile()
tmp2 <- tempfile()
tmp3 <- tempfile()
svglite_test <- function() {
svglite(tmp1)
plot(x, y)
dev.off()
}
svg_test <- function() {
svg(tmp2, onefile = TRUE)
plot(x, y)
dev.off()
}
httpgd_test <- function() {
hgd(webserver = FALSE)
plot(x, y)
hgd_svg(file = tmp3)
dev.off()
}
ben <-
bench::mark(httpgd_test(), svglite_test(), svg_test(), iterations = 250)
expression | min | median | itr/sec | mem_alloc | gc/sec | n_itr | n_gc | total_time |
---|---|---|---|---|---|---|---|---|
httpgd_test() | 10.2ms | 10.8ms | 91.43165 | 361KB | 0.7373520 | 248 | 2 | 2.71s |
svglite_test() | 20.3ms | 21.4ms | 46.29561 | 593KB | 0.5622948 | 247 | 3 | 5.33s |
svg_test() | 27.2ms | 28.3ms | 35.15964 | 126KB | 0.1412034 | 249 | 1 | 7.08s |
Package versions: httpgd
(1.0.0 dev), svglite
(1.2.3.2), grDevices
(4.0.3)
System requirements
Depends on R
version ≥ 4.0.
libpng
and X11 are required on unix like systems (e.g. Linux, macOS).
macOS
If libpng
is missing install it via:
brew install libpng
If X11
is missing the error message will include the text:
unable to load shared object [...] systemfonts/libs/systemfonts.so [...]
Install XQuartz
.
(see: https://github.com/r-lib/systemfonts/issues/17)
Help welcome!
The various components of httpgd
are written in C++, R and TypeScript. We welcome contributions of any kind.
Other areas in need of improvement are: Testing, documentation, net security and continuous integration.
If you feel lost, the technical documentation might help.
Links & Articles
About & License
Depends on cpp11
, later
and systemfonts
.
Webserver based on Boost/Beast
included in the BH
package.
Much of the font handling and SVG rendering code is modified code from the excellent svglite
package.
This project is licensed GPL v2.0.
It includes parts of svglite
(GPL ≥ 2), Belle
(MIT) and fmt
(MIT). The HTML client includes Material Design icons by Google which are licensed under the Apache License Version 2.0.
Full copies of the license agreements used by these components are included in ./inst/licenses
.