1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#' @name SDMXCrossSectionalData
#' @rdname SDMXCrossSectionalData
#' @aliases SDMXCrossSectionalData,SDMXCrossSectionalData-method
#'
#' @usage
#' SDMXCrossSectionalData(xmlObj, namespaces)
#'
#' @param xmlObj object of class "XMLInternalDocument derived from XML package
#' @param namespaces object of class "data.frame" given the list of namespace URIs
#' @return an object of class "SDMXCrossSectionalData"
#'
#' @seealso \link{readSDMX}
#' @export
#'
SDMXCrossSectionalData <- function(xmlObj, namespaces){
new("SDMXCrossSectionalData",
SDMXData(xmlObj, namespaces)
)
}
#methods
#=======
#'@export
as.data.frame.SDMXCrossSectionalData <- function(x, row.names=NULL, optional=FALSE,
labels = FALSE, ...){
xmlObj <- x@xmlObj;
dataset <- NULL
schema <- slot(x,"schema")
sdmxVersion <- slot(schema,"version")
#namespace
hasAuthorityNS <- FALSE
nsDefs.df <- getNamespaces(x)
ns <- findNamespace(nsDefs.df, "crosssection")
authorityNs <- nsDefs.df[
attr(regexpr("http://www.sdmx.org", nsDefs.df$uri, ignore.case = TRUE),"match.length") == -1,]
authorityNs <- as.data.frame(authorityNs, stringsAsFactors = FALSE)
colnames(authorityNs) <- "uri"
if(nrow(authorityNs) > 0){
hasAuthorityNS <- TRUE
if(nrow(authorityNs) > 1){
warning("More than one target dataset namespace found!")
authorityNs <- authorityNs[1L,]
authorityNs <- as.data.frame(authorityNs, stringsAsFactors = FALSE)
colnames(authorityNs) <- "uri"
}
}
#parse Groups
if(hasAuthorityNS){
groupsXML <- getNodeSet(xmlObj, "//ns:Group", namespaces = c(ns = authorityNs$uri))
if(length(groupsXML) == 0){
groupsXML <- getNodeSet(xmlObj, "//ns:Group", namespaces = ns)
}
}else{
if(length(ns) > 0){
groupsXML <- getNodeSet(xmlObj, "//ns:Group", namespaces = ns)
}else{
if(nrow(nsDefs.df) > 0){
groupNs <- nsDefs.df[1,]
groupsXML <- getNodeSet(xmlObj, "//nt:Group", c(nt = groupNs$uri))
}else{
stop("Unsupported CrossSectionalData parser for empty target XML namespace")
}
}
}
if(length(groupsXML) == 0){
groupsXML <- getNodeSet(xmlObj, "//Group")
}
groupsNb <- length(groupsXML)
if(groupsNb == 0) return(NULL);
#function to parse a Section
parseSection <- function(x){
sectionAttrs <- xmlAttrs(x)
sectionAttrs <- as.data.frame(t(sectionAttrs), stringsAsFactors = FALSE)
sectionChildren <- xmlChildren(x)
secObs <- names(sectionChildren)
secValues <- do.call("rbind.fill", lapply(sectionChildren, function(x) {
attrs <- xmlAttrs(x)
return(as.data.frame(t(attrs), stringsAsFactors = FALSE))
}))
secContent <- data.frame(sectionAttrs,
obs = secObs,
secValues,
stringsAsFactors = FALSE)
return(secContent)
}
#function to parse a Group
parseGroup <- function(x){
groupAttrs <- xmlAttrs(x)
groupAttrs <- as.data.frame(t(groupAttrs), stringsAsFactors = FALSE)
x <- xmlDoc(x)
if(hasAuthorityNS){
secXML <- getNodeSet(x, "//ns:Section", namespaces = c(ns = authorityNs$uri))
if(length(secXML) == 0){
secXML <- getNodeSet(x, "//ns:Section", namespaces = ns)
}
}else{
if(length(ns) > 0){
secXML <- getNodeSet(x, "//ns:Section", namespaces = ns)
}else{
if(nrow(nsDefs.df) > 0){
secNs <- nsDefs.df[1,]
secXML <- getNodeSet(x, "//nt:Section", c(nt = secNs$uri))
}else{
stop("Unsupported CrossSectionalData parser for empty target XML namespace")
}
}
}
if(length(secXML) == 0){
secXML <- getNodeSet(xmlObj, "//Section")
}
secNb <- length(secXML)
if(secNb == 0) return(NULL);
#converting SDMX section to a DataFrame R object
sections <- do.call("rbind.fill", lapply(secXML, parseSection))
group <- data.frame(groupAttrs, sections)
return(group)
}
#converting SDMX groups to a DataFrame R object
dataset <- do.call("rbind.fill", lapply(groupsXML, parseGroup))
if(any(as.character(dataset$obsValue) == "NaN", na.rm = TRUE)){
dataset[as.character(dataset$obsValue) == "NaN",]$obsValue <- NA
}
if(!is.null(dataset)) base::row.names(dataset) <- 1:nrow(dataset)
#enrich with labels
if(labels){
dsd <- slot(x, "dsd")
if(!is.null(dsd)) dataset <- addLabels.SDMXData(dataset, dsd)
}
# output
return(encodeSDMXOutput(dataset))
}
|