PythonEnv: Python Environment

Description Usage Arguments Methods Examples

Description

The Python Environment Class. Provides an interface to a Python process.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
py = PythonEnv$new(port, path)

py$start()

py$running
py$pid
py$path (py$path <-)
py$host (py$host <-)
py$port (py$port <-)
py$timeout (py$timeout <-)

py$exec(..., file = NULL)
py$set(...)
py$get(varname)

py$stop(force = FALSE)

print(py)

Arguments

port The port to use for communication with Python.

path The path to the Python executable.

varname The name of a Python variable to bring into R.

... Commands to run or named variables to set in the Python process.

file File containing Python code to execute.

force If TRUE, force the Python process to terminate using a system call.

Methods

$new() Initialize a Python interface. The Python process is not started automatically.

$path Set or get the path to the Python executable. The path cannot be changed while the Python process is running.

$host Set or get the host address of the Python connection. Default is 'localhost'. The host cannot be changed while the Python process is running.

$port Set or get the port of the Python interface. The port cannot be changed while the Python process is running.

$timeout Set or get the timeout for receiving messages from Python. Default is 60 seconds.

$start() Start the Python process. The Python process runs asynchronously.

$running Check if the Python process is running.

$pid Get the Process ID of the Python interface. Useful for diagnosing problems.

$set() Set variables in the Python process. R variables are encoded into JSON format, sent to the Python process as text, and decoded into variables on the Python side.

$get() Get a variable from the Python process. Python variables are encoded into JSON format, sent to R as text, and decoded into variables on the R side.

$exec() Execute the specified Python commands and invisibly return printed Python output (if any). Alternatively, the file argument can be used to specify a file containing Python code. Note that there will be no return value unless an explicit Python print statement is executed.

$stop() Stop the Python process by sending a request to the Python process. If force = TRUE, the process will be terminated using a system call instead.

print(py) Show some information about the Python process on the screen, whether it is running and its process id, etc.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Not run: 
pypath = Sys.which('python')
py = PythonEnv$new(path = pypath, port = 6011, host = "127.0.0.1")
py$start()
py$running
py$set(a = 5)
py$get('a')
py$stop(force = TRUE)

## End(Not run)

mkoohafkan/pysockr documentation built on May 6, 2019, 10:37 a.m.