R/dsvg_view.R

Defines functions dsvg_view

Documented in dsvg_view

#' Run plotting code and view svg in RStudio Viewer or web broswer.
#'
#' This is useful primarily for testing. Requires the `htmltools`
#' package.
#'
#' @param code Plotting code to execute.
#' @param ... Other arguments passed on to [dsvg()].
#' @export
#' @examples
#' \donttest{
#' dsvg_view(plot(1:10))
#' dsvg_view(hist(rnorm(100)))
#' }
#' @importFrom htmltools browsable HTML
dsvg_view <- function(code, ...) {
  path <- tempfile()
  devlength <- length(dev.list())
  dsvg(path, ...)
  tryCatch({
    code
  }, finally = {
    if (length(dev.list()) > devlength) {
      dev.off()
    }
  })
  if( interactive() ){
    doc <- read_file(path)
    browsable(HTML(as.character(doc)) )
  }
  else invisible()
}

Try the ggiraph package in your browser

Any scripts or data that you put into this service are public.

ggiraph documentation built on March 31, 2023, 9:53 p.m.