Learn R Programming

PythonInR

More documenation can be found at https://bitbucket.org/Floooo/pythoninr.

Dependencies

Python >= 2.7.0 R >= 2.15.0

R-packages:

  • pack

Linux

Python headers

On Debian and Debian-based Linux distributions (including Ubuntu and other derivatives) the "Python Development Headers" can be installed by typing the following into the terminal.

    apt-get install python-dev

For installation on Red Hat Enterprise Linux , Fedora, and other Red Hat Linux-based distributions, use the following:

    yum install python-devel

Windows

There are no additional dependencies on Windows. (One obviously needs to have R and Python installed.)

Installation

    install.packages("PythonInR")
#   or via devtools
    require(devtools)
    install_bitbucket("Floooo/PythonInR")

Windows Setup

Since the Windows version of PythonInR uses explicit linkage one can switch between different Python versions without recompiling the package. This flexibility comes at the price of additional configuration at the startup. Which results in a different behavior for the static (Linux default) and the explicit linked (Windows default) version. Where as the static linked version automatically connects, when the package get’s loaded, the explicitly linked version needs to be connected manually.

To enable automatic connection for the explicitly linked version the environment variable PYTHON_EXE has to be set. You can put your Python path into your .Renviron or .Rprofile file (Setting up a .Renviron file).

NOTES

Python 3

Due to api changes in Python 3 the function execfile is no longer available. The PythonInR package provides a execfile function following the typical workaround.

def execfile(filename):
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), globals())

Type Casting

R to Python (pySet)

To allow a nearly one to one conversion from R to Python, PythonInR provides Python classes for vectors, matrices and data.frames which allow an easy conversion from R to Python and back. The names of the classes are PrVector, PrMatrix and PrDataFrame.

Default Conversion

Rlength (n)Python
NULLNone
logical1boolean
integer1integer
numeric1double
character1unicode
logicaln > 1PrVector
integern > 1PrVector
numericn > 1PrVector
charactern > 1PrVector
list without namesn > 0list
list with namesn > 0dict
matrixn > 0PrMatrix
data.framen > 0PrDataFrame

Change the predefined conversion of pySet

PythonInR is designed in way that the conversion of types can easily be added or changed. This is done by utilizing polymorphism: if pySet is called, pySet calls pySetPoly which can be easily modified by the user. The following example shows how pySetPoly can be used to modify the behavior of pySet on the example of integer vectors.

The predefined type casting for integer vectors at an R level looks like the following:

setMethod("pySetPoly", signature(key="character", value = "integer"),
          function(key, value){
    success <- pySetSimple(key, list(vector=unname(value), names=names(value), rClass=class(value)))
    cmd <- sprintf("%s = PythonInR.prVector(%s['vector'], %s['names'], %s['rClass'])", 
                   key, key, key, key)
    pyExec(cmd)
})

To change the predefined behavior one can simply use setMethod again.

pySetPoly <- PythonInR:::pySetPoly
showMethods("pySetPoly")

pySet("x", 1:3)
pyPrint(x)
pyType("x")

setMethod("pySetPoly",
          signature(key="character", value = "integer"),
          function(key, value){
    PythonInR:::pySetSimple(key, value)
})

pySet("x", 1:3)
pyPrint(x)
pyType("x")

NOTE PythonInR:::pySetSimple
The functions pySetSimple and pySetPoly shouldn't be used outside the function pySet since they do not check if R is connected to Python. If R is not connected to Python this can yield to segfault !

NOTE (named lists):
When executing pySet("x", list(b=3, a=2)) and pyGet("x") the order of the elements in x will change. This is not a special behavior of PythonInR but the default behavior of Python for dictionaries.

NOTE (matrix):
Matrices are either transformed to an object of the class PrMatrix or to an numpy array (if the option useNumpy is set to TRUE).

NOTE (data.frame):
Data frames are either transformed to an object of the class PrDataFrame
or to a pandas DataFrame (if the option usePandas is set to TRUE).

R to Python (pyGet)

PythonRsimplify
NoneNULLTRUE / FALSE
booleanlogicalTRUE / FALSE
integerintegerTRUE / FALSE
doublenumericTRUE / FALSE
stringcharacterTRUE / FALSE
unicodecharacterTRUE / FALSE
bytescharacterTRUE / FALSE
tuplelistFALSE
tuplelist or vectorTRUE
listlistFALSE
listlist or vectorTRUE
dictnamed listFALSE
dictnamed list or vectorTRUE
PrVetorvectorTRUE / FALSE
PrMatrixmatrixTRUE
PrDataFramedata.frameTRUE

Change the predefined conversion of pyGet

Similar to pySet the behavior of pyGet can be changed by utilizing pyGetPoly. The predefined version of pyGetPoly for an object of class PrMatrix looks like the following:

setMethod("pyGetPoly", signature(key="character", autoTypecast = "logical", simplify = "logical", pyClass = "PrMatrix"),
          function(key, autoTypecast, simplify, pyClass){
    x <- pyExecg(sprintf("x = %s.toDict()", key), autoTypecast = autoTypecast, simplify = simplify)[['x']]
    M <- do.call(rbind, x[['matrix']])
    rownames(M) <- x[['rownames']]
    colnames(M) <- x[['colnames']]
    return(M)
})

For objects of type "type" no conversion is defined. Therefore, PythonInR doesn't know how to transform it into an R object so it will return a PythonInR_Object. This is kind of a nice example since the return value of type(x) is a function therefore PythonInR will return an object of type pyFunction.

pyGet("type(list())")

One can define a new function to get elements of type "type" as follows.

pyGetPoly <- PythonInR:::pyGetPoly
setClass("type")
setMethod("pyGetPoly", signature(key="character", autoTypecast = "logical", simplify = "logical", pyClass = "type"),
          function(key, autoTypecast, simplify, pyClass){
    pyExecg(sprintf("x = %s.__name__", key))[['x']]
})
pyGet("type(list())")

NOTE pyGetPoly
The functions pyGetPoly should not be used outside the function pyGet since it does not check if R is connected to Python. If R is not connected to Python this will yield to segfault !

NOTE (bytes):
In short, in Python 3 the data type string was replaced by the data type bytes.

Cheat Sheet

CommandShort DescriptionExample Usage
BEGIN.PythonStart a Python read-eval-print loopBEGIN.Python() print("Hello" + " " + "R!") END.Python
pyAttachAttach a Python object to an R environmentpyAttach("os.getcwd", .GlobalEnv)
pyCallCall a callable Python objectpyCall("pow", list(2,3), namespace="math")
pyConnectConnect R to PythonpyConnect()
pyDictCreate a representation of a Python dict in RmyNewDict = pyDict('myNewDict', list(p=2, y=9, r=1))
pyDirThe Python function dir (similar to ls)pyDir()
pyExecExecute Python codepyExec('some_python_code = "executed"')
pyExecfileExecute a file (like source)pyExecfile("myPythonFile.py")
pyExecgExecute Python code and get all assigned variablespyExecg('some_python_code = "executed"')
pyExecpExecute and print Python CodepyExecp('"Hello" + " " + "R!"')
pyExitClose PythonpyExit()
pyFunctionCreate a representation of a Python function in RpyFunction(key)
pyGetGet a Python variablepyGet('myPythonVariable')
pyGet0Get a Python variablepyGet0('myPythonVariable')
pyHelpPython helppyHelp("help")
pyImportImport a Python modulepyImport("numpy", "np")
pyIsConnectedCheck if R is connected to PythonpyIsConnected()
pyListCreate a representation of a Python list in RpyList(key)
pyObjectCreate a representation of a Python object in RpyObject(key)
pyOptionsA function to get and set some package optionspyOptions("numpyAlias", "np")
pyPrintPrint a Python variable from within RpyPrint("somePythonVariable")
pySetSet a R variable in PythonpySet("pi", pi)
pySourceA modified BEGIN.Python aware version of sourcepySource("myFile.R")
pyTupleCreate a representation of a Python tuple in RpyTuple(key)
pyTypeGet the type of a Python variablepyType("sys")
pyVersionReturns the version of PythonpyVersion()

Usage Examples

Dynamic Documents

Data and Text Mining

Copy Link

Version

Install

install.packages('PythonInR')

Monthly Downloads

116

Version

0.1-12

License

GPL-3

Last Published

June 21st, 2020

Functions in PythonInR (0.1-12)

pyExecg

Executes multiple lines of python code and gets the output
pyIsConnected

checks if R is connected to Python
pyTuple

Creates a virtual Python tuple
pyImport

Import virtual Python objects to R
pySet

assigns R objects to Python
pyExecp

Executes a single line of Python code from within R
pyGet

Gets Python objects by name and transforms them into R objects
pyFunction

creates a virtual Python function
pyOptions

Options for the PythonInR package
pyExit

closes the connection to Python
pyList

Creates a virtual Python list
pyObject

Creates a virtual Python object
pyType

Convenience function to call the Python function type
pyPrint

Convenience function to print a given Python object to the R terminal
pyHelp

Convenience function to access the Python help system
pyGet0

Creates an R representation of an Python object
pyVersion

is a convenience function to get sys.version from Python
pyDict

Create a virtual Python dictionary
BEGIN.Python

Execute Python interactively from within R
pyConnect

connects R to Python
pyExec

Executes multiple lines of Python code from within R
pyDir

Convenience function to call the Python function dir
pyExecfile

Executes Python source file from within R
autodetectPython

Autodetects the settings for Windows
pyAttach

Attach Python objects to R
pyCall

Call a callable Python object from within R