The plothistory() function initiates saving all plots generated during the session in phdir. It works as follows:

  • It first starts a web server graphics device httpgd::hgd().

  • Next it creates and runs a websocket connected to the graphics device.

  • Every time the user creates a plot, the web socket receives an event that triggers the saving of the plot into the user defined directory phdir.

plothistory(phdir = phist_tmp_dir(), start_hgd = TRUE, ext = "svg", ...)

Arguments

phdir

character(1) defining the plothistory directory. Default is to phist_tmp_dir(). If set to NULL, plot recording is stopped.

start_hgd

logical(1) that defines if a web server graphics device should be initialised with httpgd::hgd(). Default is TRUE. Set to FALSE to simply change phdir and keep using the same graphics device and websocket.

ext

character(1) defining the file extension/type to save the plots to, such as "pdf", "svg", "png", "tiff", ... Default is "svg". See unigd::ugd_renderers() to get a list of supported extensions.

...

additional parameters passed to httpgd::hgd().

Value

Invisibly returns phdir.

Examples


##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
## Note that this doesn't work in a non-interactive ##
## session. The example below doesn't make sense    ##
## on-line. Please run it locally.                  ##
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##


#################################################
## Start recording svg plots in a temp directory.

phdir <- plothistory()
#> Plot history set to
#>   /tmp/Rtmpt1DKh3
#> httpgd server running at:
#>   http://127.0.0.1:38507/live?token=Fsd1qiem
plot(rnorm(10000))
plot(1:10, col = "red")
dir(phdir, full.names = TRUE)
#> [1] "/tmp/Rtmpt1DKh3/downlit"          "/tmp/Rtmpt1DKh3/file180a140a19f1"

#########################################################
## Recording pdf plots in central cache, using same http
## graphics device (with start_hgd = FALSE).

phdir <- plothistory(phist_cache_dir(ask = FALSE),
                     ext = "pdf",
                     start_hgd = FALSE)
#> Plot history set to
#>   /home/runner/.cache/R/plothistory
dir(phdir)
#> character(0)
plot(rnorm(100), col = "blue")
plot(rnorm(100), col = "green", main = "plot")
dir(phdir)
#> character(0)

########################
## Stop recording plots.
length(dir(phdir))
#> [1] 0
plothistory(NULL)

plot(rnorm(100))
plot(rnorm(100))

## None added
length(dir(phdir))
#> [1] 0