The main difference between PythonInR version 0.1-3 and version 2.0-0 is that PythonInR now gives more possibilities to control the type conversions via type hints and supports more data types. Furthermore, I added operator support for PythonInR_Objects which seems especially interesting for the use of SymPy.

Type hints

The motivation behind the type hints is since there is no 1:1 mapping between the R and Python data types, in some situations the automatically inferred type conversions will fail. This makes automatically generated interfaces unuseable. For example assume that you have a Python function which expects an integer vector in the form of a numpy array but gets a list of integers. In this case, the function would fail and this would disagree with the concept of automatically generated interfaces. Although one could argue that in this case the Python function should have been written in a way that it accepts iterables instead of only numpy vectors, one can not assume that all Python functions are written that general and there are other cases e.g. sparse matrices where such a change wouldn’t be possible.

Another motivation for type hints is the use of operators. If one want’s to use virtual Python objects in combination with operators it turns out a more fine-grained type control make things which seemed unfeasible before, quite easy.

Virtual Python objects

Since version 0.1-0 PythonInR supports virtual Python objects. Virtual Python objects are implemented as R6 reference classes and are used to import the functionality of Python objects into R.

How does it work? PythonInR gets the name of a Python object then it iterates over all members of the objects and checks if they are callable or not. If a given member is callable it adds function to the virtual Python object, if it is not callable it adds an active binding. At the end, one ends up with an R6 object which has all the methods and fields of the Python object, but all the function calls are dispatched to Python and all the data is stored in Python. Therefore a virtual Python object in some sense is just a collection of pyGet, pySet and pyCall functions nicely glued together by a reference class.

Translation table

The following table gives an overview of the type conversions performed by PythonInR.

R length Python
Container Type typehint dim Container Type
NULL N=0 None
vector logical N=1 boolean
vector integer int N=1 int
vector integer N=1 long
vector double N=1 float
vector character string N=1 string, bytes
vector character N=1 unicode
vector complex N=1 complex
vector logical N!=1 vector boolean
vector integer int N!=1 vector int
vector integer N!=1 vector long
vector double N!=1 vector float
vector character string N!=1 vector string, bytes
vector character N!=1 vector unicode
vector complex N!=1 vector complex
vector logical vector any vector boolean
vector integer vector, int any vector int
vector integer vector any vector long
vector double vector any vector float
vector character vector, string any vector string, bytes
vector character vector any vector unicode
vector complex vector any vector complex
vector logical list any tlist boolean
vector integer list, int any tlist int
vector integer list any tlist long
vector double list any tlist float
vector character list, string any tlist string, bytes
vector character list any tlist unicode
vector complex list any tlist complex
vector logical tuple any ttuple boolean
vector integer tuple, int any ttuple int
vector integer tuple any ttuple long
vector double tuple any ttuple float
vector character tuple, string any ttuple string, bytes
vector character tuple any ttuple unicode
vector complex tuple any ttuple complex
vector logical numpy (N) ndarray boolean
vector integer numpy, int (N) ndarray int
vector integer numpy (N) ndarray long
vector double numpy (N) ndarray float
vector character numpy, string (N) ndarray string, bytes
vector character numpy (N) ndarray unicode
vector complex numpy (N) ndarray complex
matrix logical any matrix boolean
matrix integer int any matrix int
matrix integer any matrix long
matrix double any matrix float
matrix character string any matrix string, bytes
matrix character any matrix unicode
matrix complex any matrix complex
matrix logical list any list boolean
matrix integer list, int any list int
matrix integer list any list long
matrix double list any list float
matrix character list, string any list string, bytes
matrix character list any list unicode
matrix complex list any list complex
matrix logical numpy (N,N) ndarray boolean
matrix integer numpy, int (N,N) ndarray int
matrix integer numpy (N,N) ndarray long
matrix double numpy (N,N) ndarray float
matrix character numpy, string (N,N) ndarray string, bytes
matrix character numpy (N,N) ndarray unicode
matrix complex numpy (N,N) ndarray complex
matrix logical cvxopt (N,N) cvxopt.matrix boolean
matrix integer cvxopt, int (N,N) cvxopt.matrix int
matrix integer cvxopt (N,N) cvxopt.matrix long
matrix double cvxopt (N,N) cvxopt.matrix float
matrix character cvxopt, string (N,N) cvxopt.matrix string, bytes
matrix character cvxopt (N,N) cvxopt.matrix unicode
matrix complex cvxopt (N,N) cvxopt.matrix complex
array logical (N,N,N,...) array boolean
array integer int (N,N,N,...) array int
array integer (N,N,N,...) array long
array double (N,N,N,...) array float
array character string (N,N,N,...) array string, bytes
array character (N,N,N,...) array unicode
array complex (N,N,N,...) array complex
array logical numpy (N,N,N,...) ndarray boolean
array integer numpy, int (N,N,N,...) ndarray int
array integer numpy (N,N,N,...) ndarray long
array double numpy (N,N,N,...) ndarray float
array character numpy, string (N,N,N,...) ndarray string, bytes
array character numpy (N,N,N,...) ndarray unicode
array complex numpy (N,N,N,...) ndarray complex
list list
list tuple tuple
named list dict
environment dict environment
named list list nlist
data.frame (N,N) data_frame
data.frame dict (N,N) dict
data.frame pandas (N,N) pandas.DataFrame
simple_trimple_matrix logical simple_trimple_matrix boolean
simple_trimple_matrix integer int simple_trimple_matrix int
simple_trimple_matrix integer simple_trimple_matrix long
simple_trimple_matrix double simple_trimple_matrix float
simple_trimple_matrix character string simple_trimple_matrix string, bytes
simple_trimple_matrix character simple_trimple_matrix unicode
simple_trimple_matrix complex simple_trimple_matrix complex
simple_trimple_matrix logical cvxopt cvxopt.spmatrix boolean
simple_trimple_matrix integer cvxopt, int cvxopt.spmatrix int
simple_trimple_matrix integer cvxopt cvxopt.spmatrix long
simple_trimple_matrix double cvxopt cvxopt.spmatrix float
simple_trimple_matrix character cvxopt, string cvxopt.spmatrix string, bytes
simple_trimple_matrix character cvxopt cvxopt.spmatrix unicode
simple_trimple_matrix complex cvxopt cvxopt.spmatrix complex