Setting Up TensorFlow in RStudio on macOS: A Step-by-Step Guide
1) Install Anaconda
Download Anaconda: Visit the Anaconda Distribution page and download the installer suitable for macOS. Install Anaconda: Run the downloaded installer and follow the on-screen instructions to complete the installation.
2) Open Terminal
3) Create a Conda Environment
conda create --name rstudio-tf
4) Activate the Environment
conda activate rstudio-tf
5) Install Python
conda install python=3.11 # use supported version for tensorflow
6) Install tensorflow
conda install -c anaconda tensorflow
7) Configure R to Use the Conda Environment
Inform R about the specific Python environment to ensure seamless integration. Edit the .Renviron File: This file is typically located in your home directory. If it doesn’t exist, you can create it.
echo "RETICULATE_PYTHON=~/miniforge3/envs/rstudio-tf/bin/python" >> ~/.Renviron
8) Verify TensorFlow Installation
conda list | grep tensorflow
9) Go to RStudio and Restart the R Session
To ensure RStudio recognizes the new environment, Navigate to Session -> Restart R within RStudio.
10) Initialize and Test TensorFlow in R
Run the following R script to verify the setup::
reticulate::use_python("/opt/anaconda3/envs/rstudio-tf/bin/python")
reticulate::use_condaenv("rstudio-tf", conda="/opt/anaconda3/bin/conda", required = TRUE)
library(tensorflow)
tf_config()
tf_version()
tf$config$list_logical_devices()
Following these steps, you should have a functional TensorFlow setup within RStudio on your macOS system. Ensure all paths are correctly specified, and you use compatible versions of Python, TensorFlow, and R packages.