3.4 Plot Viewer
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. httpgd is required by the interactive plot viewer of the R extension for VS Code.
Enable r.plot.useHttpgd in VS Code settings.
r.plot.useHttpgd: use the httpgd-based plot viewer instead of the base VSCode-R plot viewer
This will allow you to view plots in a separate window or in the browser, which is useful for interactive visualizations.
The httpgd plot viewer supports
- auto-resizing,
- light/dark theme mode,
- plot history,
- hiding and zooming.
Getting started with httpgd
library(httpgd)
# Start the httpgd graphics device
hgd()
# Open the plot viewer in your browser
hgd_browse()
# Create a figure
x = seq(0, 3 * pi, by = 0.1)
plot(x, sin(x), type = "l")Q: Plot viewer is missing.
A: Run hgd() in R and get the url to the viewer. Use the command palette to run “R Plot: Open httpgd Url”. It will let you enter the url address, fill it in and hit Enter to open the plot viewer. Alternatively, if you want to view in external browser, you can copy the url and paste it in your browser.
hgdInitialize device and start server.hgd_browseOpen the plot viewer in your browser, can be internal or external.When you call
hgd_browse(), it will open the graphic device in a new tab in your default web browser. You wil see the path to the viewer in the R console.You may copy and paste the url to your external browser.
There is an url in the tab header to indicate it is a web browser. When you hover over the image, the menu bar will show up. You can choose to download, copy, clear all plots, etc.
hgd_close()will clear all plots.Note that this will NOT close the viewer automatically for you. You can close the viewer manually by clicking the “x” button on the top right corner of the viewer window.
By the next time you call
plot, you will see all historical plots are gone.Closing the plot viewer window does not clear the plots, nor clearing the plots closes the plot viewer window.
If you don’t call
hgd_close(), but click the “x” button on the top right corner of the viewer window, the viewer will be closed but all historical plots are still there. You can callplotagain to see the previous plots.Otherwise, if you click the “x” button directly, the viewer will be closed, but all historical plots are still there. You can call
plotagain to see the previous plots.Keyboard shortcut in the WebView
←/→/↑/↓: navigate through the plot history
+/-: zoom in/out0: reset zooms/p: save the current plot as SVG/PNGNot recommended to save as PNG as the resolution is low.
Use
ggsave("plot.png", dpi = 300)instead to save high-resolution plots.
Universal graphics device (unigd) is a package that provides a set of functions to manage the plot viewer, such as:
unigd::ugd_clear()Clear all pages in the plot viewer.Same effects as
hgd_close().unigd::ugd_remove(page = 2)Remove the second page.
Ref: