Learn R Programming

reticulate (version 1.38.0)

py_eval: Evaluate a Python Expression

Description

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

Usage

py_eval(code, convert = TRUE)

Value

The result produced by evaluating code, converted to an R

object when convert is set to TRUE.

Arguments

code

A single Python expression.

convert

Boolean; automatically convert Python objects to R?

Caveats

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.