xmlSource(XML)
xmlSource()所属R语言包:XML
Source the R code, examples, etc. from an XML document
来源从XML文档中的R代码,示例等
译者:生物统计家园网 机器人LoveR
描述----------Description----------
This is the equivalent of a smart source 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 xmlSource 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.
这是相当于一个聪明的source提取的R代码从XML文档中的元素和评估。这使得一个“简单”的方法来收集在一个XML文档中的R函数的定义或序列(注明)R代码段以及与其他材料如票据,文件,数据,FAQ条目等,仍然能够访问R代码直接在R会话。这种方法使得使用XML文档作为一个异类收集相关的材料,其中有一些是R代码的容器。在文学编程的说法,这个功能基本上是动态的“缠结”的文件在R,但却可以工作,他们很容易xmlSource函数调用中指定的小的子集。这是一个方便的方法来注释代码了致富路,并与源文件在一个新的和潜在的更有效的方式。
xmlSourceFunctions provides a convenient way to read only the function definitions, i.e. the <r:function> nodes. We can restrict to a subset by specifying the node ids of interest.
xmlSourceFunctions提供了一个便捷的方式为只读的功能定义,即<r:function>节点。我们可以限制为一个子集,通过指定感兴趣的节点的ID。
xmlSourceSection allows us to evaluate the code in one or more specific sections.
xmlSourceSection使我们能够评估中的代码的一个或多个特定的部分。
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.
这种风格的创作代码中,我们提出,例如,C和R代码放在一起,在同一个文档支持混合语言支持。事实上,人们可以使用该文件到任意内容存储和检索的R代码。结构,这有利于创造工具来提取,使用XPath表达式的信息。
We can identify individual r:code nodes in the document to process, i.e. evaluate. We do this using their id attribute and specifying which to process via the ids argument. Alternatively, if a document has a node r:codeIds as a child of the top-level node (or within an invisible node), we read its contents as a sequence of line separated id values as if they had been specified via the argument ids to this function.
我们可识别个别r:code在要处理的文档的节点,即评价。我们使用他们的id属性和指定通过ids参数处理。另外,如果一个文档节点r:codeIds“”作为一个孩子的顶层节点(或在一个无形的节点),我们读它的内容作为一个序列的分离id值,如果他们有通过参数ids这个函数被指定。
We can also use XSL to extract the code. See getCode.xsl in the Omegahat XSL collection.
我们也可以使用XSL中提取的代码。见getCode.xsl在Omegahat XSL的收藏。
This particular version (as opposed to other implementations) uses XPath to conveniently find the nodes of interest.
这种特殊的版本(如相对的其他实施方式)使用XPath可以方便地找到感兴趣的节点。
用法----------Usage----------
xmlSource(url, ...,
envir = globalenv(),
xpath = character(),
ids = character(),
omit = character(),
ask = FALSE,
example = NA,
fatal = TRUE, verbose = FALSE, echo = verbose, print = echo,
xnodes = c("//r:function",
"//r:init[not(@eval='false')]",
"//r:code[not(@eval='false')]",
"//r:plot[not(@eval='false')]"),
namespaces = DefaultXPathNamespaces, section = character(),
eval = TRUE, init = TRUE, setNodeNames = FALSE, parse = TRUE)
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, ...)
参数----------Arguments----------
参数:url
the name of the file, URL containing the XML document, or an XML string. This is passed to xmlTreeParse which is called with useInternalNodes = TRUE.
的文件名,URL包含的XML文件,或一个XML字符串。这是传递给xmlTreeParse,被称为useInternalNodes = TRUE。
参数:...
additional arguments passed to xmlTreeParse
额外的参数传递给xmlTreeParse
参数:envir
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.
的XML文档的代码元素的环境,在该环境中进行评价。默认情况下,在全球环境中对它们进行评估,使作业在这里举行。
参数:xpath
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 xmlSource function's appropriate method. This argument merely allows for a more convenient form of those steps, collapsing it into one action.
一个字符串,给出的XPath表达式用于解析后的文档过滤文件到一个特定的节点的子集。这允许一个限制的评价的原始文档的一个子集。直接通过解析XML文档,应用XPath查询,然后将得到的节点设置为这个xmlSource功能的适当的方法,我们可以做到这一点。此参数只允许这些步骤更方便的形式,坍塌成一个动作。
参数:ids
a character vector. XML nodes containing R code (e.g. r:code, r:init, r:function, r:plot) 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. If this is not specified and the document has a node r:codeIds as an immediate child of the top-most node, the contents of this node or contained within an invisible 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.
字符向量。 XML节点包含R代码(如:r:code,r:init,r:function,r:plot)可以有一个id属性。该向量允许调用者指定处理这些节点的子集,即,它的代码将被评估。该订单是当前并不重要。它可用于在将来指定的顺序在该节点进行评估。如果未指定文件节点r:codeIds的直接子的最顶层节点,这个节点的内容或包含在一个invisible节点(因此,它并没有渲染时被过滤的文件),从身体的各个行这个节点的名字的R代码的id值过程。
参数:omit
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 ids argument. The order is not important.
字符向量。我们要跳到或省略从评价的节点的id属性的值。这使得我们可以指定一组,我们不希望进行评估,在ids参数。的顺序并不重要。
参数:ask
logical
逻辑
参数:example
a character or numeric vector specifying the values of the id attributes of any r:example nodes in the document. A single document may contain numerous, separate examples and these can be marked uniquely using an id attribute, e.g. <r:example id=''. 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 id attributes of the r:example nodes to identify the examples of interest. Alternatively, one can specify the example(s) to run by number.
指定任何r:example节点在文档中的id属性的值的一个字符或数值向量。一个文件可包含了大量的,独立的例子,这些都可以标记为唯一id使用属性,例如<r:example id=''。该参数允许调用者指定运行的例子(例子)。如果这不是由调用者和有r:例如在文档中的节点,系统会提示用户通过一个(基于文本)菜单中选择一个例子。如果一个特征向量的来电者,我们对id的r属性:例如节点的集合,以确定使用部分匹配的例子。另外,可以指定由数量例(S)来运行。
参数:fatal
(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.
(当前未使用)一个逻辑值。这样做是为了控制我们如何处理错误进行评估时,各代码段。我们可以从错误中恢复并继续处理后续的节点。
参数:verbose
a logical value. If TRUE, information about what code segments are being evaluated is displayed on the console. echo controls whether code is displayed, but this controls whether additional informatin is also displayed. See source.
一个逻辑值。如果TRUE,在控制台上显示的是什么样的代码段目前正在评估的信息。 echo控制是否显示代码,但这个控制,是否额外的informatin的也显示。见source。
参数:xnodes
a character vector. This is a collection of xpath expressions given as individual strings which find the nodes whose contents we evaluate.
字符向量。这是一个集合的XPath表达式作为单独的字符串,找到节点的内容,我们评估。
参数:echo
a logical value indicating whether to display the code before it is evaluated.
一逻辑值,该值指示是否显示代码之前进行评估。
参数:namespaces
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 XML::efaultXPathNamespaces.
指定的字符向量(即“名称=值”对的字符串)的前缀 - XPath表达式中使用的命名空间的URI配对。 URI必须符合这些文件中的,但前缀是本地的XPath表达式。默认提供映射的前缀“R”,“OMG”,“perl”的,“PY”,等等。见XML::efaultXPathNamespaces。
参数:section
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 section node. A string value is used to match the id attribute of the section node. A number (assumed to be an integer) is used to index the set of section nodes. These amount to XPath expressions of the form //section[number] and //section[@id = string].
一个矢量的数字或字符串。这允许调用者指定的功能应该只寻找在指定的部分(S)R-相关的节点。能够轻松地处理文件确定的DocBook section节点的特定子集中的代码,这是非常有用的。一个字符串值,用于匹配id属性section节点的。甲数(假设是一个整数)用于索引section节点集。这些量的形式//section[number]和//section[@id = string]XPath表达式。
参数:print
a logical value indicating whether to print the results
一逻辑值,指示是否打印结果
参数:eval
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.
一个逻辑值,该值指示是否评估中的代码在指定的节点或只返回结果的分析文本中的每个节点。
参数:init
a logical controlling whether to run the R code in any r:init nodes.
逻辑控制是否运行在任何r:初始化节点的R代码。
参数:doc
the XML document, either a file name, the content of the document or the parsed document.
XML文档中,可以是文件名,该文件的内容或解析后的文档。
参数:parse
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.
一逻辑值,控制是否我们解析的代码,或只返回的XML的文本表示不解析。这使我们能够得到公正的代码。
参数:setNodeNames
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.
一逻辑值,用于控制是否发现我们计算每个节点的名称(或结果)是id或name属性或封闭的任务节点。
Details
详细信息----------Details----------
This evaluates the code, function and example elements in the XML content that have the appropriate namespace (i.e. r, s, or no namespace) and discards all others. It also discards rutput nodes from the text, along with processing instructions and comments. And it resolves r:frag or r:code nodes with a ref attribute by identifying the corresponding r:code node with the same value for its id attribute and then evaluating that node in place of the r:frag reference.
评估code,function和example有适当的命名空间(即R,S,或没有命名空间),并丢弃所有其他元素中的XML内容。它也丢弃R:从文本的输出节点,以及处理指令和注释。它解决r:frag或r:code一个ref属性节点通过识别相应的r:code节点具有相同的值,它的id属性,然后评估该节点的地方r:frag参考。
值----------Value----------
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 sapply 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.
R的对象(通常为一个列表),包含在XML文档中的内容的所选择的不同的代码段的评价结果。我们使用的sapply迭代的节点上,所以,如果结果中的所有节点列表,对表情和评价对象的不同处理的XML元素。
(作者)----------Author(s)----------
Duncan Temple Lang <duncan@wald.ucdavis.edu>
参见----------See Also----------
xmlTreeParse
xmlTreeParse
实例----------Examples----------
xmlSource(system.file("exampleData", "Rsource.xml", package="XML"))
# This illustrates using r:frag nodes.[这说明使用r:FRAG节点。]
# The r:frag nodes are not processed directly, but only[R:碎片的节点不能直接处理,但只]
# if referenced in the contents/body of a r:code node[如果引用的内容/体AR:代码节点]
f = system.file("exampleData", "Rref.xml", package="XML")
xmlSource(f)
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|