Create an SDMX file in R

380 views Asked by At

I do not have any code to post, but just a question. There are several tools I am aware of to read SDMX files in R (an SDMX is an XML file for exchanging statistical data) like for instance

https://github.com/opensdmx/rsdmx

https://github.com/amattioc/SDMX

but does anyone know a way to export some data to an SDMX format for dissemination? Any suggestion is welcome!

1

There are 1 answers

2
Paul Natsuo Kishimoto On

This is not a ‘pure’ R solution, but the Python sdmx1 package is fully usable through reticulate, and allows to programmatically generate SDMX objects and then serialize them as SDMX-ML (XML). For example:

# Use reticulate to import the Python package
> library(reticulate)
> sdmx <- import("sdmx")

# Create an (empty) DataMessage object
> msg <- sdmx$message$DataMessage()

# Convert to XML
> xml <- sdmx$to_xml(msg, pretty_print = TRUE)

# Write to file using the built-in R method
# The Python 'bytes' object must be decoded to a string
> write(xml$decode(), file = "message.xml")

This gives output like:

<mes:GenericData xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:data="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:gen="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xmlns:footer="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <mes:Header>
    <mes:Test>false</mes:Test>
  </mes:Header>
</mes:GenericData>

For more information on authoring more complex messages using sdmx1, there is a page “HOWTO Generate SDMX-ML from Python objects” in the documentation.