Search code examples
jsonrxmlsdmx

SDMX to dataframe with RSDMX in R


I'm trying to get data from the Lithuanian Statistics Department. They offer SDMX API with either XML or JSON (LSD).

The example XML shown is : https://osp-rs.stat.gov.lt/rest_xml/data/S3R629_M3010217 which downloads the XML file.

I tried following:

devtools::install_github("opensdmx/rsdmx")
library(rsdmx)
string <- "https://osp-rs.stat.gov.lt/rest_xml/data/S3R629_M3010217"
medianage <- readSDMX(string)

which results in error:

<simpleError in doTryCatch(return(expr), name, parentenv, handler): Invalid SDMX-ML file>

I also tried simply reading in the manually downloaded file

devtools::install_github("opensdmx/rsdmx")
library(rsdmx)
medianage <- readSDMX(file="rest_data_M3010217_20180116163251.xml" , isURL = FALSE)
medianage <- as.data.frame(medianage)

results in medianage being NULL (empty)

Maybe soneone has an idea, how I could solve downloading /transforming the data from LSD by using either:

https://osp-rs.stat.gov.lt/rest_xml/data/S3R629_M3010217
https://osp-rs.stat.gov.lt/rest_json/data/S3R629_M3010217 

Thanks a lot!


Solution

  • In order to use rsdmx for this datasource, some enhancements have been added (see details at https://github.com/opensdmx/rsdmx/issues/141). You will need re-install rsdmx from Github (version 0.5-11)

    You can use the url of the SDMX-ML file

    library(rsdmx)
    url <- "https://osp-rs.stat.gov.lt/rest_xml/data/S3R629_M3010217"
    medianage <- readSDMX(url)
    df <- as.data.frame(medianage)
    

    A connector has been added in rsdmx to facilitate data query on the LSD (Lithuanian Statistics Department) SDMX endpoint. See below an example on how to use it.

    sdmx <- readSDMX(providerId = "LSD", resource = "data",
    flowRef = "S3R629_M3010217", dsd = TRUE)
    df <- as.data.frame(sdmx, labels = TRUE)
    

    The above example shows how to enrich the data.frame with code labels extracted from the SDMX Data Structure Definition (DSD). For this, specify dsd = TRUE with readSDMX. This allows then to use labels = TRUE when converting to data.frame. For filtering data with readSDMX, e.g. (startPeriod, endPeriod, code filters), check this page https://github.com/opensdmx/rsdmx/wiki#readsdmx-as-helper-function