1 handler <- function() { 2 data <- NULL 3 4 # Private or local variables used to store information across 5 # method calls from the event parser 6 numRecords <- 0 7 varNames <- NULL 8 meta <- NULL 9 currentRecord <- 0 10 expectingVariableName <- F 11 rowNames <- NULL 12 13 # read the attributes from the dataset 14 dataset <- function(x,atts) { 15 numRecords <<- as.integer(atts[["numRecords"]]) 16 # store these so that we can put these as attributes 17 # on data when we create it. 18 meta <<- atts 19 } 20 21 variables <- function(x, atts) { 22 # From the DTD, we expect a count attribute telling us the number 23 # of variables. 24 data <<- matrix(0., numRecords, as.integer(atts[["count"]])) 25 # set the XML attributes from the dataset element as R 26 # attributes of the data. 27 attributes(data) <<- c(attributes(data),meta) 28 } 29 30 # when we see the start of a variable tag, then we are expecting 31 # its name next, so handle text accordingly. 32 variable <- function(x,...) { 33 expectingVariableName <<- T 34 } 35 36 record <- function(x,atts) { 37 # advance the current record index. 38 currentRecord <<- currentRecord + 1 39 rowNames <<- c(rowNames, atts[["id"]]) 40 } 41 42 text <- function(x,...) { 43 if(x == "") 44 return(NULL) 45 46 if(expectingVariableName) { 47 varNames <<- c(varNames, x) 48 if(length(varNames) >= ncol(data)) { 49 expectingVariableName <<- F 50 dimnames(data) <<- list(NULL, varNames) 51 } 52 } else { 53 e <- gsub("[ \t]*",",",x) 54 vals <- sapply(strsplit(e,",")[[1]], as.numeric) 55 data[currentRecord,] <<- vals 56 } 57 } 58 59 endElement <- function(x,...) { 60 if(x == "dataset") { 61 dimnames(data)[[1]] <<- rowNames 62 } 63 } 64 65 return(list(variable = variable, 66 variables = variables, 67 dataset=dataset, 68 text = text, 69 record= record, 70 endElement = endElement, 71 data = function() {data }, 72 rowNames = function() rowNames 73 )) 74 } 75