readPicture <- function(file, warn = TRUE) {
if (missing(file))
stop("'file' must be a character string representing a path to a file.")
doc <- xmlParse(file)
checkValidSVG(doc, warn = warn)
svgImage <- xmlRoot(doc)
pictureDims <- getPictureDims(svgImage)
# Need to create picture definitions table first
picdefs <- parsePictureDefinitions(svgImage)
# Stick defs in .grImport2Env so can be modded on-the-fly during parseImage
assign("defs", picdefs, envir=.grImport2Env)
# Now parse the contents of the image (<defs> are ignored).
# <use>s are resolved to "real" elements
pic <- parseImage(xmlChildren(svgImage, addNames = FALSE),
picdefs, createDefs = FALSE)
# Update defs for changes during parseImage
picdefs <- get("defs", picdefs, envir=.grImport2Env)
new("Picture",
content = pic,
defs = picdefs,
summary = new("PictureSummary",
xscale = c(0, pictureDims[1]),
yscale = c(pictureDims[2], 0)))
}
checkValidSVG <- function(doc, minVersion = NA, warn = TRUE) {
if (xmlName(xmlRoot(doc)) != "svg")
stop("This picture is not an SVG document.")
if (! warn)
return()
# Note: suppressing warnings because we know we just want comments
# and do not care about the namespace (SVG) that they belong in
grConvertComment <-
suppressWarnings(getNodeSet(doc, "//comment()"))
warningText <-
"This picture was not generated by the 'grConvert' package, errors may result"
if (! length(grConvertComment) ||
! grepl("Created by grConvert", xmlValue(grConvertComment[[1]])))
warning(warningText)
# Future proofing on grConvert input, add a min version check on
# the output that grConvert creates.
if (! is.na(minVersion)) {
grConvertComment <- xmlValue(grConvertComment[[1]])
# Collect the version number (of the form v0.1-0, for example)
fileVersion <-
gsub(".*v([0-9.-]+) $", "\\1", grConvertComment, perl = TRUE)
if (package_version(fileVersion) < package_version(minVersion))
warning(paste0("This picture was generated by an old version of 'grConvert'. ",
"The minimum supported version is ", minVersion,". ",
"Errors may result"))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.