liveBook · Manning

Reticulate

As explained in Deep Learning with R, Third Edition

reticulate is an R package that provides an interface for calling Python code and managing Python interoperability from R.

reticulate connects R to Python by loading a full Python interpreter inside an R session. It lets R code call Python modules directly, including NumPy, TensorFlow, PyTorch, and the wider Python ecosystem. section 2.2 section 3.4.1

Use reticulate to move between R and Python-oriented numerical workflows:

  • Import a Python module with import(), such as np <- import("numpy") or torch <- import("torch"). section 2.2 section 3.4.1
  • Let R arrays pass automatically to Keras, TensorFlow, PyTorch, or Python functions; reticulate generally converts them efficiently without copying data. section 2.2
  • Control conversion explicitly with np_array(), r_to_py(), as.array(), or py_to_r() when a Python or R representation is preferable. section 2.2
  • Set convert = FALSE when importing NumPy to keep NumPy arrays, then convert results to R when needed. section 2.2

Calling NumPy from R

Import NumPy normally when an R result is convenient: np <- import("numpy"); np$sum(1:3) returns 6. Import with convert = FALSE to retain Python objects: np <- import("numpy", convert = FALSE); np$sum(1:3) |> np$square() |> py_to_r() returns 36.

Accessing PyTorch

With keras3, select the torch backend and import the Python package: library(keras3); use_backend("torch"); torch <- import("torch"). The Python package is named torch, distinct from the R package named torch. section 3.4.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains interpreter access, automatic and explicit array conversion, and module imports, but stops short of general environment administration.
Examples
Demonstrates NumPy sums and squaring, explicit conversion, and importing PyTorch through the Keras torch backend.
What you can do after
Apply it via reticulate to import Python modules, call their functions, and manage R–Python array representations.
How it connects
Connects R arrays with NumPy, TensorFlow, PyTorch, JAX, Keras, and Python modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.