r2py: AI-Assisted Conversion of R Statistical Packages to Python
The paper introduces r2py, a human-supervised, AI-agent framework that orchestrates a multi-phase translation and rigorous verification process to convert R statistical packages into native Python libraries while preserving compiled code and ensuring numerical correctness against the original implementations.
Original paper licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
In the world of data analysis, two distinct languages have long dominated the landscape. One, called R, is the historic home of statistics, where new mathematical methods for understanding the world are often written first. It is the reference library for scientists, holding thousands of specialized tools that have been scrutinized and trusted for decades. The other, Python, has become the standard for modern computing, machine learning, and building software that runs in the real world. For years, these two worlds have existed in parallel. When a researcher in Python needs a specific statistical method that exists only in R, they face a difficult choice. They can try to run both languages side-by-side, which is clumsy and requires heavy software installations, or they can attempt to rewrite the R code from scratch in Python. The latter is a massive, error-prone undertaking that often fails to capture the subtle behaviors of the original tool.
A team of researchers has now developed a new way to bridge this gap, not by forcing the two languages to coexist, but by translating the statistical tools themselves into native Python code. They created a system called r2py, which uses artificial intelligence to convert R packages into Python libraries that work without needing the original R software installed. The researchers tested this system on two major, complex statistical packages that are widely used in science. The result is a set of Python tools that behave almost exactly like their R originals, allowing scientists to use decades of statistical innovation within the modern Python environment.
The core challenge in this work is that R and Python are not just different dialects of the same language; they are fundamentally different systems with their own rules. In R, a simple command to calculate a variance might apply a specific correction that Python does not use by default. In R, a function might return a list of results in a specific order, while Python expects a different structure. If a human tries to translate these tools by hand, they might miss these subtle differences, leading to code that looks correct but produces slightly wrong numbers. In science, where a tiny numerical error can change a conclusion, this is unacceptable. Furthermore, many R packages rely on older, compiled code written in languages like C or Fortran to perform heavy calculations. Simply translating the high-level instructions is not enough; the system must also figure out how to connect the new Python code to these old, fast calculation engines.
To solve this, the researchers built a framework that breaks the translation process into seven distinct steps, guided by human experts but executed by artificial intelligence agents. Instead of asking a computer to translate a whole book of code at once, the system first maps out the structure of the package, understanding which functions depend on others. It then creates a detailed guide for every single type of command found in the R code, deciding exactly how each one should be rendered in Python before any actual translation begins. This ensures that the same R command is always translated the same way, preventing the inconsistencies that arise when different parts of the code are handled separately.
Once the plan is set, the system translates the code function by function, in the correct order. Crucially, it does not try to rewrite the heavy mathematical calculations that are already written in compiled code. Instead, it keeps the original, fast code exactly as it is and builds a new bridge in Python that can call it. For one of the packages they tested, this was straightforward. For the other, which used a more complex way of talking to its internal code, the system had to first reconstruct a small, self-contained version of the R system's internal language so the old code could run without the original R software being present. This allowed the new Python package to use the exact same calculation engine as the original, ensuring that any differences in the results would come only from the translation, not from a change in the math.
The researchers then subjected these new packages to a rigorous series of tests. They did not just check if the code ran; they compared the numbers produced by the new Python tools against the numbers produced by the original R tools across hundreds of different scenarios. They tested standard cases, difficult edge cases, and even the error messages the tools produce. In one of the packages, they ran 846 separate tests. In the other, they ran 518. The results showed that the new Python tools reproduced the original R results with extremely high precision, matching the numbers to within a tiny margin of error that is acceptable for scientific work.
However, the process also revealed that the translation is not perfect in every single detail. In a few specific instances, the new tools produced results that differed slightly from the original, but only in ways that were impossible to fix without changing the fundamental nature of the tool. For example, the original R tool displays the command exactly as the user typed it, a feature that relies on the R system's memory of the user's input. The new Python tool, which only sees the final calculated values, cannot recreate that exact text display. The researchers identified these differences and documented them clearly, rather than trying to hide them. They also discovered a genuine flaw in the original R code that caused a tiny error in specific situations; because their goal was to create a faithful copy, they reproduced this flaw exactly in the new Python version, ensuring that scientists using the new tool would get the same results as those using the old one.
The most surprising discovery came from the testing process itself. The researchers found that the best way to catch hidden errors was not to write new tests, but to translate the original tests that came with the R packages. These old tests had been written by the original authors to check for very specific, unusual conditions that a new translator might never think to test. By running these translated tests, the system found subtle bugs that had survived all other checks, including a hidden error in how a control parameter was calculated that only appeared in very deep, complex calculations. This confirmed that the original test suites are an essential part of the translation process, acting as a safety net that catches problems no amount of human inspection could find.
The work demonstrates that converting complex statistical software from one language to another is possible, but it requires a disciplined, step-by-step approach rather than a simple automated translation. The researchers did not just convert two packages; they built a method that can be applied to others. They showed that by separating the planning, the translation of rules, and the verification of results, and by keeping the heavy calculation engines untouched, it is possible to create native Python tools that are faithful to their R origins. The converted packages are now available for anyone to use, installed like any other Python software, bringing decades of statistical innovation into the modern computing environment without the need for the original, heavy software to be installed. The success of this project suggests that the barrier between the statistical methods of the past and the computing tools of the future is more surmountable than previously thought, provided the translation is done with care, structure, and a commitment to numerical accuracy.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.