EVOLUTION-MANAGER
Edit File: xmlSource.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Source the R code, examples, etc. from an XML document</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for xmlSource {XML}"><tr><td>xmlSource {XML}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Source the R code, examples, etc. from an XML document</h2> <h3>Description</h3> <p>This is the equivalent of a smart <code><a href="../../base/html/source.html">source</a></code> for extracting the R code elements from an XML document and evaluating them. This allows for a “simple” way to collect R functions definitions or a sequence of (annotated) R code segments in an XML document along with other material such as notes, documentation, data, FAQ entries, etc., and still be able to access the R code directly from within an R session. The approach enables one to use the XML document as a container for a heterogeneous collection of related material, some of which is R code. In the literate programming parlance, this function essentially dynamically "tangles" the document within R, but can work on small subsets of it that are easily specified in the <code>xmlSource</code> function call. This is a convenient way to annotate code in a rich way and work with source files in a new and potentially more effective manner. </p> <p><code>xmlSourceFunctions</code> provides a convenient way to read only the function definitions, i.e. the <code><r:function></code> nodes. We can restrict to a subset by specifying the node ids of interest. </p> <p><code>xmlSourceSection</code> allows us to evaluate the code in one or more specific sections. </p> <p>This style of authoring code supports mixed language support in which we put, for example, C and R code together in the same document. Indeed, one can use the document to store arbitrary content and still retrieve the R code. The more structure there is, the easier it is to create tools to extract that information using XPath expressions. </p> <p>We can identify individual <code>r:code</code> nodes in the document to process, i.e. evaluate. We do this using their <code>id</code> attribute and specifying which to process via the <code>ids</code> argument. Alternatively, if a document has a node <code>r:codeIds</code> as a child of the top-level node (or within an invisible node), we read its contents as a sequence of line separated <code>id</code> values as if they had been specified via the argument <code>ids</code> to this function. </p> <p>We can also use XSL to extract the code. See <code>getCode.xsl</code> in the Omegahat XSL collection. </p> <p>This particular version (as opposed to other implementations) uses XPath to conveniently find the nodes of interest. </p> <h3>Usage</h3> <pre> xmlSource(url, ..., envir = globalenv(), xpath = character(), ids = character(), omit = character(), ask = FALSE, example = NA, fatal = TRUE, verbose = TRUE, echo = verbose, print = echo, xnodes = DefaultXMLSourceXPath, namespaces = DefaultXPathNamespaces, section = character(), eval = TRUE, init = TRUE, setNodeNames = FALSE, parse = TRUE, force = FALSE) xmlSourceFunctions(doc, ids = character(), parse = TRUE, ...) xmlSourceSection(doc, ids = character(), xnodes = c(".//r:function", ".//r:init[not(@eval='false')]", ".//r:code[not(@eval='false')]", ".//r:plot[not(@eval='false')]"), namespaces = DefaultXPathNamespaces, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>url</code></td> <td> <p>the name of the file, URL containing the XML document, or an XML string. This is passed to <code><a href="xmlTreeParse.html">xmlTreeParse</a></code> which is called with <code>useInternalNodes = TRUE</code>. </p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>additional arguments passed to <code><a href="xmlTreeParse.html">xmlTreeParse</a></code></p> </td></tr> <tr valign="top"><td><code>envir</code></td> <td> <p>the environment in which the code elements of the XML document are to be evaluated. By default, they are evaluated in the global environment so that assignments take place there. </p> </td></tr> <tr valign="top"><td><code>xpath</code></td> <td> <p>a string giving an XPath expression which is used after parsing the document to filter the document to a particular subset of nodes. This allows one to restrict the evaluation to a subset of the original document. One can do this directly by parsing the XML document, applying the XPath query and then passing the resulting node set to this <code>xmlSource</code> function's appropriate method. This argument merely allows for a more convenient form of those steps, collapsing it into one action. </p> </td></tr> <tr valign="top"><td><code>ids</code></td> <td> <p>a character vector. XML nodes containing R code (e.g. <code>r:code</code>, <code>r:init</code>, <code>r:function</code>, <code>r:plot</code>) can have an id attribute. This vector allows the caller to specify the subset of these nodes to process, i.e. whose code will be evaluated. The order is currently not important. It may be used in the future to specify the order in which the nodes are evaluated. </p> <p>If this is not specified and the document has a node <code>r:codeIds</code> as an immediate child of the top-most node, the contents of this node or contained within an <code>invisible</code> node (so that it doesn't have to be filtered when rendering the document), the names of the r:code id values to process are taken as the individual lines from the body of this node. </p> </td></tr> <tr valign="top"><td><code>omit</code></td> <td> <p>a character vector. The values of the id attributes of the nodes that we want to skip or omit from the evaluation. This allows us to specify the set that we don't want evaluated, in contrast to the <code>ids</code> argument. The order is not important. </p> </td></tr> <tr valign="top"><td><code>ask</code></td> <td> <p>logical</p> </td></tr> <tr valign="top"><td><code>example</code></td> <td> <p>a character or numeric vector specifying the values of the id attributes of any <code>r:example</code> nodes in the document. A single document may contain numerous, separate examples and these can be marked uniquely using an <code>id</code> attribute, e.g. <code><r:example id=''</code>. This argument allows the caller to specify which example (or examples) to run. If this is not specified by the caller and there are r:example nodes in the document, the user is prompted to select an example via a (text-based) menu. If a character vector is given by the caller, we use partial matching against the collection of <code>id</code> attributes of the r:example nodes to identify the examples of interest. Alternatively, one can specify the example(s) to run by number. </p> </td></tr> <tr valign="top"><td><code>fatal</code></td> <td> <p>(currently unused) a logical value. The idea is to control how we handle errors when evaluating individual code segments. We could recover from errors and continue processing subsequent nodes.</p> </td></tr> <tr valign="top"><td><code>verbose</code></td> <td> <p>a logical value. If <code>TRUE</code>, information about what code segments are being evaluated is displayed on the console. <code>echo</code> controls whether code is displayed, but this controls whether additional informatin is also displayed. See <code><a href="../../base/html/source.html">source</a></code>. </p> </td></tr> <tr valign="top"><td><code>xnodes</code></td> <td> <p>a character vector. This is a collection of xpath expressions given as individual strings which find the nodes whose contents we evaluate. </p> </td></tr> <tr valign="top"><td><code>echo</code></td> <td> <p>a logical value indicating whether to display the code before it is evaluated.</p> </td></tr> <tr valign="top"><td><code>namespaces</code></td> <td> <p>a named character vector (i.e. name = value pairs of strings) giving the prefix - URI pairings for the namespaces used in the XPath expressions. The URIs must match those in the document, but the prefixes are local to the XPath expression. The default provides mappings for the prefixes "r", "omg", "perl", "py", and so on. See <code>XML:::DefaultXPathNamespaces</code>. </p> </td></tr> <tr valign="top"><td><code>section</code></td> <td> <p>a vector of numbers or strings. This allows the caller to specify that the function should only look for R-related nodes within the specified section(s). This is useful for being able to easily process only the code in a particular subset of the document identified by a DocBook <code>section</code> node. A string value is used to match the <code>id</code> attribute of the <code>section</code> node. A number (assumed to be an integer) is used to index the set of <code>section</code> nodes. These amount to XPath expressions of the form <code>//section[number]</code> and <code>//section[@id = string]</code>. </p> </td></tr> <tr valign="top"><td><code>print</code></td> <td> <p>a logical value indicating whether to print the results</p> </td></tr> <tr valign="top"><td><code>eval</code></td> <td> <p>a logical value indicating whether to evaluate the code in the specified nodes or to just return the result of parsing the text in each node.</p> </td></tr> <tr valign="top"><td><code>init</code></td> <td> <p>a logical controlling whether to run the R code in any r:init nodes.</p> </td></tr> <tr valign="top"><td><code>doc</code></td> <td> <p>the XML document, either a file name, the content of the document or the parsed document.</p> </td></tr> <tr valign="top"><td><code>parse</code></td> <td> <p>a logical value that controls whether we parse the code or just return the text representation from the XML without parsing it. This allows us to get just the code.</p> </td></tr> <tr valign="top"><td><code>setNodeNames</code></td> <td> <p>a logical value that controls whether we compute the name for each node (or result) by finding is id or name attribute or enclosing task node. </p> </td></tr> <tr valign="top"><td><code>force</code></td> <td> <p>a logical value. If this is <code>TRUE</code>, the function will evaluate the code in a node even if it is explicitly marked as not to be evaluated with <code>eval = "false"</code>, either on the node itself or an ancestor.</p> </td></tr> </table> <h3>Details</h3> <p>This evaluates the <code>code</code>, <code>function</code> and <code>example</code> elements in the XML content that have the appropriate namespace (i.e. r, s, or no namespace) and discards all others. It also discards r:output nodes from the text, along with processing instructions and comments. And it resolves <code>r:frag</code> or <code>r:code</code> nodes with a <code>ref</code> attribute by identifying the corresponding <code>r:code</code> node with the same value for its <code>id</code> attribute and then evaluating that node in place of the <code>r:frag</code> reference. </p> <h3>Value</h3> <p>An R object (typically a list) that contains the results of evaluating the content of the different selected code segments in the XML document. We use <code><a href="../../base/html/sapply.html">sapply</a></code> to iterate over the nodes and so If the results of all the nodes A list giving the pairs of expressions and evaluated objects for each of the different XML elements processed. </p> <h3>Author(s)</h3> <p>Duncan Temple Lang <duncan@wald.ucdavis.edu></p> <h3>See Also</h3> <p><code><a href="xmlTreeParse.html">xmlTreeParse</a></code> </p> <h3>Examples</h3> <pre> xmlSource(system.file("exampleData", "Rsource.xml", package="XML")) # This illustrates using r:frag nodes. # The r:frag nodes are not processed directly, but only # if referenced in the contents/body of a r:code node f = system.file("exampleData", "Rref.xml", package="XML") xmlSource(f) </pre> <hr /><div style="text-align: center;">[Package <em>XML</em> version 3.99-0.3 <a href="00Index.html">Index</a>]</div> </body></html>