By default hgd()
listens on 127.0.0.1, so only the machine running R can
reach it. It picks a free port and requires a random token. This article
shows how to view the plots from another machine: over SSH, through a
reverse proxy, or through the port forwarding of an editor.
The code below starts servers and is not run when this article is built.
host is the address the server listens on. The default
"127.0.0.1" only accepts connections from the same machine.
"0.0.0.0" listens on all network interfaces.port defaults to 0, which picks a free
port. Set a fixed port when it has to match an SSH or proxy
configuration.token = TRUE (the default) generates a random
16-character token. A string sets a fixed token, a number generates a
random token of that length, and FALSE turns the token
off.The defaults can also be set as options, for example in
.Rprofile:
hgd() prints the viewer URL, which includes the token.
With host = "0.0.0.0" it prints one URL with
127.0.0.1 and one with the machine name.
hgd_url() returns the URL again, and
hgd_details() returns host, port and token:
The viewer page, the API and the WebSocket all require the token,
either as token query parameter or as
X-HTTPGD-TOKEN header. Only the static files of the viewer
(scripts, styles, icons) are served without it.
When the browser reaches the server under another address,
host and port of hgd_url()
replace the ones in the URL:
httpgd is a development tool, and its network security has not been thoroughly tested.
host = "127.0.0.1" where you can, and
connect through SSH or a proxy as described below. Listen on
"0.0.0.0" only in trusted networks.127.0.0.1; the token is what keeps them
out.cors = FALSE): the server sends
no CORS headers, so web pages from other origins cannot read its
responses. hgd(cors = TRUE) sends
Access-Control-Allow-Origin: *. Enable it only if a page
from another origin needs the API (see The hgd URL parameter);
the token is still required.This is the usual setup when R runs on a remote server you log into with SSH. Forward a local port to the httpgd port on the server:
Then start httpgd on that port in R on the server:
Open the printed URL
(http://127.0.0.1:8888/live?token=...) in your local
browser. SSH encrypts the connection, and httpgd keeps listening on
127.0.0.1 only. If the local port is different
(ssh -L 9000:127.0.0.1:8888 user@server),
hgd_url(port = 9000) gives the local URL.
With the default port = 0 the port is only known after
the server has started. Print the matching command and run it in a
second terminal; -N forwards the port without opening a
shell:
To forward the port on every login, add it to
~/.ssh/config:
Host myserver
HostName server.example.com
LocalForward 8888 127.0.0.1:8888
Editors with remote development, such as VS Code with Remote-SSH or Dev Containers, forward ports the same way (see their “Ports” view). For plots inside VS Code, see the VS Code article.
The viewer finds the server relative to the address it was loaded
from, including the path, so it works behind a reverse proxy that serves
httpgd under a path prefix. The proxy only has to forward requests to
the httpgd port. It may strip the prefix (a request for
https://example.com/httpgd/live reaches httpgd as
/live) or keep it (/httpgd/live); httpgd needs
no setting for either. Since httpgd routes a prefixed request by the
last segment of its path, path-based proxy rules cannot block individual
endpoints (/clear is also reachable as
/anything/clear); access control is the token, which every
endpoint except the static viewer files requires.
The WebSocket uses the same prefix
(wss://example.com/httpgd/). If the proxy forwards
WebSocket upgrades, the viewer gets updates over it; otherwise it falls
back to polling (see WebSockets and
polling).
If host is a base URL with scheme and path,
hgd_url() builds the link through the proxy and adds no
port:
hgd_url(host = "https://example.com/httpgd")
#> [1] "https://example.com/httpgd/live?token=kW2nT9T6G8WFxgXF"Keep httpgd on 127.0.0.1 (here with
hgd(port = 8888)) and let the proxy handle TLS.
With nginx, the Upgrade and Connection
headers forward WebSockets. The trailing / in
proxy_pass strips the /httpgd/ prefix; without
it the prefix is kept, which works as well:
location /httpgd/ {
proxy_pass http://127.0.0.1:8888/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
nginx closes WebSocket connections that see no traffic for
proxy_read_timeout (60 seconds by default). The viewer
reconnects on its own; raise the timeout to avoid it.
With Caddy, handle_path strips the prefix. Caddy obtains
TLS certificates and forwards WebSockets without further
configuration:
example.com {
handle_path /httpgd/* {
reverse_proxy 127.0.0.1:8888
}
}
In both cases the viewer is at
hgd_url(host = "https://example.com/httpgd"). The token is
the only access control of httpgd itself; the proxy can add its own
authentication in front of it.
Browser-based editors and notebook servers often include a proxy for
ports on the server. httpgd works behind them with or without path
prefix. For example, with hgd(port = 8888):
https://<code-server>/proxy/8888/live?token=... or
https://<code-server>/absproxy/8888/live?token=...,
which
hgd_url(host = "https://<code-server>/absproxy/8888")
returns.jupyter-server-proxy:
https://<hub>/user/<name>/proxy/8888/live?token=...
or
https://<hub>/user/<name>/proxy/absolute/8888/live?token=....In RStudio Server, hgd_view() opens the viewer in the
Viewer pane, which RStudio serves under a path prefix of its own (see
the RStudio article). This was tested
with RStudio Server 2026.11.0: the Viewer pane shows the plots and
receives updates over the WebSocket.
hgd URL parameterA viewer page served by httpgd, directly or through a proxy, always
reaches its server at the page address. The hgd query
parameter is for pages that are not served by httpgd, for example an
editor that loads the viewer from somewhere else. It names the server as
host:port, optionally followed by a path prefix, or as a
full URL with http://, https://,
ws:// or wss://:
/live?hgd=127.0.0.1:8888&token=...
/live?hgd=https://example.com/httpgd&token=...
Without a scheme, the scheme of the page is used. The viewer tries
the page address first and uses hgd only if no httpgd
server answers there. A hgd value that does not fit the
browser, such as 127.0.0.1 in a proxied viewer, is
therefore harmless.
hgd_url(explicit = TRUE) adds the parameter, and
hgd_view() always does:
hgd_url(explicit = TRUE)
#> [1] "http://127.0.0.1:8888/live?hgd=127.0.0.1%3A8888&token=kW2nT9T6G8WFxgXF"If the page that uses hgd comes from another origin than
the server, the browser only lets it read the API with
hgd(cors = TRUE).
The viewer receives updates over a WebSocket at the root of the
server address, or at the prefix behind a proxy. If the WebSocket fails
while plain HTTP still answers, for example behind a proxy that does not
forward WebSockets, the viewer switches to polling after three failed
attempts and tries the WebSocket again every 5 minutes. When the
WebSocket is known not to work, add ws=0 to poll from the
start:
websockets=0 works as well.