[R] Objects must be passed as an argument or generated in the function, right?

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 24 18:34:17 CET 2011


On 24/02/2011 9:59 AM, Jk Zheng wrote:
> Hi,
>
> I'm new to R, and found this code confusing.  It is from the bioconductor
> package "RpsiXML".
>
>    entryCount<- length(nodes)
>
> The "nodes" object is either the argument nor generated in the function.
> How can R find the "nodes" object?  What am I missing here?

Functions in R are more properly called "closures".  They are made up of 
code *and an associated environment*.  Normally the environment is the 
one in which the function is created, e.g. the global environment for 
functions entered at the command line, the namespace of a package for 
functions there, or an evaluation frame for a function created within 
another one.

When evaluating a closure, R sets up an evaluation frame with the 
closure's environment as its parent, so any variables defined in the 
environment are also visible.  Presumably "nodes"  was defined in the 
namespace of RpsiXML, but it's possible that parseXmlEntryNodeSet has a 
different environment, depending on how it has been created.  (Or maybe 
it's a typo.)

Duncan Murdoch

> Thanks
>
> Zheng Jk
>
>
> parseXmlEntryNodeSet<- function(psimi25file, psimi25source, verbose=TRUE) {
>
>    psimi25Doc<- xmlTreeParse(psimi25file, useInternalNodes = TRUE)
>
>    psimi25NS<- getDefaultNamespace(psimi25Doc)
>    namespaces<- c(ns = psimi25NS)
>    entry<- getNodeSet(psimi25Doc, "/ns:entrySet/ns:entry", namespaces)
>
>    if(verbose)
>      statusDisplay(paste(length(entry),"Entries found\n",sep=" "))
>
>    entryCount<- length(nodes)
>    entryList<- list()
>    for(i in 1:entryCount) {
>      entryList[[i]]<- parseXmlEntryNode(doc=psimi25Doc, index=i,
>                                          namespaces=namespaces,
>                                          psimi25source=psimi25source,
>                                          verbose=verbose)
>    }
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list