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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#' @name SDMXData
#' @rdname SDMXData
#' @aliases SDMXData,SDMXData-method
#'
#' @usage
#' SDMXData(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 "SDMXData"
#'
#' @seealso \link{readSDMX}
#' @export
#'
SDMXData <- function(xmlObj, namespaces){
sdmxObj <- SDMX(xmlObj, namespaces)
dsdRef <- dsdRef.SDMXData(xmlObj, namespaces)
dsd <- NULL
if(!is.null(dsdRef)){
dsd <- NULL
}
new("SDMXData",
sdmxObj,
dsdRef = dsdRef,
dsd = dsd
)
}
#get DSD REF
#===========
dsdRef.SDMXData <- function(xmlObj, namespaces){
sdmxVersion <- version.SDMXSchema(xmlObj, namespaces)
dsXML <- xmlChildren(xmlChildren(xmlObj)[[1]])$DataSet
dsdRef <- switch(sdmxVersion,
"1.0" = NULL, #TODO
"2.0" = {
ref <- NULL
xml <- xmlChildren(dsXML)
xmlNames <- names(xml)
if("KeyFamilyRef" %in% xmlNames){
keyFamilyRef <- xml$KeyFamilyRef
ref <- xmlValue(keyFamilyRef)
}
ref
},
"2.1" = xmlGetAttr(dsXML,"structureRef")
)
return(dsdRef)
}
#ENRICH DATA WITH LABELS
#=======================
addLabels.SDMXData <- function(data, dsd){
#try to inherit datastructure components
components <- NULL
datastructures <- slot(slot(dsd,"datastructures"), "datastructures")
if(length(datastructures)>0){
ds <- datastructures[[1]]
components <- slot(ds, "Components")
components <- as.data.frame(components)
}
#function to enrich a column with its labels
enrichColumnWithLabels <- function(column, data, dsd, components){
datac <- as.data.frame(data[,column], stringsAsFactors = FALSE)
colnames(datac) <- column
#grab codelist name
clName <- NULL
if(!is.null(components)){
#try to grab codelist using concepts
clMatcher <- components$conceptRef == column
clName <- components[clMatcher, "codelist"]
if(is.null(clName) || all(is.na(clName))){
#try to grab codelist using regexpr on codelist
clMatcher2 <- regexpr(column, components$codelist, ignore.case = TRUE)
attr(clMatcher2,"match.length")[is.na(clMatcher2)] <- -1
clName <- components[attr(clMatcher2,"match.length")>1, "codelist"]
if(length(clName)>1) clName <- clName[1]
}
if(length(clName)>0 && !is.na(clName)){
#additional check in case codelist would not be specified in DSD
codelists <- sapply(slot(slot(dsd,"codelists"), "codelists"), slot, "id")
if(!(clName %in% codelists)){
clName <- NULL
}
}else if(length(clName)==0){
#check if component has a conceptSchemeRef and if so try to resolve
#codelist from conceptscheme.
conceptSchemeRef <- components[clMatcher, "conceptSchemeRef"]
if(length(conceptSchemeRef)>0 && !is.na(conceptSchemeRef)){
codelists <- sapply(slot(slot(dsd,"codelists"), "codelists"), slot, "id")
conceptSchemeVersion <- components[clMatcher, "conceptSchemeVersion"]
conceptSchemeAgency <- components[clMatcher, "conceptSchemeAgency"]
conceptSchemes <- slot(slot(dsd, "concepts"), "conceptSchemes")
clFound <- FALSE
for(conceptScheme in conceptSchemes){
if(conceptSchemeRef == conceptScheme@id &&
conceptSchemeAgency == conceptScheme@agencyID &&
conceptSchemeVersion == conceptScheme@version){
for(concept in conceptScheme@Concept){
if(concept@id == column){
coreRepresentation = concept@coreRepresentation
if(coreRepresentation %in% codelists){
clName <- coreRepresentation
clFound <- TRUE
break
}
}
}
if(clFound){break}
}
}
}
}
}else{
#no components, take the column name as codelistId
codelists <- sapply(slot(slot(dsd,"codelists"), "codelists"), slot, "id")
if(column %in% codelists){
clName <- column
}
}
if(length(clName) != 0 && !is.na(clName) && !is.null(clName)){
cl <- as.data.frame(slot(dsd, "codelists"), codelistId = clName)
datac$order <- seq(len=nrow(datac))
datac = merge(x = datac, y = cl, by.x = column, by.y = "id",
all.x = TRUE, all.y = FALSE, sort = FALSE)
datac <- datac[sort.list(datac$order),]
datac$order <- NULL
datac <- datac[,((regexpr("label", colnames(datac)) != -1) +
(colnames(datac) == column) == 1)]
colnames(datac)[regexpr("label",colnames(datac)) != -1] <- paste0(column,
"_",colnames(datac)[regexpr("label",colnames(datac)) != -1])
}
return(datac)
}
fulldata <- do.call("cbind" ,lapply(colnames(data), enrichColumnWithLabels,
data, dsd, components))
return(fulldata)
}
#' @name setDSD
#' @docType methods
#' @rdname SDMXData-method
#' @aliases setDSD,SDMXData,SDMXDataStructureDefinition-method
#' @title setDSD
#' @description set the dsd slot of a \code{SDMXData} object
#' @usage
#' setDSD(obj, dsd)
#'
#' @param obj An object deriving from class "SDMXData"
#' @param dsd An object of class "SDMXDataStructureDefinition"
#' @return the 'obj' object of class "SDMXData" enriched with the dsd
#'
#' @seealso \link{SDMXData-class}
#'
#' @author Emmanuel Blondel, \email{emmanuel.blondel1@@gmail.com}
setGeneric("setDSD", function(obj, dsd) standardGeneric("setDSD"));
#' @rdname SDMXData-methods
setMethod(f = "setDSD", signature = c("SDMXData","SDMXDataStructureDefinition"), function(obj, dsd){
slot(obj, "dsd") <- dsd
return(obj)
})
|