1

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!

larry77
  • 1,309
  • 14
  • 29

1 Answers1

1

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.

  • Thanks. Can you please make a reprex? It would make my life easier! – larry77 May 30 '21 at 12:13
  • Sorry—I'm not much of an R user, and thus don't even know what "a reprex" is! The code in the first snippet _is_ complete, if that's what you mean, and will work so long as you have reticulate (R) and sdmx1 (Python) installed. – Paul Natsuo Kishimoto May 30 '21 at 20:58