python.call

Share:

Description

Calls Python functions and methods from R

Usage

1
2
  python.call( py.foo, ..., simplify = TRUE, as.is = FALSE )
  python.method.call( py.object, py.method, ... )

Arguments

py.foo

rame of a Python function

py.object

name of a Python object

py.method

name of a method of such object

...

R objects to pass as arguments to the Python function or method

simplify

logical value indicating whether simplification of output should be simplified

as.is

logical value indicating whether length 1 vectors in R should be passed as atomic variables in Python as opposed to length 1 vectors. Note that, e.g., strings such as "hello" in R are vectors of length 1 in R, i.e., "hello" is the same as c("hello"). But Python functions operating on arrays will want to receive the array ["hello"] rather than the literal string "hello".

This argument provides little granularity: it affects either all or none of the arguments of the function. Finer control can be obtained using the I() function as shown in the examples section below.

Details

This function runs a Python function taking as arguments R objects and returning an R object. Some limitations exist as to the nature of the objects that can be passed between R and Python. As of this writing, atomic arguments and vectors are supported.

The user has to be careful to indicate named parameters as required according to Python conventions.

Value

An R representation of the object returned by the call to the Python function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
python.call( "len", 1:3 )
a <- 1:4
b <- 5:8
python.exec( "def concat(a,b): return a+b" )
python.call( "concat", a, b)

python.assign( "a",  "hola hola" )
python.method.call( "a", "split", " " )

## simplification of arguments
a <- 1
b <- 5:8

## Not run: 
python.call("concat", a, b)
## End(Not run)

# using function I()
python.call("concat", I(a), b)

# setting as.is = TRUE
python.call("concat", a, b, as.is = TRUE)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.