Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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)

Arguments

code

A single Python expression.

convert

Boolean; automatically convert Python objects to R?

Value

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

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.


reticulate

Interface to 'Python'

v1.20
Apache License 2.0
Authors
Kevin Ushey [aut, cre], JJ Allaire [aut], RStudio [cph, fnd], Yuan Tang [aut, cph] (<https://orcid.org/0000-0001-5243-233X>), Dirk Eddelbuettel [ctb, cph], Bryan Lewis [ctb, cph], Sigrid Keydana [ctb], Ryan Hafen [ctb, cph], Marcus Geelnard [ctb, cph] (TinyThread library, http://tinythreadpp.bitsnbites.eu/)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.