9952 explained code solutions for 126 technologies

python-kerasHow can I install the python module tensorflow.keras in R?

Installing the python module tensorflow.keras in R can be done using the reticulate package. reticulate is an R package that allows for interoperability between R and Python.

The following example code can be used to install the tensorflow.keras module:

if (!requireNamespace("reticulate", quietly = TRUE)) {


The output of this code will look something like this:

Collecting tensorflow.keras
  Downloading tensorflow_keras-2.4.0-py3-none-any.whl (36 kB)
Collecting h5py
  Downloading h5py-2.10.0-cp37-cp37m-win_amd64.whl (2.9 MB)
  |████████████████████████████████| 2.9 MB 2.2 MB/s
Collecting pyyaml
  Downloading PyYAML-5.3.1-cp37-cp37m-win_amd64.whl (223 kB)
  |████████████████████████████████| 223 kB 2.1 MB/s
Collecting six>=1.12.0
  Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting numpy>=1.13.3
  Downloading numpy-1.19.1-cp37-cp37m-win_amd64.whl (12.9 MB)
  |████████████████████████████████| 12.9 MB 2.2 MB/s
Installing collected packages: six, numpy, h5py, pyyaml, tensorflow.keras
Successfully installed h5py-2.10.0 numpy-1.19.1 pyyaml-5.3.1 six-1.15.0 tensorflow.keras-2.4.0

The code above has several parts:

  1. The first line checks to see if the reticulate package is installed. If it is not, it will install the package.
  2. The second line uses the py_install function from the reticulate package to install the tensorflow.keras module.

Additional resources:

  1. Reticulate documentation
  2. TensorFlow Keras installation guide

Edit this code on GitHub