### Install libpng on macOS Source: https://svglite.r-lib.org/index.html Provides the command to install the libpng system dependency on macOS using Homebrew. This is a prerequisite for building svglite from source. ```bash brew install libpng ``` -------------------------------- ### Basic SVG Plotting Example Source: https://svglite.r-lib.org/reference/svglite.html Demonstrates how to save a simple plot to an SVG file using svglite and close the device. ```R # Save to file svglite(tempfile("Rplots.svg")) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off() #> agg_record_1000261060 #> 2 ``` -------------------------------- ### Install svglite Development Version Source: https://svglite.r-lib.org/index.html Installs the development version of the svglite package from GitHub using the 'pak' package manager. This is useful for users who want to access the latest features or contribute to the project. ```R # install.packages("pak") pak::pak("r-lib/svglite") ``` -------------------------------- ### Generate @font-face block with local fallbacks Source: https://svglite.r-lib.org/reference/font_face.html Example of using `font_face()` to create an @font-face block. It specifies the font family, a TTF source, and local font names for fallback. ```R font_face( family = "MyHelvetica", ttf = "MgOpenModernaBold.ttf", local = c("Helvetica Neue Bold", "HelveticaNeue-Bold"), weight = "bold" ) #> @font-face { #> font-family: "MyHelvetica"; #> src: local("Helvetica Neue Bold"), #> local("HelveticaNeue-Bold"), #> url("") format("woff2"), #> url("") format("woff"), #> url("") format("opentype"), #> url("MgOpenModernaBold.ttf") format("truetype"); #> font-weight: bold; #> } ``` -------------------------------- ### Generate Compressed SVGZ Output with svglite Source: https://svglite.r-lib.org/index.html Demonstrates how to create a compressed SVG file (.svgz) directly using svglite by providing a file path with the '.svgz' extension. This results in even smaller file sizes. ```R tmp3 <- tempfile(fileext = ".svgz") svglite(tmp3) plot(x, y) invisible(dev.off()) # svglite - svgz fs::file_size(tmp3) #> 9.45K ``` -------------------------------- ### Configure Makevars for libpng on macOS (arm64) Source: https://svglite.r-lib.org/index.html Shows how to configure the Makevars file on macOS (specifically for arm64 architecture) to point to the Homebrew-installed libpng include and library folders. This is necessary for building svglite from source. ```make CPPFLAGS += -I/opt/homebrew/include LDFLAGS += -L/opt/homebrew/lib ``` -------------------------------- ### Create an SVG String Device Source: https://svglite.r-lib.org/reference/svgstring.html Use `svgstring()` to create a device that captures SVG output as a string. Call the returned function to retrieve the SVG content. ```R svgstring( width = 10, height = 8, bg = "white", pointsize = 12, standalone = TRUE, web_fonts = list(), id = NULL, fix_text_size = TRUE, scaling = 1, system_fonts = list(), user_fonts = list() ) ``` -------------------------------- ### Initialize and Capture SVG Output Source: https://svglite.r-lib.org/reference/svgstring.html Initializes an svgstring device and captures the output of subsequent plotting commands. Call the object created by svgstring() to retrieve the SVG string. ```r s <- svgstring() s() #> plot.new() s() #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> ``` -------------------------------- ### Benchmark svglite vs. svg Speed Source: https://svglite.r-lib.org/index.html Compares the rendering speed of svglite() against the base R svg() function using the 'bench' package. Demonstrates that svglite() is considerably faster for dynamic SVG generation. ```R library(svglite) x <- runif(1e3) y <- runif(1e3) tmp1 <- tempfile() tmp2 <- tempfile() svglite_test <- function() { svglite(tmp1) plot(x, y) dev.off() } svg_test <- function() { svg(tmp2, onefile = TRUE) plot(x, y) dev.off() } bench::mark(svglite_test(), svg_test(), min_iterations = 250, check = FALSE) #> # A tibble: 2 × 6 #> expression min median `itr/sec` mem_alloc `gc/sec` #> #> 1 svglite_test() 2.86ms 2.97ms 325. 709KB 5.28 #> 2 svg_test() 6.11ms 6.26ms 159. 224KB 0.638 ``` -------------------------------- ### svglite() Source: https://svglite.r-lib.org/reference/index.html The main SVG graphics driver for creating SVG files. ```APIDOC ## svglite() ### Description An SVG Graphics Driver ### Usage svglite() ### Details This is the primary function used to initiate and manage the creation of SVG graphics. It acts as a graphics device that directs plotting output to an SVG file. ### See Also `svgstring()` ``` -------------------------------- ### Compare svglite and svg File Sizes Source: https://svglite.r-lib.org/index.html Compares the file sizes of SVGs generated by svglite() and the base R svg() function. Shows that svglite() produces significantly smaller files. ```R # svglite fs::file_size(tmp1) #> 75K # svg fs::file_size(tmp2) #> 327K ``` -------------------------------- ### Capture SVG with Text Output Source: https://svglite.r-lib.org/reference/svgstring.html Demonstrates capturing SVG output that includes text elements. The text is rendered using standard SVG text elements. ```r text(0.5, 0.5, "Hi!") s() #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> Hi! #> #> ``` -------------------------------- ### svgstring Function Source: https://svglite.r-lib.org/reference/svgstring.html This function creates an SVG device that captures the output as a string. Calling the returned function provides access to the SVG content. ```APIDOC ## svgstring() ### Description This is a variation on `svglite` that makes it easy to access the current value as a string. ### Usage ```R svgstring( width = 10, height = 8, bg = "white", pointsize = 12, standalone = TRUE, web_fonts = list(), id = NULL, fix_text_size = TRUE, scaling = 1, system_fonts = list(), user_fonts = list() ) ``` ### Arguments * `width` (numeric) - Height and width in inches. * `height` (numeric) - Height and width in inches. * `bg` (character) - Default background color for the plot (defaults to "white"). * `pointsize` (numeric) - Default point size. * `standalone` (logical) - Produce a standalone svg file? If `FALSE`, omits xml header and default namespace. * `web_fonts` (list) - A list containing web fonts to use in the SVG. The fonts will still need to be available locally on the computer running the code, but viewers of the final SVG will not need the font if specified as a web font. Web fonts can either be specified using `font_face()` or given as a single string in which case they are taken to be URL's for an `@import` directive to e.g. Google Fonts. For the latter, you can use `fonts_as_import()` to automatically generate the string, optionally embedding the font data in it. If the passed in string is not in the form of a URL or `@import` statement then it is considered a font family name and `fonts_as_import()` will be called to convert it to an import automatically, using the default arguments. * `id` (character vector) - A character vector of ids to assign to the generated SVG's. If creating more SVG files than supplied ids the exceeding SVG's will not have an id tag and a warning will be thrown. * `fix_text_size` (logical) - Should the width of strings be fixed so that it doesn't change between svg renderers depending on their font rendering? Defaults to `TRUE`. If `TRUE` each string will have the `textLength` CSS property set to the width calculated by systemfonts and `lengthAdjust='spacingAndGlyphs'`. Setting this to `FALSE` can be beneficial for heavy post-processing that may change content or style of strings, but may lead to inconsistencies between strings and graphic elements that depend on the dimensions of the string (e.g. label borders and background). * `scaling` (numeric) - A scaling factor to apply to the rendered line width and text size. Useful for getting the right sizing at the dimension that you need. * `system_fonts` (list) - _Consider using`systemfonts::register_font()` instead_. Named list of font names to be aliased with fonts installed on your system. If unspecified, the R default families `sans`, `serif`, `mono` and `symbol` are aliased to the family returned by `font_info()`. * `user_fonts` (list) - _Consider using`systemfonts::register_font()` instead_. Named list of fonts to be aliased with font files provided by the user rather than fonts properly installed on the system. The aliases can be fonts from the fontquiver package, strings containing a path to a font file, or a list containing `name` and `file` elements with `name` indicating the font alias in the SVG output and `file` the path to a font file. ### Value A function with no arguments: call the function to get the current value of the string. ### Details See `svglite()` documentation for information about specifying fonts. ``` -------------------------------- ### Run plotting code and return SVG as string Source: https://svglite.r-lib.org/reference/stringSVG.html This function is useful primarily for testing but can be used as an alternative to svgstring(). It takes plotting code as input and returns the SVG output as a string. ```R stringSVG(code, ...) ``` -------------------------------- ### create_svgz Source: https://svglite.r-lib.org/reference/create_svgz.html Converts an SVG file to SVGZ format, overwriting the original file. ```APIDOC ## create_svgz ### Description Convert an svg file to svgz, overwriting the old file ### Usage ```R create_svgz(file) ``` ### Arguments * **file** (character) - The path to the file to convert. ``` -------------------------------- ### font_face() Source: https://svglite.r-lib.org/reference/index.html Creates a font-face specification for use in SVG. ```APIDOC ## font_face() ### Description Create a font-face specification ### Usage font_face() ### Details This function generates the necessary SVG font-face elements, which define custom fonts for the SVG output. ### See Also `add_web_fonts()` ``` -------------------------------- ### font_face() function signature Source: https://svglite.r-lib.org/reference/font_face.html This is the function signature for `font_face()`. It outlines the available arguments for specifying font properties and sources. ```R font_face( family, woff2 = NULL, woff = NULL, ttf = NULL, otf = NULL, eot = deprecated(), svg = deprecated(), local = NULL, weight = NULL, style = NULL, range = NULL, variant = NULL, stretch = NULL, feature_setting = NULL, variation_setting = NULL, embed = FALSE ) ``` -------------------------------- ### Run plotting code and return SVG Source: https://svglite.r-lib.org/reference/xmlSVG.html Use xmlSVG to execute R plotting code and capture the output as an xml2 XML document. This is primarily for testing purposes and requires the xml2 package. Set standalone to FALSE to omit the XML header and default namespace. ```R if (require("xml2")) { x <- xmlSVG(plot(1, axes = FALSE)) x xml_find_all(x, ".//text") } ``` -------------------------------- ### htmlSVG Source: https://svglite.r-lib.org/reference/htmlSVG.html Runs plotting code and returns an SVG object that can be displayed in HTML. ```APIDOC ## htmlSVG ### Description Run plotting code and view svg in RStudio Viewer or web browser. ### Usage ```R htmlSVG(code, ...) ``` ### Arguments * `code` (expression) - Plotting code to execute. * `...` - Other arguments passed on to `svgstring`. ### Examples ```R if (interactive() && require("htmltools")) { htmlSVG(plot(1:10)) htmlSVG(hist(rnorm(100))) } ``` ``` -------------------------------- ### svglite Function Signature Source: https://svglite.r-lib.org/reference/svglite.html This is the main function signature for svglite, detailing all available arguments for creating SVG graphics. ```R svglite( filename = "Rplot%03d.svg", width = 10, height = 8, bg = "white", pointsize = 12, standalone = TRUE, web_fonts = list(), id = NULL, fix_text_size = TRUE, scaling = 1, always_valid = FALSE, file, system_fonts = list(), user_fonts = list() ) ``` -------------------------------- ### Basic SVG Structure Source: https://svglite.r-lib.org/reference/stringSVG.html This snippet shows the fundamental structure of an SVG document, including the root SVG tag and a group element. ```svg 10 Index 1:10 ``` -------------------------------- ### svglite Function Source: https://svglite.r-lib.org/reference/svglite.html The `svglite` function creates an SVG graphics device. It allows customization of filename, dimensions, background color, point size, and other SVG-specific features like web fonts and text scaling. ```APIDOC ## svglite() ### Description Produces graphics compliant to the current W3C SVG XML standard. ### Usage ```R svglite( filename = "Rplot%03d.svg", width = 10, height = 8, bg = "white", pointsize = 12, standalone = TRUE, web_fonts = list(), id = NULL, fix_text_size = TRUE, scaling = 1, always_valid = FALSE, file, system_fonts = list(), user_fonts = list() ) ``` ### Arguments * `filename` (character): The file where output will appear. * `width` (numeric): Width in inches. * `height` (numeric): Height in inches. * `bg` (character): Default background color for the plot (defaults to "white"). * `pointsize` (numeric): Default point size. * `standalone` (logical): Produce a standalone SVG file? If `FALSE`, omits xml header and default namespace. * `web_fonts` (list): A list containing web fonts to use in the SVG. Can be specified using `font_face()` or as a single string (URLs for `@import` directive). * `id` (character vector): A character vector of ids to assign to the generated SVG's. * `fix_text_size` (logical): Should the width of strings be fixed to prevent inconsistencies between SVG renderers? Defaults to `TRUE`. * `scaling` (numeric): A scaling factor to apply to the rendered line width and text size. * `always_valid` (logical): Should the svgfile be a valid SVG file while it is being written to? Setting this to `TRUE` incurs a considerable performance hit. * `file` (character): Identical to `filename`. Provided for backward compatibility. * `system_fonts` (list): Named list of font names to be aliased with fonts installed on your system. * `user_fonts` (list): Named list of fonts to be aliased with font files provided by the user. ### Details `svglite` provides two ways of controlling fonts: system fonts aliases and user fonts aliases. Supplying a font alias has two effects. First it determines the `font-family` property of all text anchors in the SVG output. Secondly, the font is used to determine the dimensions of graphical elements and has thus an influence on the overall aspect of the plots. ### References W3C Scalable Vector Graphics (SVG): https://www.w3.org/groups/wg/svg/ ### See also `pictex`, `postscript`, `Devices` ### Author This driver was written by T Jake Luciani jakeluciani@yahoo.com 2012: updated by Matthieu Decorde matthieu.decorde@ens-lyon.fr ### Examples ```R # Save to file svglite(tempfile("Rplots.svg")) plot(1:11, (-5:5)^2, type = "b", main = "Simple Example") dev.off() ``` -------------------------------- ### Add Web Fonts to SVG Source: https://svglite.r-lib.org/reference/add_web_fonts.html Use this function to add web font imports to an SVG file after it has been created. The result is identical to using the `web_fonts` argument during SVG creation. ```R add_web_fonts(filename, web_fonts) ``` -------------------------------- ### add_web_fonts Source: https://svglite.r-lib.org/reference/add_web_fonts.html This function allows you to add web fonts after an SVG has been created. The result is the same as using the `web_fonts` argument in `svglite()`. Only SVGs created with svglite can have web fonts added. ```APIDOC ## add_web_fonts ### Description This function allows you to add web fonts after creation. The result is the same as using the `web_fonts` argument in `svglite()`. Only SVGs created with svglite can get web fonts added. ### Usage ```R add_web_fonts(filename, web_fonts) ``` ### Arguments * **filename** (character or svg object) - The svgfile(s) or `svg` object(s) (as created by `svgstring()`) to edit. * **web_fonts** (list or character) - A list containing web fonts to use in the SVG. The fonts will still need to be available locally on the computer running the code, but viewers of the final SVG will not need the font if specified as a web font. Web fonts can either be specified using `font_face()` or given as a single string in which case they are taken to be URL's for an `@import` directive to e.g. Google Fonts. For the latter, you can use `fonts_as_import()` to automatically generate the string, optionally embedding the font data in it. If the passed in string is not in the form of a URL or `@import` statement then it is considered a font family name and `fonts_as_import()` will be called to convert it to an import automatically, using the default arguments. ### Value Invisibly returns `filename`. If any of elements of this were inline SVGs then these have been modified to include the imports. ``` -------------------------------- ### Convert SVG to SVGZ Source: https://svglite.r-lib.org/reference/create_svgz.html Use this function to compress an SVG file into an SVGZ file, overwriting the original. Ensure the file path is correct. ```R create_svgz(file) ``` -------------------------------- ### stringSVG Source: https://svglite.r-lib.org/reference/stringSVG.html This function is useful primarily for testing but can be used as an alternative to svgstring(). It runs plotting code and returns the SVG as a string. ```APIDOC ## stringSVG ### Description Run plotting code and return svg as string. ### Usage ```R stringSVG(code, ...) ``` ### Arguments * `code` - Plotting code to execute. * `...` - Other arguments passed on to `svgstring`. ``` -------------------------------- ### Run plotting code and open SVG in viewer Source: https://svglite.r-lib.org/reference/editSVG.html Use this function to execute R plotting code and have the resulting SVG automatically opened in your system's default SVG viewer or editor. This is primarily useful for testing or for immediate post-processing of the SVG output. ```R editSVG(code, ..., width = NA, height = NA) ``` ```R if (interactive()) { editSVG(plot(1:10)) editSVG(contour(volcano)) } ``` -------------------------------- ### svgstring() Source: https://svglite.r-lib.org/reference/index.html Access the current SVG output as a string. ```APIDOC ## svgstring() ### Description Access current SVG as a string. ### Usage svgstring() ### Details This function allows you to retrieve the generated SVG content as a character string, which can be useful for embedding SVG directly into HTML or for further manipulation. ### See Also `svglite()` ``` -------------------------------- ### add_web_fonts() Source: https://svglite.r-lib.org/reference/index.html Adds web font imports to an existing SVG file. ```APIDOC ## add_web_fonts() ### Description Add web font imports to an already created SVG file ### Usage add_web_fonts() ### Details This function is used to include necessary font definitions for web display within an SVG. ### See Also `font_face()` ``` -------------------------------- ### htmlSVG Function Usage Source: https://svglite.r-lib.org/reference/htmlSVG.html Demonstrates how to use the htmlSVG function to render R plots as SVGs. This function takes R plotting code as its first argument and passes additional arguments to svgstring. It's useful for interactive testing. ```r if (interactive() && require("htmltools")) { htmlSVG(plot(1:10)) htmlSVG(hist(rnorm(100))) } ``` -------------------------------- ### xmlSVG Source: https://svglite.r-lib.org/reference/xmlSVG.html This function runs plotting code and returns an SVG object, primarily for testing purposes. It requires the xml2 package. ```APIDOC ## xmlSVG ### Description Run plotting code and return svg. This is useful primarily for testing. Requires the `xml2` package. ### Usage ```R xmlSVG(code, ..., standalone = FALSE, height = 7, width = 7) ``` ### Arguments * `code` (any) - Plotting code to execute. * `...` (any) - Other arguments passed on to `svgstring`. * `standalone` (logical) - Produce a standalone svg file? If `FALSE`, omits xml header and default namespace. Defaults to `FALSE`. * `height` (numeric) - Height in inches. Defaults to 7. * `width` (numeric) - Width in inches. Defaults to 7. ### Value A `xml2::xml_document` object. ### Examples ```R if (require("xml2")) { x <- xmlSVG(plot(1, axes = FALSE)) x xml_find_all(x, ".//text") } ``` ``` -------------------------------- ### font_face Function Source: https://svglite.r-lib.org/reference/font_face.html Creates a CSS @font-face block for embedding or referencing webfonts. This block can be passed to the `web_fonts` argument of `svglite()` and `svgstring()`. ```APIDOC ## font_face() ### Description Helps you create a valid `@font-face` block for the `web_fonts` argument in `svglite()` and `svgstring()` functions. ### Usage ```R font_face( family, woff2 = NULL, woff = NULL, ttf = NULL, otf = NULL, eot = deprecated(), svg = deprecated(), local = NULL, weight = NULL, style = NULL, range = NULL, variant = NULL, stretch = NULL, feature_setting = NULL, variation_setting = NULL, embed = FALSE ) ``` ### Arguments * **family** (character) - The font family name this font should respond to. * **woff2, woff, ttf, otf** (character) - URLs to the font in different formats. At least one must be given. Best browser support is provided by the woff format. * **eot, svg** (deprecated) - Deprecated arguments. * **local** (character) - One or more font names that local installations of the font may have. If a local font is found with either of the given names it will be used and no download will happen. * **weight** (character) - An optional value for the `font-weight` descriptor. * **style** (character) - An optional value for the `font-style` descriptor. * **range** (character) - An optional value for the `unicode-range` descriptor. Will give the range of unicode values that this font will support. * **variant** (character) - An optional value for the `font-variant` descriptor. * **stretch** (character) - An optional value for the `font-stretch` descriptor. * **feature_setting** (character) - An optional value for the `font-feature-settings` descriptor. It is recommended to avoid using this if possible. * **variation_setting** (character) - An optional value for the `font-variation-settings` descriptor. * **embed** (logical) - Should the font data be embedded directly in the SVG. ### Value A character string with the `@font-face` block. ### Example ```R font_face( family = "MyHelvetica", ttf = "MgOpenModernaBold.ttf", local = c("Helvetica Neue Bold", "HelveticaNeue-Bold"), weight = "bold" ) #> @font-face { #> font-family: "MyHelvetica"; #> src: local("Helvetica Neue Bold"), #> local("HelveticaNeue-Bold"), #> url("") format("woff2"), #> url("") format("woff"), #> url("") format("opentype"), #> url("MgOpenModernaBold.ttf") format("truetype"); #> font-weight: bold; #> } ``` ``` -------------------------------- ### editSVG Function Source: https://svglite.r-lib.org/reference/editSVG.html Runs plotting code and opens the generated SVG in the default system viewer or editor. This is primarily useful for testing or post-processing the SVG. ```APIDOC ## Function Signature editSVG(code, ..., width = NA, height = NA) ### Description Run plotting code and open svg in OS/system default svg viewer or editor. This is useful primarily for testing or post-processing the SVG. ### Arguments * `code` (Plotting code to execute) * `...` (Other arguments passed on to `svgstring`) * `width` (Height in inches) * `height` (Width in inches) ### Examples ```R if (interactive()) { editSVG(plot(1:10)) editSVG(contour(volcano)) } ``` ``` -------------------------------- ### Embed SVG with Img Tag for Natural Scaling Source: https://svglite.r-lib.org/articles/scaling.html Specify dimensions directly in the tag when embedding the SVG. Dimensions must be in pixels, calculated by multiplying point dimensions by 16/12. This ensures the SVG appears seamless with web text. ```html ``` -------------------------------- ### Capture SVG with Plot Output Source: https://svglite.r-lib.org/reference/svgstring.html Captures the SVG output of a standard R plot, such as a scatter plot generated by plot(rnorm(5), rnorm(5)). The output includes elements for points and axes. ```r s <- svgstring() plot(rnorm(5), rnorm(5)) s() #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> #> -1.5 ``` -------------------------------- ### Embed SVG with Div for Natural Scaling Source: https://svglite.r-lib.org/articles/scaling.html Enclose the SVG figure within a div tag with specified width and height in points to allow the SVG to rescale itself to the container. This method is useful for maintaining consistent text sizes. ```html
``` -------------------------------- ### Generate SVG String from Plot Source: https://svglite.r-lib.org/reference/stringSVG.html Use stringSVG to capture the SVG output of a plot as a string. This function is useful for programmatic SVG generation and manipulation. ```R stringSVG(plot(1:10)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.