Introducing SnakeCharmR

I have been using rPython for some time in Linux and Mac OS X environments. When I first found out about it, the idea of using JSON as a way to serialize and deserialize values between Python and R was beautifully simple and intuitive.

However, over time I realized rPython suffered from a more than a few issues. Plus, I noticed that the codebase hadn't been updated in a long time, was based on direct C code and used RJSONIO for the underlying JSON conversions.

So I decided to create a new package called SnakeCharmR inspired by rPython but with the following differences:

  • Use Rcpp instead of direct R to C bindings;

  • Replace RJSONIO with the much more well-behaved jsonlite for JSON serialization / deserialization;

  • Allow the calling code to have full control of the options to be passed to jsonlite::fromJSON and jsonlite::toJSON;

  • Use testthat for unit testing as much as possible, including Travis and Codecov for automation of tests and coverage.

The package is not a drop-in replacement to rPython, but most of existing code will require very few updates to work with it. Most of them will be related to the differences between RJSONIO and jsonlite.

Installation

The package has not yet been submitted to CRAN, so currently this is how you install it:

> library(devtools)
> install_github("asieira/SnakeCharmR")

Please refer to the README for more details on linking with the proper Python version.

Comparison with rPython

A few examples of differences in the way SnakeCharmR and rPython handle some situations:

  • SnakeCharmR will correctly remove any temporary values used for function/method arguments and/or serialized values from the Python environment. In the case of rPython, they would remain in memory until overwritten by another operation.

  • Handling of strings with single quotes:

> library(rPython)
> python.assign("a", "'")
  File "<string>", line 2
    a =' [ "'" ] '
                 ^
SyntaxError: EOL while scanning string literal
> library(SnakeCharmR)
SnakeCharmR v1.0.2 - R and Python Integration
Contribute and submit issues at https://github.com/asieira/SnakeCharmR
> py.assign("a", "'")
> py.get("a")
[1] "'"
  • Handling of strings with unicode values:
> library(rPython)
> python.assign("a", "áéíóú")
> python.get("a")
[1] "\xe1\xe9\xed\xf3\xfa"
> Encoding(python.get("a"))
[1] "unknown"
> library(SnakeCharmR)
SnakeCharmR v1.0.2 - R and Python Integration
Contribute and submit issues at https://github.com/asieira/SnakeCharmR
> py.assign("a", "áéíóú")
> py.get("a")
[1] "áéíóú"
> Encoding(py.get("a"))
[1] "UTF-8"
  • Improved exception handling, which in SnakeCharmR now includes a full stack trace to help debugging errors that happen in Python:
> library(rPython)
> python.exec("def a():\n    return b()")
> python.exec("def b():\n    raise Exception('uh oh')")
> python.call("a")
Error in python.exec(python.command) : uh oh
> library(SnakeCharmR)
SnakeCharmR v1.0.2 - R and Python Integration
Contribute and submit issues at https://github.com/asieira/SnakeCharmR
> py.exec("def a():\n    return b()")
> py.exec("def b():\n    raise Exception('uh oh')")
> py.call("a")
Error in py.exec(sprintf("_SnakeCharmR_return = json.dumps(%s(%s))", fname,  : 
  Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "<string>", line 3, in a
  File "<string>", line 3, in b
Exception: uh oh

Future Improvements

There are still a few pending issues to work on before SnakeCharmR can be submitted to CRAN, check out GitHub issues and help if you can.

The most relevant gap is testing and ensuring SnakeCharmR works correctly on Windows. If any of you have had experience with compiling Rcpp packages that use external libraries on Windows, your help will be much appreciated.

Many thanks to Bob Rudis for his invaluable help in getting this project going.

Go Top