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
