From 99b6537b50f26063c5f6602047e19b1ce1212373 Mon Sep 17 00:00:00 2001 From: Giulio Valentino Dalla Riva Date: Tue, 17 Dec 2024 10:52:15 +1300 Subject: [PATCH 01/23] parentCode fetching from parentNode if it exists Fixing a bug where the parent code ids where not reachable. --- R/SDMXCode-methods.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/R/SDMXCode-methods.R b/R/SDMXCode-methods.R index a0f25da..30ed9f8 100644 --- a/R/SDMXCode-methods.R +++ b/R/SDMXCode-methods.R @@ -39,8 +39,16 @@ SDMXCode <- function(xmlObj, namespaces){ urn = xmlGetAttr(xmlObj, "urn") if(is.null(urn)) urn <- as.character(NA) - parentCode = xmlGetAttr(xmlObj, "parentCode") - if(is.null(parentCode)) parentCode <- as.character(NA) + parentCode <- as.character(NA) + parentId <- xmlGetAttr(xmlObj, "parentCode") + if(!is.null(parentId)) parentCode <- parentId + parentNode <- getNodeSet(xmlDoc(xmlObj), "//ns:Parent//Ref", namespaces = strNs) + if(length(parentNode) == 1) parentCode <- xmlGetAttr(parentNode[[1]], "id") + if(length(parentNode) > 1) { + parentCode <- sapply(parentNode, function(x) { xmlGetAttr(x, "id") }) + # we collapse the vector of parent codes into a single string as required by the SDMXCode class + parentCode <- paste(parentCode, collapse = ",") + } #elements #======== From 583e6f5a39e6542fbb4b2b5b265b1b2c1e43eacf Mon Sep 17 00:00:00 2001 From: gvdr Date: Tue, 17 Dec 2024 14:15:56 +1100 Subject: [PATCH 02/23] Test for hierarchical codelists --- ...SDMXCodelists_Example_hierarchical_2.1.xml | 3067 +++++++++++++++++ tests/testthat/test_Codelists.R | 25 +- 2 files changed, 3088 insertions(+), 4 deletions(-) create mode 100644 inst/extdata/SDMXCodelists_Example_hierarchical_2.1.xml diff --git a/inst/extdata/SDMXCodelists_Example_hierarchical_2.1.xml b/inst/extdata/SDMXCodelists_Example_hierarchical_2.1.xml new file mode 100644 index 0000000..bfea630 --- /dev/null +++ b/inst/extdata/SDMXCodelists_Example_hierarchical_2.1.xml @@ -0,0 +1,3067 @@ + + + + + IDREF3313 + false + 2024-12-17T00:41:26.1595+00:00 + + + + + + + Common hierarchical codelist for PICTs + Liste hiérarchique de codes commune pour les PICTs + + + + ORDER + 10000 + 10000 + + + Pacific region + Région du Pacifique + + + + + ORDER + 20000 + 20000 + + + Melanesia + Mélanésie + + + + + ORDER + 30000 + 30000 + + + Micronesia + Micronésie + + + + + ORDER + 40000 + 40000 + + + Polynesia + Polynésie + + + + + ORDER + 100 + 1700 + + + American Samoa + Samoa américaines + + + + + + + + ORDER + 101 + 1701 + + + Eastern + Eastern + + + + + + + + ORDER + 102 + 1702 + + + Manu'a + Manu'a + + + + + + + + ORDER + 103 + 1703 + + + Unorganized + Unorganized + + + + + + + + ORDER + 104 + 1704 + + + Western + Western + + + + + + + + ORDER + 200 + 400 + + + Cook Islands + Îles Cook + + + + + + + + ORDER + 201 + 401 + + + Northern Group + Groupe du Nord + + + + + + + + ORDER + 202 + 402 + + + Rarotonga + Rarotonga + + + + + + + + ORDER + 203 + 403 + + + Southern Group + Groupe du Sud + + + + + + + + ORDER + 300 + 200 + + + Fiji + Fidji + + + + + + + + ORDER + 301 + 201 + + + Central + Centre + + + + + + + + ORDER + 302 + 202 + + + Eastern + Est + + + + + + + + ORDER + 303 + 203 + + + Northern + Nord + + + + + + + + ORDER + 304 + 204 + + + Western + Ouest + + + + + + + + ORDER + 800 + 100 + + + Micronesia (Federated States of) + États fédérés de Micronésie (pays) + + + + + + + + ORDER + 801 + 101 + + + Chuuk + Chuuk + + + + + + + + ORDER + 802 + 102 + + + Kosrae + Kosrae + + + + + + + + ORDER + 803 + 103 + + + Pohnpei + Pohnpei + + + + + + + + ORDER + 804 + 104 + + + Yap + Yap + + + + + + + + ORDER + 500 + 300 + + + Guam + Guam + + + + + + + + ORDER + 501 + 301 + + + Agana Heights + Agana Heights + + + + + + + + ORDER + 502 + 302 + + + Agat + Agat + + + + + + + + ORDER + 503 + 303 + + + Asan + Asan + + + + + + + + ORDER + 504 + 304 + + + Barrigada + Barrigada + + + + + + + + ORDER + 505 + 305 + + + Chalan-Pago-Ordot + Chalan-Pago-Ordot + + + + + + + + ORDER + 506 + 306 + + + Dededo + Dededo + + + + + + + + ORDER + 507 + 307 + + + Hagåtña + Hagåtña + + + + + + + + ORDER + 508 + 308 + + + Inarajan + Inarajan + + + + + + + + ORDER + 509 + 309 + + + Mangilao + Mangilao + + + + + + + + ORDER + 510 + 310 + + + Merizo + Merizo + + + + + + + + ORDER + 511 + 311 + + + Mongmong-Toto-Maite + Mongmong-Toto-Maite + + + + + + + + ORDER + 512 + 312 + + + Piti + Piti + + + + + + + + ORDER + 513 + 313 + + + Santa Rita + Santa Rita + + + + + + + + ORDER + 514 + 314 + + + Sinajana + Sinajana + + + + + + + + ORDER + 515 + 315 + + + Talofofo + Talofofo + + + + + + + + ORDER + 516 + 316 + + + Tamuning + Tamuning + + + + + + + + ORDER + 517 + 317 + + + Umatac + Umatac + + + + + + + + ORDER + 518 + 318 + + + Yigo + Yigo + + + + + + + + ORDER + 519 + 319 + + + Yona + Yona + + + + + + + + ORDER + 600 + 800 + + + Kiribati + Kiribati + + + + + + + + ORDER + 601 + 801 + + + Banaba + Banaba + + + + + + + + ORDER + 602 + 802 + + + Makin + Makin + + + + + + + + ORDER + 603 + 803 + + + Butaritari + Butaritari + + + + + + + + ORDER + 604 + 804 + + + Marakei + Marakei + + + + + + + + ORDER + 605 + 805 + + + Abaiang + Abaiang + + + + + + + + ORDER + 606 + 806 + + + Nth.Tarawa + Nth.Tarawa + + + + + + + + ORDER + 607 + 807 + + + Sth.Tarawa + Sth.Tarawa + + + + + + + + ORDER + 608 + 808 + + + Maiana + Maiana + + + + + + + + ORDER + 609 + 809 + + + Abemama + Abemama + + + + + + + + ORDER + 610 + 810 + + + Kuria + Kuria + + + + + + + + ORDER + 611 + 811 + + + Aranuka + Aranuka + + + + + + + + ORDER + 612 + 812 + + + Nonouti + Nonouti + + + + + + + + ORDER + 613 + 813 + + + Tabiteuea.Nth + Tabiteuea.Nth + + + + + + + + ORDER + 614 + 814 + + + Tabiteuea.Sth + Tabiteuea.Sth + + + + + + + + ORDER + 615 + 815 + + + Beru + Beru + + + + + + + + ORDER + 616 + 816 + + + Nikunau + Nikunau + + + + + + + + ORDER + 617 + 817 + + + Onotoa + Onotoa + + + + + + + + ORDER + 618 + 818 + + + Tamana + Tamana + + + + + + + + ORDER + 619 + 819 + + + Arorae + Arorae + + + + + + + + ORDER + 620 + 820 + + + Teeraina + Teeraina + + + + + + + + ORDER + 621 + 821 + + + Tabuaeran + Tabuaeran + + + + + + + + ORDER + 622 + 822 + + + Kiritimati + Kiritimati + + + + + + + + ORDER + 623 + 823 + + + Kanton + Kanton + + + + + + + + ORDER + 700 + 600 + + + Marshall Islands + Îles Marshall (pays) + + + + + + + + ORDER + 701 + 601 + + + Ailinglaplap + Ailinglaplap + + + + + + + + ORDER + 702 + 602 + + + Ailuk + Ailuk + + + + + + + + ORDER + 703 + 603 + + + Arno + Arno + + + + + + + + ORDER + 704 + 604 + + + Aur + Aur + + + + + + + + ORDER + 705 + 605 + + + Eneu + Eneu + + + + + + + + ORDER + 706 + 606 + + + Ebon + Ebon + + + + + + + + ORDER + 707 + 607 + + + Enewetak + Enewetak + + + + + + + + ORDER + 708 + 608 + + + Jabat + Jabat + + + + + + + + ORDER + 709 + 609 + + + Jaluit + Jaluit + + + + + + + + ORDER + 710 + 610 + + + Kili + Kili + + + + + + + + ORDER + 711 + 611 + + + Kwajalein + Kwajalein + + + + + + + + ORDER + 712 + 612 + + + Lae + Lae + + + + + + + + ORDER + 713 + 613 + + + Lib + Lib + + + + + + + + ORDER + 714 + 614 + + + Likiep + Likiep + + + + + + + + ORDER + 715 + 615 + + + Majuro + Majuro + + + + + + + + ORDER + 716 + 616 + + + Maloelap + Maloelap + + + + + + + + ORDER + 717 + 617 + + + Mejit + Mejit + + + + + + + + ORDER + 718 + 618 + + + Mili + Mili + + + + + + + + ORDER + 719 + 619 + + + Namdrik + Namdrik + + + + + + + + ORDER + 720 + 620 + + + Namu + Namu + + + + + + + + ORDER + 721 + 621 + + + Rongelap + Rongelap + + + + + + + + ORDER + 722 + 622 + + + Ujae + Ujae + + + + + + + + ORDER + 723 + 623 + + + Utirik + Utirik + + + + + + + + ORDER + 724 + 624 + + + Wotho + Wotho + + + + + + + + ORDER + 725 + 625 + + + Wotje + Wotje + + + + + + + + ORDER + 1200 + 500 + + + Northern Mariana Islands + Îles Mariannes du Nord + + + + + + + + ORDER + 1201 + 501 + + + Saipan + Saipan + + + + + + + + ORDER + 1202 + 502 + + + Tinian + Tinian + + + + + + + + ORDER + 1203 + 503 + + + Rota + Rota + + + + + + + + ORDER + 1000 + 1100 + + + New Caledonia + Nouvelle-Calédonie + + + + + + + + ORDER + 1001 + 1101 + + + Loyalty Islands + Îles Loyauté + + + + + + + + ORDER + 1002 + 1102 + + + Northern Province + Province Nord + + + + + + + + ORDER + 1003 + 1103 + + + Southern Province + Province Sud + + + + + + + + ORDER + 900 + 900 + + + Nauru + Nauru + + + + + + + + ORDER + 901 + 901 + + + Yaren + Yaren + + + + + + + + ORDER + 902 + 902 + + + Boe + Boe + + + + + + + + ORDER + 903 + 903 + + + Aiwo + Aiwo + + + + + + + + ORDER + 904 + 904 + + + Buada + Buada + + + + + + + + ORDER + 905 + 905 + + + Denigomodu + Denigomodu + + + + + + + + ORDER + 906 + 906 + + + Nibok + Nibok + + + + + + + + ORDER + 907 + 907 + + + Uaeboe + Uaeboe + + + + + + + + ORDER + 908 + 908 + + + Baitsi + Baitsi + + + + + + + + ORDER + 909 + 909 + + + Ewa + Ewa + + + + + + + + ORDER + 910 + 910 + + + Anibare + Anibare + + + + + + + + ORDER + 911 + 911 + + + Anetan + Anetan + + + + + + + + ORDER + 912 + 912 + + + Anabar + Anabar + + + + + + + + ORDER + 913 + 913 + + + Ijuw + Ijuw + + + + + + + + ORDER + 914 + 914 + + + Meneng + Meneng + + + + + + + + ORDER + 915 + 915 + + + Location + Location + + + + + + + + ORDER + 1100 + 1000 + + + Niue + Niue + + + + + + + + ORDER + 1101 + 1001 + + + Alofi South + Alofi South + + + + + + + + ORDER + 1102 + 1002 + + + Alofi North + Alofi North + + + + + + + + ORDER + 1103 + 1003 + + + Makefu + Makefu + + + + + + + + ORDER + 1104 + 1004 + + + Tuapa + Tuapa + + + + + + + + ORDER + 1105 + 1005 + + + Namukulu + Namukulu + + + + + + + + ORDER + 1106 + 1006 + + + Hikutavake + Hikutavake + + + + + + + + ORDER + 1107 + 1007 + + + Toi + Toi + + + + + + + + ORDER + 1108 + 1008 + + + Mutalau + Mutalau + + + + + + + + ORDER + 1109 + 1009 + + + Lakepa + Lakepa + + + + + + + + ORDER + 1110 + 1010 + + + Liku + Liku + + + + + + + + ORDER + 1111 + 1011 + + + Hakupu + Hakupu + + + + + + + + ORDER + 1112 + 1012 + + + Vaiea + Vaiea + + + + + + + + ORDER + 1113 + 1013 + + + Avatele + Avatele + + + + + + + + ORDER + 1114 + 1014 + + + Tamakautoga + Tamakautoga + + + + + + + + ORDER + 400 + 1400 + + + French Polynesia + Polynésie française + + + + + + + + ORDER + 401 + 1401 + + + Îles Australes + Îles Australes + + + + + + + + ORDER + 402 + 1402 + + + Îles du Vent + Îles du Vent + + + + + + + + ORDER + 403 + 1403 + + + Îles Marquises + Îles Marquises + + + + + + + + ORDER + 404 + 1404 + + + Îles Sous-le-Vent + Îles Sous-le-Vent + + + + + + + + ORDER + 405 + 1405 + + + Îles Tuamotu-Gambier + Îles Tuamotu-Gambier + + + + + + + + ORDER + 1400 + 1300 + + + Papua New Guinea + Papouasie-Nouvelle-Guinée + + + + + + + + ORDER + 1401 + 1301 + + + Bougainville + Bougainville + + + + + + + + ORDER + 1402 + 1302 + + + Central + Central + + + + + + + + ORDER + 1403 + 1303 + + + Chimbu + Chimbu + + + + + + + + ORDER + 1404 + 1304 + + + East New Britain + East New Britain + + + + + + + + ORDER + 1405 + 1305 + + + East Sepik + East Sepik + + + + + + + + ORDER + 1406 + 1306 + + + Eastern Highlands + Eastern Highlands + + + + + + + + ORDER + 1407 + 1307 + + + Enga + Enga + + + + + + + + ORDER + 1408 + 1308 + + + Gulf + Gulf + + + + + + + + ORDER + 1409 + 1309 + + + Hela + Hela + + + + + + + + ORDER + 1410 + 1310 + + + Jiwaka + Jiwaka + + + + + + + + ORDER + 1411 + 1311 + + + Madang + Madang + + + + + + + + ORDER + 1412 + 1312 + + + Manus + Manus + + + + + + + + ORDER + 1413 + 1313 + + + Milne Bay + Milne Bay + + + + + + + + ORDER + 1414 + 1314 + + + Morobe + Morobe + + + + + + + + ORDER + 1415 + 1315 + + + National Capital District + National Capital District + + + + + + + + ORDER + 1416 + 1316 + + + New Ireland + New Ireland + + + + + + + + ORDER + 1417 + 1317 + + + Oro + Oro + + + + + + + + ORDER + 1418 + 1318 + + + Sandaun + Sandaun + + + + + + + + ORDER + 1419 + 1319 + + + Southern Highlands + Southern Highlands + + + + + + + + ORDER + 1420 + 1320 + + + West New Britain + West New Britain + + + + + + + + ORDER + 1421 + 1321 + + + Western + Western + + + + + + + + ORDER + 1422 + 1322 + + + Western Highlands + Western Highlands + + + + + + + + ORDER + 1500 + 700 + + + Pitcairn + Îles Pitcairn + + + + + + + + ORDER + 1300 + 1200 + + + Palau + Palaos + + + + + + + + ORDER + 1301 + 1201 + + + Koror + Koror + + + + + + + + ORDER + 1302 + 1202 + + + Airai + Airai + + + + + + + + ORDER + 1303 + 1203 + + + Outlying States + Outlying States + + + + + + + + ORDER + 1304 + 1204 + + + West Babeldaob + West Babeldaob + + + + + + + + ORDER + 1305 + 1205 + + + East Babeldaob + East Babeldaob + + + + + + + + ORDER + 1700 + 1500 + + + Solomon Islands + Salomon + + + + + + + + ORDER + 1701 + 1501 + + + Choiseul + Choiseul + + + + + + + + ORDER + 1702 + 1502 + + + Western + Western + + + + + + + + ORDER + 1703 + 1503 + + + Isabel + Isabel + + + + + + + + ORDER + 1704 + 1504 + + + Central + Central + + + + + + + + ORDER + 1705 + 1505 + + + Rennell-Bell + Rennell-Bell + + + + + + + + ORDER + 1706 + 1506 + + + Guadalcanal + Guadalcanal + + + + + + + + ORDER + 1707 + 1507 + + + Malaita + Malaita + + + + + + + + ORDER + 1708 + 1508 + + + Makira-Ulawa + Makira-Ulawa + + + + + + + + ORDER + 1709 + 1509 + + + Temotu + Temotu + + + + + + + + ORDER + 1710 + 1510 + + + Honiara + Honiara + + + + + + + + ORDER + 1800 + 1800 + + + Tokelau + Tokelau + + + + + + + + ORDER + 1801 + 1801 + + + Anafu + Anafu + + + + + + + + ORDER + 1802 + 1802 + + + Fakaofo + Fakaofo + + + + + + + + ORDER + 1803 + 1803 + + + Nukunonu + Nukunonu + + + + + + + + ORDER + 1900 + 1900 + + + Tonga + Tonga + + + + + + + + ORDER + 1901 + 1901 + + + Ha'apai + Ha'apai + + + + + + + + ORDER + 1902 + 1902 + + + 'Eua + 'Eua + + + + + + + + ORDER + 1903 + 1903 + + + Niuas + Niuas + + + + + + + + ORDER + 1904 + 1904 + + + Tongatapu + Tongatapu + + + + + + + + ORDER + 1905 + 1905 + + + Vava'u + Vava'u + + + + + + + + ORDER + 2000 + 2000 + + + Tuvalu + Tuvalu + + + + + + + + ORDER + 2001 + 2001 + + + Nanumea + Nanumea + + + + + + + + ORDER + 2002 + 2002 + + + Nanumanga + Nanumanga + + + + + + + + ORDER + 2003 + 2003 + + + Niutao + Niutao + + + + + + + + ORDER + 2004 + 2004 + + + Nui + Nui + + + + + + + + ORDER + 2005 + 2005 + + + Vaitupu + Vaitupu + + + + + + + + ORDER + 2006 + 2006 + + + Nukufetau + Nukufetau + + + + + + + + ORDER + 2007 + 2007 + + + Funafuti + Funafuti + + + + + + + + ORDER + 2008 + 2008 + + + Nukulaelae + Nukulaelae + + + + + + + + ORDER + 2009 + 2009 + + + Niulakita + Niulakita + + + + + + + + ORDER + 2100 + 2100 + + + Vanuatu + Vanuatu + + + + + + + + ORDER + 2101 + 2101 + + + Torba + Torba + + + + + + + + ORDER + 2102 + 2102 + + + Sanma + Sanma + + + + + + + + ORDER + 2103 + 2103 + + + Penama + Penama + + + + + + + + ORDER + 2104 + 2104 + + + Malampa + Malampa + + + + + + + + ORDER + 2105 + 2105 + + + Shefa + Shefa + + + + + + + + ORDER + 2106 + 2106 + + + Tafea + Tafea + + + + + + + + ORDER + 2200 + 2200 + + + Wallis and Futuna + Wallis-et-Futuna + + + + + + + + ORDER + 2201 + 2201 + + + Alo + Alo + + + + + + + + ORDER + 2202 + 2202 + + + Sigave + Sigave + + + + + + + + ORDER + 2203 + 2203 + + + Hahake + Hahake + + + + + + + + ORDER + 2204 + 2204 + + + Hihifo + Hihifo + + + + + + + + ORDER + 2205 + 2205 + + + Mua + Mua + + + + + + + + ORDER + 1600 + 1600 + + + Samoa + Samoa + + + + + + + + ORDER + 1601 + 1601 + + + Apia Urban Area + Apia Urban Area + + + + + + + + ORDER + 1602 + 1602 + + + North West Upolu + North West Upolu + + + + + + + + ORDER + 1603 + 1603 + + + Rest of Upolu + Rest of Upolu + + + + + + + + ORDER + 1604 + 1604 + + + Savaii + Savaii + + + + + + + + ORDER + 50000 + 50000 + + + Pacific region (excluding PNG) + Région du Pacifique (sans PNG) + + + + + ORDER + 60000 + 60000 + + + Melanesia (excluding PNG) + Mélanésie (sans PNG) + + + + + \ No newline at end of file diff --git a/tests/testthat/test_Codelists.R b/tests/testthat/test_Codelists.R index 33ba36e..10d220a 100644 --- a/tests/testthat/test_Codelists.R +++ b/tests/testthat/test_Codelists.R @@ -7,26 +7,43 @@ require(rsdmx, quietly = TRUE) require(testthat) context("SDMXCodelists") -test_that("Codelists - 2.0",{ +test_that("Codelists - 2.0", { file <- system.file("extdata", "SDMXCodelists_Example_2.0.xml", package = "rsdmx") xmlObj <- xmlParse(file) ns <- namespaces.SDMX(xmlObj) codelists <- SDMXCodelists(xmlObj, ns) expect_is(codelists, "SDMXCodelists") expect_equal(length(codelists@codelists), 1L) - + df <- as.data.frame(codelists) expect_is(df, "data.frame") }) -test_that("Codelists - 2.1",{ +test_that("Codelists - 2.1", { file <- system.file("extdata", "SDMXCodelists_Example_2.1.xml", package = "rsdmx") xmlObj <- xmlParse(file) ns <- namespaces.SDMX(xmlObj) codelists <- SDMXCodelists(xmlObj, ns) expect_is(codelists, "SDMXCodelists") expect_equal(length(codelists@codelists), 1L) - + + df <- as.data.frame(codelists) + expect_is(df, "data.frame") +} + +test_that("Hierarchical dodelists", { + file <- system.file("extdata", "SDMXCodelists_Example_hierarchical_2.1.xml", package = "rsdmx") + xmlObj <- xmlParse(file) + ns <- namespaces.SDMX(xmlObj) + codelists <- SDMXCodelists(xmlObj, ns) + expect_is(codelists, "SDMXCodelists") + expect_equal(length(codelists@codelists), 1L) + + # expect to read the right parentCode from the XML object when there is + expect_equal(codelists@codelists[[1]]@Code[[5]]@parentCode, "POL") + # expect to find an NA in parentCode when there is no parentCode in the XML object + expect_true(is.na(codelists@codelists[[1]]@Code[[1]]@parentCode)) + df <- as.data.frame(codelists) expect_is(df, "data.frame") }) \ No newline at end of file From 8253ab32187a164939d73e0fe1c2ffe4e865ea35 Mon Sep 17 00:00:00 2001 From: gvdr Date: Tue, 17 Dec 2024 14:18:36 +1100 Subject: [PATCH 03/23] missed trailing bracket in test --- tests/testthat/test_Codelists.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_Codelists.R b/tests/testthat/test_Codelists.R index 10d220a..8dfabd4 100644 --- a/tests/testthat/test_Codelists.R +++ b/tests/testthat/test_Codelists.R @@ -29,7 +29,7 @@ test_that("Codelists - 2.1", { df <- as.data.frame(codelists) expect_is(df, "data.frame") -} +}) test_that("Hierarchical dodelists", { file <- system.file("extdata", "SDMXCodelists_Example_hierarchical_2.1.xml", package = "rsdmx") From c22da7dac34ae4dd520852c8d84eda93ce7e6185 Mon Sep 17 00:00:00 2001 From: gvdr Date: Tue, 17 Dec 2024 14:18:36 +1100 Subject: [PATCH 04/23] missed trailing bracket and little typo in test --- tests/testthat/test_Codelists.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test_Codelists.R b/tests/testthat/test_Codelists.R index 10d220a..00898fc 100644 --- a/tests/testthat/test_Codelists.R +++ b/tests/testthat/test_Codelists.R @@ -29,9 +29,9 @@ test_that("Codelists - 2.1", { df <- as.data.frame(codelists) expect_is(df, "data.frame") -} +}) -test_that("Hierarchical dodelists", { +test_that("Hierarchical codelists", { file <- system.file("extdata", "SDMXCodelists_Example_hierarchical_2.1.xml", package = "rsdmx") xmlObj <- xmlParse(file) ns <- namespaces.SDMX(xmlObj) From dc6e943d9c53f0e2b8acd653fe69ee69bb0b0b53 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Tue, 17 Dec 2024 12:14:40 -0500 Subject: [PATCH 05/23] Add tests for IMF_DATA --- tests/testthat/test_Main_Helpers.R | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/testthat/test_Main_Helpers.R b/tests/testthat/test_Main_Helpers.R index 06c37bc..84c0f53 100644 --- a/tests/testthat/test_Main_Helpers.R +++ b/tests/testthat/test_Main_Helpers.R @@ -164,6 +164,37 @@ test_that("IMF - data",{ #} }) +#IMF_DATA +#---- + +#-> dataflow +test_that("IMF_DATA - dataflow",{ + testthat::skip_on_cran() + sdmx <- readSDMX(providerId = "IMF_DATA", resource = "dataflow") + if(!is.null(sdmx)){ + expect_is(sdmx, "SDMXDataFlows") + } +}) + +#-> datastructure +test_that("IMF_DATA - datastructure",{ + testthat::skip_on_cran() + sdmx <- readSDMX(providerId = "IMF_DATA", resource = "datastructure", resourceId = "IMF.STA,DSD_CPI") + if(!is.null(sdmx)){ + expect_is(sdmx, "SDMXDataStructureDefinition") + } +}) + +#-> data +test_that("IMF_DATA - data",{ + testthat::skip_on_cran() + sdmx <- readSDMX(providerId = "IMF_DATA", resource = "data", flowRef = "IMF.STA,CPI", + key = list("USA", "CPI", "CP01", "IX", "A"), start = 2020, end = 2020) + if(!is.null(sdmx)){ + expect_is(sdmx, "SDMXStructureSpecificData") + } +}) + #OECD #---- From 27be7e3f434f4b7603dde774c591fd463e02d063 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Tue, 17 Dec 2024 13:37:13 -0500 Subject: [PATCH 06/23] Add references to SDMXRequestParams and SDMXREST21ReuestBuilder. --- R/Class-SDMXRequestParams.R | 4 +++- R/SDMXREST21RequestBuilder-methods.R | 3 ++- R/SDMXRequestParams-methods.R | 5 +++-- R/readSDMX.R | 18 ++++++++++++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/R/Class-SDMXRequestParams.R b/R/Class-SDMXRequestParams.R index 21c86d4..932b76e 100644 --- a/R/Class-SDMXRequestParams.R +++ b/R/Class-SDMXRequestParams.R @@ -17,7 +17,8 @@ #' @slot flowRef an object of class "character" giving the flowRef to be queried #' @slot key an object of class "character" giving the key (SDMX url formatted) to be used for the query #' @slot start an object of class "character" giving the start time -#' @slot end an object of class "character" giving the end time +#' @slot end an object of class "character" giving the end time +#' @slot references an object of class "character" giving the instructions to return (or not) the artefacts referenced by the artefact to be returned #' @slot compliant an object of class "logical" indicating if the web-service is compliant with the SDMX REST web-service specifications #' #' @section Warning: @@ -40,6 +41,7 @@ setClass("SDMXRequestParams", key = "character_OR_NULL", start = "character_OR_numeric_OR_NULL", end = "character_OR_numeric_OR_NULL", + references = "character_OR_NULL", compliant = "logical" ), prototype = list(), diff --git a/R/SDMXREST21RequestBuilder-methods.R b/R/SDMXREST21RequestBuilder-methods.R index e575a00..6aaa914 100644 --- a/R/SDMXREST21RequestBuilder-methods.R +++ b/R/SDMXREST21RequestBuilder-methods.R @@ -82,7 +82,8 @@ SDMXREST21RequestBuilder <- function(regUrl, repoUrl, accessKey = NULL, complian if(is.null(obj@version)) obj@version = "latest" req <- sprintf("%s/datastructure/%s/%s/%s/",obj@regUrl, obj@agencyId, obj@resourceId, obj@version) if(forceProviderId) req <- paste(req, obj@providerId, sep = "/") - req <- paste0(req, "?references=children") #TODO to see later to have arg for this + if(is.null(obj@references)) obj@references = "children" + req <- paste0(req, "?references=", obj@references) #require key if(!is.null(accessKey)){ diff --git a/R/SDMXRequestParams-methods.R b/R/SDMXRequestParams-methods.R index d2d2644..d8350ef 100644 --- a/R/SDMXRequestParams-methods.R +++ b/R/SDMXRequestParams-methods.R @@ -21,6 +21,7 @@ #' @param key an object of class "character" giving the key (SDMX url formatted) to be used for the query #' @param start an object of class "character" giving the start time #' @param end an object of class "character" giving the end time +#' @param references an object of class "character" giving the instructions to return (or not) the artefacts referenced by the artefact to be returned #' @param compliant an object of class "logical" indicating if the web-service is compliant with the SDMX REST web-service specifications #' #' @examples @@ -33,10 +34,10 @@ #' @export #' SDMXRequestParams <- function(regUrl, repoUrl, accessKey, providerId, agencyId, resource, resourceId, version = NULL, - flowRef, key = NULL, start = NULL, end = NULL, compliant){ + flowRef, key = NULL, start = NULL, end = NULL, references = NULL, compliant){ new("SDMXRequestParams", regUrl = regUrl, repoUrl = repoUrl, accessKey = accessKey, providerId = providerId, agencyId = agencyId, resource = resource, resourceId = resourceId, version = version, - flowRef = flowRef, key = key, start = start, end = end) + flowRef = flowRef, key = key, start = start, end = end, references = references) } diff --git a/R/readSDMX.R b/R/readSDMX.R index 2f1c8bc..89a9441 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -48,6 +48,8 @@ #' Recognized if a valid provider or provide id has been specified as argument. #' @param end an object of class "integer" or "character" giving the SDMX end time to apply. #' Recognized if a valid provider or provide id has been specified as argument. +#' @param references an object of class "character" giving the instructions to return (or not) the +#' artefacts referenced by the artefact to be returned. #' @param dsd an Object of class "logical" if an attempt to inherit the DSD should be performed. #' Active only if \code{"readSDMX"} is used as helper method (ie if data is fetched using #' an embedded service provider. Default is FALSE @@ -138,7 +140,7 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, provider = NULL, providerId = NULL, providerKey = NULL, agencyId = NULL, resource = NULL, resourceId = NULL, version = NULL, flowRef = NULL, key = NULL, key.mode = "R", start = NULL, end = NULL, dsd = FALSE, - headers = list(), validate = FALSE, + headers = list(), validate = FALSE, references = NULL, verbose = !is.null(logger), logger = "INFO", ...) { #logger @@ -203,6 +205,7 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, key = key, start = start, end = end, + references = references, compliant = provider@builder@compliant ) #formatting requestParams @@ -450,9 +453,16 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, } if(resource == "data"){ - dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, - resource = "datastructure", resourceId = dsdRef, headers = headers, - verbose = verbose, logger = logger, ...) + if providerId == "IMF_DATA"{ + dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, + resource = "datastructure", resourceId = dsdRef, headers = headers, + verbose = verbose, references = "descendants", logger = logger, ...) + }else{ + dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, + resource = "datastructure", resourceId = dsdRef, headers = headers, + verbose = verbose, logger = logger, ...) + } + if(is.null(dsdObj)){ log$WARN(sprintf("Impossible to fetch DSD for dataset '%s'", flowRef)) }else{ From c6e88a11d78c2e639b58e46b0359b4d97bb6ffd9 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Tue, 17 Dec 2024 13:47:28 -0500 Subject: [PATCH 07/23] fix typo --- R/readSDMX.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/readSDMX.R b/R/readSDMX.R index 89a9441..1510588 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -453,10 +453,10 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, } if(resource == "data"){ - if providerId == "IMF_DATA"{ + if(providerId == "IMF_DATA"){ dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, - resource = "datastructure", resourceId = dsdRef, headers = headers, - verbose = verbose, references = "descendants", logger = logger, ...) + resource = "datastructure", resourceId = dsdRef, headers = headers, + verbose = verbose, references = "descendants", logger = logger, ...) }else{ dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, resource = "datastructure", resourceId = dsdRef, headers = headers, From e85fd124d3cf3c1d8d7d24031ed0fd36ed224695 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Tue, 17 Dec 2024 14:02:52 -0500 Subject: [PATCH 08/23] Update Doc --- man/SDMXRequestParams.Rd | 4 ++++ man/readSDMX.Rd | 3 +++ 2 files changed, 7 insertions(+) diff --git a/man/SDMXRequestParams.Rd b/man/SDMXRequestParams.Rd index 2b4629b..99f3e81 100644 --- a/man/SDMXRequestParams.Rd +++ b/man/SDMXRequestParams.Rd @@ -39,6 +39,8 @@ mandatory for some service providers.} \item{end}{an object of class "character" giving the end time} +\item{references}{an object of class "character" giving the instructions to return (or not) the artefacts referenced by the artefact to be returned} + \item{compliant}{an object of class "logical" indicating if the web-service is compliant with the SDMX REST web-service specifications} } \description{ @@ -72,6 +74,8 @@ an authentication or subscription user key/token has to be provided to perform r \item{\code{end}}{an object of class "character" giving the end time} +\item{\code{references}}{an object of class "character" giving the instructions to return (or not) the artefacts referenced by the artefact to be returned} + \item{\code{compliant}}{an object of class "logical" indicating if the web-service is compliant with the SDMX REST web-service specifications} }} diff --git a/man/readSDMX.Rd b/man/readSDMX.Rd index 7dcc106..50464d1 100644 --- a/man/readSDMX.Rd +++ b/man/readSDMX.Rd @@ -75,6 +75,9 @@ an embedded service provider. Default is FALSE} be performed on the SDMX-ML document to check its SDMX compliance when reading it. Default is FALSE.} +\item{references}{an object of class "character" giving the instructions to return (or not) the +artefacts referenced by the artefact to be returned.} + \item{verbose}{an Object of class "logical" that indicates if rsdmx logs should appear to user. Default is set to \code{FALSE} (see argument \code{logger}).} From 57dc12e9527420c851388a2f5075a8277a038e1e Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Tue, 17 Dec 2024 16:05:34 -0500 Subject: [PATCH 09/23] Use new parameter in service provider --- R/SDMXServiceProvider-methods.R | 1 + R/readSDMX.R | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/R/SDMXServiceProvider-methods.R b/R/SDMXServiceProvider-methods.R index 654e941..9a3ad7f 100644 --- a/R/SDMXServiceProvider-methods.R +++ b/R/SDMXServiceProvider-methods.R @@ -168,6 +168,7 @@ setSDMXServiceProviders <- function(){ # nocov start builder = SDMXREST21RequestBuilder( regUrl = "https://api.imf.org/external/sdmx/2.1", repoUrl = "https://api.imf.org/external/sdmx/2.1", + references = "descendants", compliant = TRUE) ) diff --git a/R/readSDMX.R b/R/readSDMX.R index 1510588..43a2bb1 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -453,15 +453,15 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, } if(resource == "data"){ - if(providerId == "IMF_DATA"){ - dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, - resource = "datastructure", resourceId = dsdRef, headers = headers, - verbose = verbose, references = "descendants", logger = logger, ...) - }else{ - dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, - resource = "datastructure", resourceId = dsdRef, headers = headers, - verbose = verbose, logger = logger, ...) - } + #if(providerId == "IMF_DATA"){ + # dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, + # resource = "datastructure", resourceId = dsdRef, headers = headers, + # verbose = verbose, references = "descendants", logger = logger, ...) + #}else{ + dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, + resource = "datastructure", resourceId = dsdRef, headers = headers, + verbose = verbose, logger = logger, ...) + #} if(is.null(dsdObj)){ log$WARN(sprintf("Impossible to fetch DSD for dataset '%s'", flowRef)) From 61583e8b4fdd9ae56586f57ebce84fc2430f0fdd Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Wed, 18 Dec 2024 10:59:59 -0500 Subject: [PATCH 10/23] Pass references for IMF_DATA requests --- R/SDMXRequestParams-methods.R | 2 +- R/SDMXServiceProvider-methods.R | 1 - R/readSDMX.R | 19 +++++++++++-------- tests/testthat/test_Main_Helpers.R | 10 +--------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/R/SDMXRequestParams-methods.R b/R/SDMXRequestParams-methods.R index d8350ef..bb170c1 100644 --- a/R/SDMXRequestParams-methods.R +++ b/R/SDMXRequestParams-methods.R @@ -29,7 +29,7 @@ #' params <- SDMXRequestParams( #' regUrl = "", repoUrl ="", accessKey = NULL, #' providerId = "", agencyId ="", resource = "data", resourceId = "", -#' version = "", flowRef = "", key = NULL, start = NULL, end = NULL, compliant = FALSE +#' version = "", flowRef = "", key = NULL, start = NULL, end = NULL, references = NULL, compliant = FALSE #' ) #' @export #' diff --git a/R/SDMXServiceProvider-methods.R b/R/SDMXServiceProvider-methods.R index 9a3ad7f..654e941 100644 --- a/R/SDMXServiceProvider-methods.R +++ b/R/SDMXServiceProvider-methods.R @@ -168,7 +168,6 @@ setSDMXServiceProviders <- function(){ # nocov start builder = SDMXREST21RequestBuilder( regUrl = "https://api.imf.org/external/sdmx/2.1", repoUrl = "https://api.imf.org/external/sdmx/2.1", - references = "descendants", compliant = TRUE) ) diff --git a/R/readSDMX.R b/R/readSDMX.R index 43a2bb1..bd241b7 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -208,6 +208,14 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, references = references, compliant = provider@builder@compliant ) + + #allow IMF requests to + if(providerId == "IMF_DATA"){ + if is.null(references){ + requestParams@references <- "descendants" + } + } + #formatting requestParams requestFormatter <- provider@builder@formatter requestParams <- switch(resource, @@ -453,15 +461,10 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, } if(resource == "data"){ - #if(providerId == "IMF_DATA"){ - # dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, - # resource = "datastructure", resourceId = dsdRef, headers = headers, - # verbose = verbose, references = "descendants", logger = logger, ...) - #}else{ dsdObj <- readSDMX(providerId = providerId, providerKey = providerKey, resource = "datastructure", resourceId = dsdRef, headers = headers, - verbose = verbose, logger = logger, ...) - #} + verbose = verbose, references = references, logger = logger, ...) + if(is.null(dsdObj)){ log$WARN(sprintf("Impossible to fetch DSD for dataset '%s'", flowRef)) @@ -473,7 +476,7 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, dsdObj <- lapply(1:length(dsdRef), function(x){ flowDsd <- readSDMX(providerId = providerId, providerKey = providerKey, resource = "datastructure", resourceId = dsdRef[[x]], headers = headers, - verbose = verbose, logger = logger, ...) + verbose = verbose, references = references, logger = logger, ...) if(is.null(flowDsd)){ log$INFO(sprintf("Impossible to fetch DSD for dataflow '%s'",resourceId)) }else{ diff --git a/tests/testthat/test_Main_Helpers.R b/tests/testthat/test_Main_Helpers.R index 84c0f53..9d86bfa 100644 --- a/tests/testthat/test_Main_Helpers.R +++ b/tests/testthat/test_Main_Helpers.R @@ -154,15 +154,7 @@ test_that("IMF - datastructure",{ } }) -#-> data -test_that("IMF - data",{ - testthat::skip_on_cran() - #TODO to test, sounds it's not public anymore - #sdmx <- readSDMX(providerId = "IMF", resource = "data", flowRef = "BOP_GBPM6", start = 2020, end = 2020) - #if(!is.null(sdmx)){ - # expect_is(sdmx, "SDMXStructureSpecificData") - #} -}) +#No plans to dissiminate data on IMF provider, IMF data avaliable on IMF_DATA. #IMF_DATA #---- From b22a8f59f8c2bf0bc5c5a34611a3ec8e98844809 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Wed, 18 Dec 2024 11:15:04 -0500 Subject: [PATCH 11/23] Typo --- R/readSDMX.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/readSDMX.R b/R/readSDMX.R index bd241b7..3984adf 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -211,7 +211,7 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, #allow IMF requests to if(providerId == "IMF_DATA"){ - if is.null(references){ + if(is.null(references)){ requestParams@references <- "descendants" } } From e85a4206f80ff178cffef747af91fbb6c73baed0 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Wed, 18 Dec 2024 16:45:17 -0500 Subject: [PATCH 12/23] Typo --- R/readSDMX.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/readSDMX.R b/R/readSDMX.R index 3984adf..4e866a2 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -209,7 +209,7 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, compliant = provider@builder@compliant ) - #allow IMF requests to + #allow IMF requests to use descendants instead of children if(providerId == "IMF_DATA"){ if(is.null(references)){ requestParams@references <- "descendants" From 63a7e9b78ed6ff0e2d082db230edcd3f5c541f63 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Wed, 18 Dec 2024 16:54:02 -0500 Subject: [PATCH 13/23] Update Documentation --- man/SDMXRequestParams.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/SDMXRequestParams.Rd b/man/SDMXRequestParams.Rd index 99f3e81..81d157c 100644 --- a/man/SDMXRequestParams.Rd +++ b/man/SDMXRequestParams.Rd @@ -90,7 +90,7 @@ encapsulate it as slot, when parsing an SDMX-ML document. params <- SDMXRequestParams( regUrl = "", repoUrl ="", accessKey = NULL, providerId = "", agencyId ="", resource = "data", resourceId = "", - version = "", flowRef = "", key = NULL, start = NULL, end = NULL, compliant = FALSE + version = "", flowRef = "", key = NULL, start = NULL, end = NULL, references = NULL, compliant = FALSE ) } \author{ From b289c0f1176fd7d826b3cdfbac80d126e1c36c8a Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Wed, 18 Dec 2024 17:03:28 -0500 Subject: [PATCH 14/23] Update Docs --- R/SDMXRequestParams-methods.R | 2 +- R/readSDMX.R | 2 +- man/SDMXRequestParams.Rd | 2 +- man/readSDMX.Rd | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/SDMXRequestParams-methods.R b/R/SDMXRequestParams-methods.R index bb170c1..e492ab5 100644 --- a/R/SDMXRequestParams-methods.R +++ b/R/SDMXRequestParams-methods.R @@ -5,7 +5,7 @@ #' @usage #' SDMXRequestParams(regUrl, repoUrl, accessKey, #' providerId, agencyId, resource, resourceId, version, -#' flowRef, key, start, end, compliant) +#' flowRef, key, start, end, references = NULL, compliant) #' #' @param regUrl an object of class "character" giving the base Url of the SDMX service registry #' @param repoUrl an object of class "character" giving the base Url of the SDMX service repository diff --git a/R/readSDMX.R b/R/readSDMX.R index 4e866a2..ea40903 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -7,7 +7,7 @@ #' provider = NULL, providerId = NULL, providerKey = NULL, #' agencyId = NULL, resource = NULL, resourceId = NULL, version = NULL, #' flowRef = NULL, key = NULL, key.mode = "R", start = NULL, end = NULL, dsd = FALSE, -#' headers = list(), validate = FALSE, +#' headers = list(), validate = FALSE, references = NULL, #' verbose = !is.null(logger), logger = "INFO", ...) #' #' @param file path to SDMX-ML document that needs to be parsed diff --git a/man/SDMXRequestParams.Rd b/man/SDMXRequestParams.Rd index 81d157c..0f4e4e1 100644 --- a/man/SDMXRequestParams.Rd +++ b/man/SDMXRequestParams.Rd @@ -10,7 +10,7 @@ \usage{ SDMXRequestParams(regUrl, repoUrl, accessKey, providerId, agencyId, resource, resourceId, version, - flowRef, key, start, end, compliant) + flowRef, key, start, end, references = NULL, compliant) } \arguments{ \item{regUrl}{an object of class "character" giving the base Url of the SDMX service registry} diff --git a/man/readSDMX.Rd b/man/readSDMX.Rd index 50464d1..193687e 100644 --- a/man/readSDMX.Rd +++ b/man/readSDMX.Rd @@ -8,7 +8,7 @@ readSDMX(file = NULL, isURL = TRUE, isRData = FALSE, provider = NULL, providerId = NULL, providerKey = NULL, agencyId = NULL, resource = NULL, resourceId = NULL, version = NULL, flowRef = NULL, key = NULL, key.mode = "R", start = NULL, end = NULL, dsd = FALSE, - headers = list(), validate = FALSE, + headers = list(), validate = FALSE, references = NULL, verbose = !is.null(logger), logger = "INFO", ...) } \arguments{ From 23c89310e2160baa4117592fd2362d548400b184 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Thu, 19 Dec 2024 13:44:35 -0500 Subject: [PATCH 15/23] Allow conceptXML to find nodes under ConceptScheme --- R/SDMXConcepts-methods.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/SDMXConcepts-methods.R b/R/SDMXConcepts-methods.R index 0cc21ff..b034c40 100644 --- a/R/SDMXConcepts-methods.R +++ b/R/SDMXConcepts-methods.R @@ -40,6 +40,11 @@ concepts.SDMXConcepts <- function(xmlObj, namespaces){ "//mes:Structures/str:Concepts/str:Concept", namespaces = c(mes = as.character(messageNs), str = as.character(strNs))) + conceptsXML <- c(conceptsXML, + getNodeSet(xmlObj, + "//mes:Structures/str:Concepts/str:ConceptScheme/str:Concept", + namespaces = c(mes = as.character(messageNs), + str = as.character(strNs)))) }else{ conceptsXML <- getNodeSet(xmlObj, "//mes:Concepts/str:Concept", From acc315d5c8271c7371aeeaa9b0e67c45bced15df Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Thu, 19 Dec 2024 14:38:28 -0500 Subject: [PATCH 16/23] Add information for concept scheme for dimension. --- R/Class-SDMXDimension.R | 2 ++ R/SDMXDimension-methods.R | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/R/Class-SDMXDimension.R b/R/Class-SDMXDimension.R index 3540fc1..6386de1 100644 --- a/R/Class-SDMXDimension.R +++ b/R/Class-SDMXDimension.R @@ -38,6 +38,7 @@ setClass("SDMXDimension", conceptVersion = "character", #optional conceptAgency = "character", #optional conceptSchemeRef = "character", #optional + conceptSchemeVersion = "character", #optional conceptSchemeAgency = "character", #optional codelist = "character", #optional codelistVersion = "character", #optional @@ -63,6 +64,7 @@ setClass("SDMXDimension", conceptVersion = "1.0", conceptAgency = "ORG", conceptSchemeRef = "CONCEPT_SCHEME", + conceptSchemeVersion = "1.0", conceptSchemeAgency = "ORG", codelist = "CODELIST", codelistVersion = "1.0", diff --git a/R/SDMXDimension-methods.R b/R/SDMXDimension-methods.R index 2139602..98a208d 100644 --- a/R/SDMXDimension-methods.R +++ b/R/SDMXDimension-methods.R @@ -45,6 +45,7 @@ SDMXDimension <- function(xmlObj, namespaces){ conceptVersion <- NULL conceptAgency <- NULL conceptSchemeRef <- NULL + conceptSchemeVersion <- NULL conceptSchemeAgency <- NULL codelist <- NULL codelistVersion <- NULL @@ -64,10 +65,16 @@ SDMXDimension <- function(xmlObj, namespaces){ #concepts if(!is.null(conceptRefXML)){ conceptRef = xmlGetAttr(conceptRefXML, "id") - conceptVersion = xmlGetAttr(conceptRefXML, "maintainableParentVersion") - conceptAgency = xmlGetAttr(conceptRefXML, "agencyID") - #TODO conceptSchemeRef? - #TODO conceptSchemeAgency + package = xmlGetAttr(conceptRefXML, "package") + if(pacakge == "conceptscheme"){ + conceptSchemeRef = xmlGetAttr(conceptRefXML, "maintainableParentID") + conceptSchemeVersion = xmlGetAttr(conceptRefXML, "maintainableParentVersion") + conceptSchemeAgency = xmlGetAttr(conceptRefXML, "agencyID") + }else{ + conceptVersion = xmlGetAttr(conceptRefXML, "maintainableParentVersion") + conceptAgency = xmlGetAttr(conceptRefXML, "agencyID") + } + } #codelists @@ -201,6 +208,7 @@ SDMXDimension <- function(xmlObj, namespaces){ conceptVersion = conceptVersion, conceptAgency = conceptAgency, conceptSchemeRef = conceptSchemeRef, + conceptSchemeVersion = conceptSchemeVersion, conceptSchemeAgency = conceptSchemeAgency, codelist = codelist, codelistVersion = codelistVersion, From b1edd485c467bf7f89cd5e9756de386af40c384d Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Thu, 19 Dec 2024 14:49:12 -0500 Subject: [PATCH 17/23] Typo --- R/SDMXDimension-methods.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/SDMXDimension-methods.R b/R/SDMXDimension-methods.R index 98a208d..caf069c 100644 --- a/R/SDMXDimension-methods.R +++ b/R/SDMXDimension-methods.R @@ -66,7 +66,7 @@ SDMXDimension <- function(xmlObj, namespaces){ if(!is.null(conceptRefXML)){ conceptRef = xmlGetAttr(conceptRefXML, "id") package = xmlGetAttr(conceptRefXML, "package") - if(pacakge == "conceptscheme"){ + if(package == "conceptscheme"){ conceptSchemeRef = xmlGetAttr(conceptRefXML, "maintainableParentID") conceptSchemeVersion = xmlGetAttr(conceptRefXML, "maintainableParentVersion") conceptSchemeAgency = xmlGetAttr(conceptRefXML, "agencyID") From c468d6ed2f3a7b75e8e190eb5cff833c61318c94 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Fri, 20 Dec 2024 13:04:47 -0500 Subject: [PATCH 18/23] check if component has a conceptSchemeRef and if so try to resolve codelist from conceptscheme. --- R/SDMXData-methods.R | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/R/SDMXData-methods.R b/R/SDMXData-methods.R index 6d0d486..0da0c4d 100644 --- a/R/SDMXData-methods.R +++ b/R/SDMXData-methods.R @@ -79,9 +79,9 @@ addLabels.SDMXData <- function(data, dsd){ clName <- components[clMatcher, "codelist"] if(is.null(clName) || all(is.na(clName))){ #try to grab codelist using regexpr on codelist - clMatcher <- regexpr(column, components$codelist, ignore.case = TRUE) - attr(clMatcher,"match.length")[is.na(clMatcher)] <- -1 - clName <- components[attr(clMatcher,"match.length")>1, "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] } @@ -91,6 +91,34 @@ addLabels.SDMXData <- function(data, dsd){ 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@id){ + for(concept in conceptScheme){ + if(concept@id == column){ + coreRepresentation = concept@coreRepresentation + if(coreRepresentation %in% codelists){ + clName <- coreRepresentation + clFound <- TRUE + break + } + } + } + if(newCLFound){break} + } + } + } } }else{ From 377aac2988d92647181a5e9f7e65f6afbc6109db Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Fri, 20 Dec 2024 14:01:06 -0500 Subject: [PATCH 19/23] Allow to break when codelist found --- R/SDMXData-methods.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/SDMXData-methods.R b/R/SDMXData-methods.R index 0da0c4d..4aab872 100644 --- a/R/SDMXData-methods.R +++ b/R/SDMXData-methods.R @@ -104,8 +104,8 @@ addLabels.SDMXData <- function(data, dsd){ for(conceptScheme in conceptSchemes){ if(conceptSchemeRef == conceptScheme@id && conceptSchemeAgency == conceptScheme@agencyID && - conceptSchemeVersion == conceptScheme@id){ - for(concept in conceptScheme){ + conceptSchemeVersion == conceptScheme@version){ + for(concept in conceptScheme@Concept){ if(concept@id == column){ coreRepresentation = concept@coreRepresentation if(coreRepresentation %in% codelists){ @@ -115,7 +115,7 @@ addLabels.SDMXData <- function(data, dsd){ } } } - if(newCLFound){break} + if(clFound){break} } } } From 5ab0e337e45ccdb2a45ace6835030c7ceb6bad75 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Fri, 20 Dec 2024 14:43:56 -0500 Subject: [PATCH 20/23] Convert NULL to NA for conceptSchemeVersion --- R/SDMXDimension-methods.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/SDMXDimension-methods.R b/R/SDMXDimension-methods.R index caf069c..ebd8586 100644 --- a/R/SDMXDimension-methods.R +++ b/R/SDMXDimension-methods.R @@ -130,6 +130,7 @@ SDMXDimension <- function(xmlObj, namespaces){ if(is.null(conceptVersion)) conceptVersion <- as.character(NA) if(is.null(conceptAgency)) conceptAgency <- as.character(NA) if(is.null(conceptSchemeRef)) conceptSchemeRef <- as.character(NA) + if(is.null(conceptSchemeVersion)) conceptSchemeVersion <- as.character(NA) if(is.null(conceptSchemeAgency)) conceptSchemeAgency <- as.character(NA) if(is.null(codelist)) codelist <- as.character(NA) From cbf250bd542800efcbe8b003c3be95dfe25fb089 Mon Sep 17 00:00:00 2001 From: Allen Boddie Date: Fri, 20 Dec 2024 19:46:38 -0500 Subject: [PATCH 21/23] Use Formatter --- R/SDMXServiceProvider-methods.R | 5 ++++- R/readSDMX.R | 7 ------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/R/SDMXServiceProvider-methods.R b/R/SDMXServiceProvider-methods.R index 654e941..fd5c645 100644 --- a/R/SDMXServiceProvider-methods.R +++ b/R/SDMXServiceProvider-methods.R @@ -168,7 +168,10 @@ setSDMXServiceProviders <- function(){ # nocov start builder = SDMXREST21RequestBuilder( regUrl = "https://api.imf.org/external/sdmx/2.1", repoUrl = "https://api.imf.org/external/sdmx/2.1", - compliant = TRUE) + compliant = TRUE, + formatter$datastructure = function(obj){ + if(is.null(obj@references)) obj@references = "descendants" + return(obj)}) ) #OECD diff --git a/R/readSDMX.R b/R/readSDMX.R index ea40903..e2fd81c 100644 --- a/R/readSDMX.R +++ b/R/readSDMX.R @@ -208,13 +208,6 @@ readSDMX <- function(file = NULL, isURL = TRUE, isRData = FALSE, references = references, compliant = provider@builder@compliant ) - - #allow IMF requests to use descendants instead of children - if(providerId == "IMF_DATA"){ - if(is.null(references)){ - requestParams@references <- "descendants" - } - } #formatting requestParams requestFormatter <- provider@builder@formatter From b1ee8323ddec0a426497368651d2094c9ec30fde Mon Sep 17 00:00:00 2001 From: Emmanuel Blondel Date: Wed, 8 Jan 2025 10:00:58 +0100 Subject: [PATCH 22/23] fix formatter --- R/SDMXServiceProvider-methods.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/R/SDMXServiceProvider-methods.R b/R/SDMXServiceProvider-methods.R index fd5c645..5e43d5e 100644 --- a/R/SDMXServiceProvider-methods.R +++ b/R/SDMXServiceProvider-methods.R @@ -169,9 +169,12 @@ setSDMXServiceProviders <- function(){ # nocov start regUrl = "https://api.imf.org/external/sdmx/2.1", repoUrl = "https://api.imf.org/external/sdmx/2.1", compliant = TRUE, - formatter$datastructure = function(obj){ - if(is.null(obj@references)) obj@references = "descendants" - return(obj)}) + formatter = list( + datastructure = function(obj){ + if(is.null(obj@references)) obj@references = "descendants" + return(obj) + } + ) ) #OECD From 2d25ddcb4f98386e13c7cdad1980a7524581eedf Mon Sep 17 00:00:00 2001 From: Emmanuel Blondel Date: Wed, 8 Jan 2025 10:12:12 +0100 Subject: [PATCH 23/23] fix formatter - typo --- R/SDMXServiceProvider-methods.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/SDMXServiceProvider-methods.R b/R/SDMXServiceProvider-methods.R index 5e43d5e..c50c2f6 100644 --- a/R/SDMXServiceProvider-methods.R +++ b/R/SDMXServiceProvider-methods.R @@ -175,6 +175,7 @@ setSDMXServiceProviders <- function(){ # nocov start return(obj) } ) + ) ) #OECD