######
## VT::17.02.2021
##
## roxygen2::roxygenise("C:/users/valen/onedrive/myrepo/R/indstat", load_code=roxygen2:::load_installed)
##
#' @name do_save
#' @title Save a graph to a file on disk inone of the following formats: pdf, cairo, wmf, svg
#'
#' @description Save a graph to a file on disk inone of the following formats:
#' pdf, cairo, wmf or svg.
#'
#' @details The input can be either an object generated by the function 'ggplot'
#' or a function which will generate the graph. In the latter case, argument
#' to the function to be called to generate the grpah can be passed.
#'
#' @param p an object generated by the function 'ggplot' or a function to generate the plot.
#' @param file file name where to store the output. The extension will be adjusted according to the
#' output format (pdf, svg or wmf)
#' @param type type of output - one of "pdf", "cairo", "svg", "wmf"
#' @param width of plot, defaults to=6 inch
#' @param height height of plot, defaults to 3.425 inch
#' @param colormodel color model, one of "cmyk", "srgb", "gray", "grey", defaults to "cmyk"
#' @param ... other parameters to be passed either to the function
#' passed as a parameter #1 or to other underlying functions.
#'
#' @return either the object passed as parameter #1 or the output of the function passed as parameter #1
#'
#' @examples
#'
#' mycmyk <- getcolors(n=5, colormodel="cmyk3")
#' do_save(showcolors, file="colormap", type="pdf", width=7, height=7, colormodel="cmyk", mycmyk)
#'
#' @export
#' @author Valentin Todorov, \email{v.todorov@@unido.org}
######
## Save a ggplot2 graph in WMF, PDF or SVG format
##
##
do_save <- function(p, file, type=c("pdf", "cairo", "svg", "wmf"), width=6, height=3.425,
colormodel=c("cmyk", "srgb", "gray", "grey"), ...) # size 152.5 x 87 mm
{
type <- match.arg(type)
colormodel <- match.arg(colormodel)
## stopifnot(colormodel %in% c( "cmyk", "srgb", "gray", "grey"))
ext <- if(type == "pdf" | type=="cairo") "pdf" else if(type=="svg") "svg" else "wmf"
file <- paste0(file_path_sans_ext(file), ".", ext)
if(type == "pdf")
pdf(file=file, width=width, height=height, colormodel=colormodel)
else if(type == "cairo")
cairo_pdf(filename=file, width=width, height=height)
else if(type == "svg")
svg(filename=file, width=width, height=height)
else
win.metafile(filename=file, width=width, height=height)
ret <- p
if(inherits(p, "function"))
{
ret <- p(...)
} else
print(p)
dev.off()
return(invisible(ret))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.