Digit recognition neural networks in R

Interpreting images has been a popular use case in the field of artificial intelligence (AI), and identification of handwritten digits using neural networks is commonly used in mobile applications.

In this tutorial, learn how to create a web application to recognize handwritten digits using neural networks on R in Watson Studio. The tutorial uses the MNIST database, which is a large database of handwritten digits that is commonly used in machine learning.


The following prerequisites are required to run the tutorial:

Estimated time

It should take you approximately 45 minutes to complete this tutorial.


  1. Create your IBM Cloud account and access IBM Cloud Pak for Data as a Service.
  2. Create a new project.
  3. Launch the RStudio environment IDE.
  4. Add the files to your project.

Create IBM Cloud account and access IBM Cloud Pak for Data as a Service

  1. Sign in to IBM Cloud.
  2. Search for IBM Watson Studio.

    Searching for Watson Studio

  3. Create the service by selecting the region and pricing plan, then click Create.

    Creating service

Create a new project

  1. Start the Watson Studio service.

    Creating a project

  2. Click Create a project, then Create an empty project.

    Creating an empty project

  3. Name your project and add a storage service. Click Create.

    Defining project details

Launch RStudio environment IDE

  1. Go to Assets, click Launch IDE.

    Navigating to assets

  2. Select RStudio.

    Selecting RStudio

  3. Select Default RStudio XS (2 vCPU and 8 GB RAM) for the runtime, and click Launch.

    Setting up RStudio environment

    The RStudio window opens.

    Launching RStudio environment in Watson Studio

Add the files to your project

  1. Select Files -> New Folder on the right side of the window, and name the folder.

    Create a new folder

  2. Open the newly created folder, and upload the zip folder of the GitHub Repository.

    Upload files Upload zip folder

  3. Click More, and select Set As Working Directory.

    Setting a working directory R Console

  4. Double-click and open the neuralNetwork.R file from the uploaded files.

    Neural Network

  5. Click Run, and execute each line individually. Wait for the arrow to appear before clicking Run to execute the next line.

    R Script run R Script console

  6. After training the neural network, you can run the web application to recognize handwritten digits. However, you must install some packages first. Enter the following commands in the console window.

    Installing shiny package:


    Installing nnet package:


    Installing EBImage package:

  7. Open the DigitRecognizer.R file from the uploaded files by double-clicking the file. Click Run App to run the web app. You can upload the sample images from the Sample-Images folder in the GitHub Repository that you downloaded earlier on your computer to test the application.

    Web application Digit recognizer


In this tutorial, you learned how to create and visualize a neural network that takes in a data set and trains a model to predict outcomes.