py_eval: Evaluate a Python Expression

View source: R/python.R

py_evalR Documentation

Evaluate a Python Expression


Evaluate a single Python expression, in a way analogous to the Python eval() built-in function.


py_eval(code, convert = TRUE)



A single Python expression.


Boolean; automatically convert Python objects to R?


The result produced by evaluating code, converted to an R object when convert is set to TRUE.


py_eval() only supports evaluation of 'simple' Python expressions. Other expressions (e.g. assignments) will fail; e.g.

> py_eval("x = 1")
Error in py_eval_impl(code, convert) :
  SyntaxError: invalid syntax (reticulate_eval, line 1)

and this mirrors what one would see in a regular Python interpreter:

>>> eval("x = 1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
x = 1
  SyntaxError: invalid syntax

The py_run_string() method can be used if the evaluation of arbitrary Python code is required.

reticulate documentation built on Oct. 13, 2023, 1:08 a.m.