Home » Using Keras in R: Installing and Debugging

# Using Keras in R: Installing and Debugging

For analysis, I prefer R over Python too. Last year, Tensorflow and Keras were released for R. So why not give it a try? Here’s how to proceed.

## Installation

First step, (1) installing the Anaconda (or Miniconda) distribution on you machine. If you’re not too fond of a bloated Anaconda — Miniconda is just for you. Still not satisfied and prefer a naked Python? You’re not alone in this. However, it will make your life easier in this case. Downloading Miniconda can be done from the official website. In the final step of the installation, you can choose to add conda to the path variables, which I don’t recommend, since Anaconda has its own prompt. I also prefer not having Anaconda as my default Python.

You probably already have R and RStudio on your computer, but you might want to (2) install another version of Rstudio through Anaconda. Once again, not a fan of Anaconda, but trust me on this. Open ‘Anaconda Prompt’ from your start menu (Windows) and type:

conda install rstudio

Once installation is done, run:

conda run rstudio

(3) Installing Keras for R is pretty straightforward. In the R terminal:

install.packages('devtools')
devtools::install_github("rstudio/keras")

The first thing that will happen is that R will ask you if you would like to update a bunch of packages it has found older installations from. If you’ve had a prior installation of TensorFlow or Keras in R, you might run into the following errors:

Error: (converted from warning) cannot remove prior installation of package ‘processx’

Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]): there is no package called ‘processx’

Error: (converted from warning) cannot remove prior installation of package ‘rlang’

This can easily be solved by installing the package ‘processx’, which is a tool to run system processes in the background, according to its documentation. The same goes for ‘rlang’, which is an important package within Tidyverse, which I briefly discussed here.

install.packages('processx')
install.packages('rlang')

Now, you should open the Keras R library and (4) install the Keras distribution with the following commands:

library(keras)
install_keras()

You are now ready to use Keras inside Rstudio. You can follow basic examples here.

Appendix: I see a lot of people running into the following error. Apparently, this happens when you install Keras/Tensorflow for an Rstudio installation that’s not managed by Anaconda. This is the error:

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/r/noarch/repodata.json.bz2>
Elapsed: –

An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your way.

SSLError(MaxRetryError(‘HTTPSConnectionPool(host=\’repo.anaconda.com\’, port=443): Max retries exceeded with url: /pkgs/r/noarch/repodata.json.bz2 (Caused by SSLError(“Can\’t connect to HTTPS URL because the SSL module is not available.”))’))

You can solve this problem by installing OpenSSL through the Anaconda Prompt and retrying the previous commands in R.

conda install openssl

However, you will quickly run into other errors. The conclusion is that installing Keras/Tensorflow in a Miniconda installation or RStudio is really a lot easier. Do not hesitate, at all, to stop trying and simply follow the instructions starting at the top of this page.

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

### Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.