Skip to main content

A minimal R API for Python

Project description

Minimal R API for Python

CircleCI Build status pypi

import rapi
from rapi import rcopy, reval


# install released version
pip install -U rapi

# or the development version
pip install -U git+


Why another R interface when there is rpy2?

  1. rapi is 100% python

rapi is primarily used by rtichoke which is an alternate R console. rpy2 was not an option because it requires compilations and who wants to compile!?

  1. rapi is portable

At stated above, rpy2 requires tool chains to install which makes it not portable. rapi on the other hand is lightweight and portable.

  1. rapi is lightweight

rpy2 supports a large number of python and R packages, such as numpy, scipy, ggplot2 etc. But there are situations a user may just want to compute a simple thing from R. Additionally, I found that the interface of rpy2 is not very discoverable.

  1. rapi is a brother of RCall.jl

I am the same developer behind the Julia package RCall.jl which allows Julia to communicate with R. rapi and RCall.jl share a very similar design. For example, rcopy(reval("1")) works for both rapi and RCall.jl.

  1. rapi is compatible with reticulate. Objects can be converted seamlessly between rapi and reticulate. Check the section for reticuate below.


Sometimes, rapi may fail to open the shared library.

  • On Linux

First, try to expose R to PATH.

export R_HOME=/usr/local/lib/R

Note that it should be the path to R_HOME, not the path to the R binary. The folder should contain a file called COPYING. In some cases, you may need to futher specify LD_LIBRARY_PATH,

  • On Windows

Make sure that the path(s) to R.exe and R.dll is in the PATH variable.

Very minimal API

import rapi
from rapi import rcopy, robject, reval, rcall
  • reval - evaluate an R expression in the global environment
a = reval("1:5")
  • rcopy - convert any RObject returned by reval to its python type
b = rcopy(a)
  • robject - convert any python object to its R type
c = robject(b)
  • rcall - call an R function. Python objects are converted to RObjects implicitly.
d = rcall("sum", c)

R Eventloop in IPython

When running interactively in IPython, R events such as showing graphical devices could be handled by the r eventloop. Simply enter in IPython

%gui r


Python Side

# some preparation work
# Unix users may need this PR:
# Windows + Python 2.7 users may need this PR:
# See also

import os
import sys
os.environ["RETICULATE_PYTHON"] = sys.executable

import rapi
from rapi import *

py_object = reval("r_to_py(LETTERS)")

class Foo(object):

foo = Foo()
rcall("r_to_py", robject(foo))

R side

py_run_string("import rapi")
py_run_string("from rapi import *")
r_object = py_eval("reval('LETTERS')")

py_run_string("class Foo(object): pass")
py_run_string("foo = Foo()")
py_object = py_eval("robject(foo)")

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
rapi-0.1.5.tar.gz (23.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page