A clean and robust approach to R / Python interoperability
{nanonext} is an R package available on CRAN which provides bindings to the C library NNG (Nanomsg Next Gen), a successor to ZeroMQ.
Designed for performance and reliability, the NNG library is written in C and {nanonext} is a lightweight wrapper depending on no other packages.
It provides a fast and reliable data interface between different programming languages where NNG has a binding, including C, C++, Java, Python, Go, Rust etc.
The following example demonstrates the exchange of numerical data between R and Python (NumPy), two of the most commonly-used languages for data science and machine learning.
Using a messaging interface provides a clean and robust approach that is light on resources and offers limited and identifiable points of failure. This is especially relevant when processing real-time data, as an example.
This approach can also serve as an interface / pipe between different processes written in the same or different languages, running on the same computer or distributed across networks, and is an enabler of modular software design as espoused by the Unix philosophy.
Create socket in Python using the NNG binding ‘pynng’:
import numpy as np
import pynng
= pynng.Pair0(listen="ipc:///tmp/nanonext") socket
Create nano object in R using {nanonext}, then send a vector of ‘doubles’, specifying mode as ‘raw’:
Receive in Python as a NumPy array of ‘floats’, and send back to R:
= socket.recv()
raw = np.frombuffer(raw)
array print(array)
#> [1.1 2.2 3.3 4.4 5.5]
= array.tobytes()
msg socket.send(msg)
Receive in R, specifying the receive mode as ‘double’:
n$recv(mode = "double")
#> $raw
#> [1] 9a 99 99 99 99 99 f1 3f 9a 99 99 99 99 99 01 40 66 66 66 66 66 66 0a 40 9a
#> [26] 99 99 99 99 99 11 40 00 00 00 00 00 00 16 40
#>
#> $data
#> [1] 1.1 2.2 3.3 4.4 5.5
nanonext on CRAN: https://cran.r-project.org/package=nanonext
Package website: https://shikokuchuo.net/nanonext/
NNG website: https://nng.nanomsg.org/
NNG documentation: https://nng.nanomsg.org/man/tip/
For attribution, please cite this work as
shikokuchuo (2022, Feb. 14). shikokuchuo{net}: nanonext for Cross-language Data Exchange. Retrieved from https://shikokuchuo.net/posts/15-nanonext-exchange/
BibTeX citation
@misc{shikokuchuo2022nanonext, author = {shikokuchuo, }, title = {shikokuchuo{net}: nanonext for Cross-language Data Exchange}, url = {https://shikokuchuo.net/posts/15-nanonext-exchange/}, year = {2022} }