PythonInterface-class: An Interface to Python

Description Details Methods


The "PythonInterface" class provides an evaluator for computations in Python, following the structure in the XR package. Proxy functions and classes allow use of the interface with no explicit reference to the evaluator. The function RPython() returns an evaluator object.


The class extends the "Interface" class in the XR package and has the same fields. Python-specific methods use the rPython low-level interface. See the Chapter from the “Extending R” book in the documents for this package for details.


Define(text, file)

Define a Python function from a character vector, 'text' or by reading the text from a file via readLines(). Character vectors are taken to represent lines of Python code in a function definiition. The method returns a proxy function with a name inferred from the first line of the text.

Import(module, ...)

The Python version of this method replaces the general version in XR with the "import" or "from ... import" directives in Python as appropriate. Returns the 'reticulate' version of the module object, which can be used directly.


The Python version, with special defaults for prototypeObject and modules


A low-level command execution, needed for initializing. Normally should not be used by applications since it does no error checking; use $Command() instead.

ServerAddToPath(serverDirectory, serverPos)

The Python version using sys.path.append()

ServerClassDef(Class, module = "", example = TRUE)

The Python version using PythonClassDef()

ServerEval(strings, key = "", get = NA)

The Python version using value_for_R()

ServerFunctionDef(name, module = "")

The Python version using PythonFunction()


The Python version using del_for_R())

ServerSerialize(key, file)

Serializing and unserializing in the Python interface use the pickle structure in Python. Serialization does not rely on the R equivalent object.

ServerUnserialize(file, all = FALSE)

The Python unserialize using unpickle

Shell(endCode = "exit", prompt = "Py>: ", cont = "Py+: ")

Starts an interactive Python shell. Each line of input must be a complete Python expression or command, which will be evaluated in the same context as $Eval() expressions. To continue over multiple lines, end all but the last with an unescaped backslash. The argument 'endCode' is the string to type to leave the shell, by default "exit".


The $Source() method uses the Python function execfile() and therefore is quite efficient.

XRPython documentation built on Nov. 17, 2017, 7:09 a.m.