xmlSource <- # # We could also do things this way # source(textConnection(saveXML(xsltApplyStyleSheet("cityTemperatures.xml", "~/Projects/org/omegahat/XML/Literate/segment.xsl")))) # # function(url, ..., envir =globalenv(), handlers = list(code = code, example = code, "function" = code, startElement = function(x) NULL), ids = character(), omit = character(), ask = FALSE, fatal = TRUE, verbose = FALSE) { library(XML) evalNode = function(node) { #XXX check all ancestors. Ideally exclude them in the XPath query if(xmlName(xmlParent(node)) == "ignore") return(FALSE) tmp = xmlGetAttr(node, "id", NA) if(is.na(tmp) && length(ids) > 0 && !("" %in% ids)) return() if(!is.na(tmp)) { if(length(omit) > 0 && tmp %in% omit) { if(verbose) warning("skipping id ", tmp) return() } else if(length(ids) > 0 && !(tmp %in% ids)) { if(verbose) warning("ignoring id ", tmp) return() } } tmp = xmlGetAttr(node, "ignore", NA, converter = as.logical) if(!is.na(tmp) && tmp) return() txt = xmlValue(node) if(verbose) cat("*************\nEvaluating node\n", txt, "\n") cmd = parse(text = txt) eval(cmd, globalenv()) } doc = xmlTreeParse(url, ..., useInternal = TRUE) # on.exit(free(doc)) if(ask) { v = getNodeSet(doc, "//r:function|//r:init|//r:code", c("r", "r" = "http://www.r-project.org")) funs = sapply(v, xmlName) == "function" if(any(funs)) { } } v = getNodeSet(doc, "//r:function", c("r", "r" = "http://www.r-project.org")) ans = sapply(v, evalNode) v = getNodeSet(doc, "//r:init[not(@eval='false')]|//r:code[not(@eval='false')]", c("r", "r" = "http://www.r-project.org")) ans = sapply(v, evalNode) invisible(ans) }