Learn R Programming

⚠️There's a newer version (2.0.4) of this package.Take me there.

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

KeysResult
Navigate plot history.
NJump to the newest plot.
del / DDelete plot.
+ / -Zoom in and out.
0Reset zoom level.
SDownload plot as SVG.
PDownload plot as PNG.
CCopy plot to clipboard (as PNG).
TClear 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)

See full code

expressionminmedianitr/secmem_allocgc/secn_itrn_gctotal_time
httpgd_test()10.2ms10.8ms91.43165361KB0.737352024822.71s
svglite_test()20.3ms21.4ms46.29561593KB0.562294824735.33s
svg_test()27.2ms28.3ms35.15964126KB0.141203424917.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.

Copy Link

Version

Install

install.packages('httpgd')

Monthly Downloads

3,618

Version

1.0.1

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Florian Rupprecht

Last Published

January 21st, 2021

Functions in httpgd (1.0.1)

hgd_browse

Open httpgd device URL in the browser.
httpgd-package

httpgd: HTTP server graphics device
hgd_clear

Clear all httpgd plot pages.
hgd_state

httpgd device status.
hgd_generate_token

Generate random alphanumeric token.
hgd_remove

Remove a httpgd plot page.
hgd_close

Close httpgd device.
hgd_svg

Render httpgd plot to SVG.
hgd

Asynchronous HTTP server graphics device.
hgd_inline

Inline SVG rendering.
hgd_url

httpgd server URL.