This is a variation on svglite that makes it easy to access the current value as a string.

svgstring(
  width = 10,
  height = 8,
  bg = "white",
  pointsize = 12,
  standalone = TRUE,
  system_fonts = list(),
  user_fonts = list(),
  web_fonts = list(),
  id = NULL,
  fix_text_size = TRUE,
  scaling = 1
)

Arguments

width

Height and width in inches.

height

Height and width in inches.

bg

Default background color for the plot (defaults to "white").

pointsize

Default point size.

standalone

Produce a standalone svg file? If FALSE, omits xml header and default namespace.

system_fonts

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

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.

web_fonts

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.

id

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

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

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.

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.

Examples

s <- svgstring(); s()
#>
plot.new(); s();
#> <?xml version='1.0' encoding='UTF-8' ?> #> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'> #> <defs> #> <style type='text/css'><![CDATA[ #> .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle { #> fill: none; #> stroke: #000000; #> stroke-linecap: round; #> stroke-linejoin: round; #> stroke-miterlimit: 10.00; #> } #> ]]></style> #> </defs> #> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/> #> <defs> #> <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='> #> <rect x='0.00' y='0.00' width='720.00' height='576.00' /> #> </clipPath> #> </defs> #> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'> #> </g> #> </svg>
text(0.5, 0.5, "Hi!"); s()
#> <?xml version='1.0' encoding='UTF-8' ?> #> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'> #> <defs> #> <style type='text/css'><![CDATA[ #> .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle { #> fill: none; #> stroke: #000000; #> stroke-linecap: round; #> stroke-linejoin: round; #> stroke-miterlimit: 10.00; #> } #> ]]></style> #> </defs> #> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/> #> <defs> #> <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='> #> <rect x='0.00' y='0.00' width='720.00' height='576.00' /> #> </clipPath> #> </defs> #> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'> #> </g> #> <defs> #> <clipPath id='cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng=='> #> <rect x='59.04' y='59.04' width='630.72' height='443.52' /> #> </clipPath> #> </defs> #> <g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==)'> #> <text x='374.40' y='285.11' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='14.67px' lengthAdjust='spacingAndGlyphs'>Hi!</text> #> </g> #> </svg>
#> pdf #> 2
s <- svgstring() plot(rnorm(5), rnorm(5)) s()
#> <?xml version='1.0' encoding='UTF-8' ?> #> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'> #> <defs> #> <style type='text/css'><![CDATA[ #> .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle { #> fill: none; #> stroke: #000000; #> stroke-linecap: round; #> stroke-linejoin: round; #> stroke-miterlimit: 10.00; #> } #> ]]></style> #> </defs> #> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/> #> <defs> #> <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='> #> <rect x='0.00' y='0.00' width='720.00' height='576.00' /> #> </clipPath> #> </defs> #> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'> #> </g> #> <defs> #> <clipPath id='cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng=='> #> <rect x='59.04' y='59.04' width='630.72' height='443.52' /> #> </clipPath> #> </defs> #> <g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==)'> #> <circle cx='666.40' cy='486.13' r='2.70' style='stroke-width: 0.75;' /> #> <circle cx='496.53' cy='453.65' r='2.70' style='stroke-width: 0.75;' /> #> <circle cx='380.80' cy='448.27' r='2.70' style='stroke-width: 0.75;' /> #> <circle cx='492.22' cy='260.34' r='2.70' style='stroke-width: 0.75;' /> #> <circle cx='82.40' cy='75.47' r='2.70' style='stroke-width: 0.75;' /> #> </g> #> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'> #> <line x1='63.75' y1='502.56' x2='660.95' y2='502.56' style='stroke-width: 0.75;' /> #> <line x1='63.75' y1='502.56' x2='63.75' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='149.06' y1='502.56' x2='149.06' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='234.38' y1='502.56' x2='234.38' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='319.69' y1='502.56' x2='319.69' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='405.00' y1='502.56' x2='405.00' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='490.32' y1='502.56' x2='490.32' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='575.63' y1='502.56' x2='575.63' y2='509.76' style='stroke-width: 0.75;' /> #> <line x1='660.95' y1='502.56' x2='660.95' y2='509.76' style='stroke-width: 0.75;' /> #> <text x='63.75' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-1.8</text> #> <text x='149.06' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-1.6</text> #> <text x='234.38' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-1.4</text> #> <text x='319.69' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-1.2</text> #> <text x='405.00' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-1.0</text> #> <text x='490.32' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-0.8</text> #> <text x='575.63' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-0.6</text> #> <text x='660.95' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-0.4</text> #> <line x1='59.04' y1='439.25' x2='59.04' y2='70.17' style='stroke-width: 0.75;' /> #> <line x1='59.04' y1='439.25' x2='51.84' y2='439.25' style='stroke-width: 0.75;' /> #> <line x1='59.04' y1='316.22' x2='51.84' y2='316.22' style='stroke-width: 0.75;' /> #> <line x1='59.04' y1='193.19' x2='51.84' y2='193.19' style='stroke-width: 0.75;' /> #> <line x1='59.04' y1='70.17' x2='51.84' y2='70.17' style='stroke-width: 0.75;' /> #> <text transform='translate(41.76,439.25) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='20.68px' lengthAdjust='spacingAndGlyphs'>-0.5</text> #> <text transform='translate(41.76,316.22) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='16.68px' lengthAdjust='spacingAndGlyphs'>0.0</text> #> <text transform='translate(41.76,193.19) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='16.68px' lengthAdjust='spacingAndGlyphs'>0.5</text> #> <text transform='translate(41.76,70.17) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='16.68px' lengthAdjust='spacingAndGlyphs'>1.0</text> #> <polyline points='59.04,502.56 689.76,502.56 689.76,59.04 59.04,59.04 59.04,502.56 ' style='stroke-width: 0.75;' /> #> <text x='374.40' y='557.28' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='46.00px' lengthAdjust='spacingAndGlyphs'>rnorm(5)</text> #> <text transform='translate(12.96,280.80) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: Arial;' textLength='46.00px' lengthAdjust='spacingAndGlyphs'>rnorm(5)</text> #> </g> #> </svg>
#> pdf #> 2