Outline
You have two choices to run Tensor Flow in R:
- Install miniconda with python 3.9 and install conda environment with Tensor Flow package. Then you can share the conda environment with R
- Install Python 3.9 and run install_tensorflow() function in R to create a virtual environment in R with Tensor Flow
Install Miniconda and Create Conda Environment
Check if your computer has existing conda environment or not by going to R console and run the command:
conda_list()
If not, the chance is that your computer does not have Anaconda or Miniconda. You will need to follow this guideline to install and configure Python and Tensor Flow without GPU support: https://www.ecourse.org/news.asp?which=6501, or If your computer comes with standalone NVidia GPU, follow this guideline Install Python and Tensor Flow with GPU to install Miniconda. The guideline provides separate methods for installing Miniconda with Python 3.9 and install isolated Python 3.9. Choose the first method as follows to continue the rest of this section:
- Install miniconda with Python 3.9, and also setup a Tensor Flow virtual environment "r-tensorflow" to run Deep Learning models in R
- Install standalone Python 3.9 without creating conda virtual environment
Note 1: Make sure you follow the guideline and put miniconda binary onto the system path c:\ProgramData\miniconda3\condabin (see below).

Note 2: the above installations also show you how to install Jupyter Notebook and Spyder as well in case we want to run Python code in those GUI environments. Another option for installing Python with Jupyter is to install Anaconda, which includes Python and options to install Jupyter and other packages. I will have another blog to detail the steps.
If we followed the first method, we can share or reuse the same "r-tensorflow" conda virtual environment for R without additional installations. Open R or RStudio (RStudio can also use Tools-->Global Options-->Python menu to select and activate conda environment r-tensorflow), and run the following three commands one by one:
library(reticulate)
conda_list() #see a list of conda environments
use_condaenv("r-tensorflow", required = T) #activate r-tensorflow
library(keras)
After that, you can test Tensor Flow in R by running the following commands to create a deep neural network with an input layer with 3 neurons, the first hidden layer with 64 neurons, the second hidden layer with 128 neurons, then a dropout layer with 50% rate, and the last output layer with one neuron:
model = keras_model_sequential()
layer_dense(model, units = 64, input_shape = 3, activation="sigmoid")
layer_dense(model, units = 128, activation="relu")
layer_dropout(model, rate=0.5)
layer_dense(model, units = 1, activation="sigmoid")
summary(model)
Alternatively, we can also create an input layer and then add layers one by one as outputs, and finally use keras_model() to assemble them together:
inputs = keras_input(shape=c(3))
outputs = inputs %>% layer_dense(units=64, activation="sigmoid")
outputs = outputs %>% layer_dense(units=128, activation="relu")
outputs = outputs %>% layer_dropout(rate = 0.5)
outputs = outputs %>% layer_dense(units=1, activation="sigmoid")
model = keras_model(inputs, outputs)
If there is no error, your installation is done. In the future, we will need to load keras, tensorlfow, and reticulate packages, and link the conda virtual environment with each new session of R if Tensor Flow is going to used:
library(reticulate)
use_condaenv("tf", required=T)
STOP: if the above steps are successful, you can stop here. Otherwise, proceed to the following to install isolated a virtual Tensor Flow environment.
Use Conda Tensor Flow Virtual Environment with GPU Acceleration in R Studio
If you followed https://www.ecourse.org/news.asp?which=6241 and installed either anaconda or miniconda and created an environment with Tensor Flow with GPU support in it, you can then share the same environment with R.
- If you have not done so, add the conda binary source folder to the system Path: assume the binary folder is c:\ProgramData\miniconda3\condabin, download AddCondaPath.zip, extract AddCondaPath.bat file, right click on the batch file to run as administrator.
- Open RStudio and go to Tools --> Global Options ... --> Python --> Select --> Conda Environments to select the one with tensor flow package as the default tensor flow environment to run.
- Load the following libraries: reticulate and keras, and you are ready to use tensor flow with GPU support in R.
Install Virtual Tensor Flow Environment in R
To install Tensor Flow for R, we will have to install Python. You can check if you have Python version 3.9 by going to Command Prompt and type "python --version" in the command line.

If your computer does not have Python,
- go to https://www.python.org/downloads/release/python-390/ and download Python 3.9 for Windows 64-bit
- Double click to install Python and choose Custom Installation for all users. Check
Add python.exe to PATH box.
- Select all optional features in the next screen if you choose customized installation
- Select Install Python 3.9 for all users for customized installation and add Python to environment variables
- Press Install to finish installing Python
Note: the following procedures are tested in early 2024.
Install New Tensor Flow Virtual Environment in R for Windows:
- Check Python installation: Open Windows Command Prompt (type cmd in Windows search Box to find Windows Command Prompt) and type "python --version" to make sure you have Python 3.9 installed.
- Open R to install keras package and run install_keras() command:
install.pakages("keras")
library(keras)
install_keras()
3. Test your installation,
model = keras_model_sequential()
layer_dense(model, units = 64, input_shape = 3, activation="sigmoid")
summary(model)
In case of errors, follow a more complex procedure in the following:
Install Tensor Flow for R in Windows 11 (in case of errors when following prior instructions):
- Open R
- Install Tensor Flow Package by command:
install.packages("keras")
library(keras)
#assume your installed python into C:\Program Files\Python39\
reticulate::use_python("C:\\Program Files\\Python39\\python.exe")
reticulate::py_install("tensorflow==2.10")
reticulate::py_install("keras")
3. Restart R
4. Type commands:
library(keras)
install_keras()
5. Test the installation with commands: type the following commands in R
model = keras_model_sequential()
layer_dense(model, units = 64, input_shape = 3, activation="sigmoid")
summary(model)
Errors and Fixes:
- If you get error messages saying "multiple virtualenv exist or default vitualenv not compatible" or something like that, go to your R working directory (you can find it out by the command getwd() in R), go the directory, delete .virtualenv folder. And then perform the installation as normal.
Install Tensor Flow for R in MacOS
First make sure you have the most recent version of Xcode is installed: Go to terminal windows, and issue the following commands:
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
Then, you can go to R and issue the following commands one by one:
install.packages("remotes")
remotes::install_github("rstudio/tensorflow")
reticulate::install_python()
install.packages("keras")
library(keras)
install_keras()
|