Simple code example demonstrating the use of DeployR as a real-time, R analytics scoring engine.
A more complete example-fraud-score
application can be found here.
This example demonstrates the use of DeployR as a real-time, R analytics scoring engine. The example scenario mimics a real world application where employees at a fictitious bank can request fraud scores for one or more bank account records to help detect fraudulent account activity.
This example is built using the DeployR RBroker Framework, the simplest way to integrate R analytics inside any Java, JavaScript or .NET application. This example consists of two distinct parts:
- Example R Analytics
- Example Application
The final section of this document provides additional details regarding the DeployR integration implemented for this example.
Source: analytics/*
This example uses an R model built to score fictitious bank account data to help uncover fraudulent account activity. The model used is found here:
analytics/fraudModel.rData
The model uses three variables associated with individual bank accounts:
- The number of transactions on the account
- The account balance
- The credit line on the account
This example makes use of a scoring function that uses the model to help determine the likelihood of fraud on a given account based on these data inputs. The scoring function is found here:
analytics/ccFraudScore.R
The R scripts and data models used by this example application are bundled by default within the DeployR 7.3 repository, inside the example-fraud-score directory owned by testuser.
However, if for any reason your DeployR repository does not contain these files you can add them using the DeployR Repository Manager as follows:
- Log in as testuser into the Repository Manager
- Create a new repository directory called example-fraud-score
- Upload analytics/fraudModel.rData to the example-fraud-score directory
- Upload analytics/ccFraudScore.R to the example-fraud-score directory
Source: src/main/java/com/revo/deployr/rbroker/example/FraudScoreExample.java
The example application is implemented as an RBroker Framework Simulation. This is the simplest way to build, test and verify any DeployR integration.
The application works as follows:
- Creates an instance of RBroker, representing a pool of R sessions on the DeployR server.
- Preloads the fraud-score model into the workspace for each R session in the pool.
- Registers asynchronous callback listeners for task completion and error events in the application.
- Uses the Simulation feature to submit and execute fraud-score tasks on behalf of the application.
- Releases the instance of RBroker and exits.
We recommend reading the comments provided within the FraudScoreExample.java
source file for further details.
A Gradle build script is provided to run the example:
build.gradle
By default, the build.gradle
configuration assumes an instance of the DeployR server
is running on localhost
. If your instance of DeployR is running at some
other IP address then please udpate the connection.endpoint
property
in the build.gradle
file as appropriate.
You do not need to install Gradle before running these commands. To run this example application on a Unix based OS, run the following shell script:
gradlew run
To run this example application on a Windows based OS, run the following batch file:
gradlew.bat run
You can observe the console output generated by the application in your terminal window.
By default, the example build configuration defaults to using testuser account credentials when authenticating with the DeployR server. If two or more users intend running this example application at the same time against the same DeployR server instance then:
-
Each user must update the
username
andpassword
properties in their build.gradle configuration file, each indicating the username and password for their own user account. -
The Access Control setting for each of the example R analytics file depencencies in the DeployR repository must be changed from Private access to Shared access.
DeployR-powered applications typically depend on repository-managed R analytics scripts, models and/or data files. See the DeployR Repository Manager for details on how best to manage your own R analytics dependencies.
This example depends on two repository-managed files:
- /testuser/example-fraud-score/fraudModel.rData
- /testuser/example-fraud-score/ccFraudScore.R
Both files, an R model and scoring function respectively, are owned by testuser and can be found in the example-fraud-score repository-managed directory owned by testuser.
These example file dependencies ship, pre-deployed in the DeployR repository so there is no further action for you to take in order for this example to use them. Typically a data scientist
would first develop and then provide these analytics files to an application developer
for integration.
This examples uses the RBroker Framework to integrate DeployR real-time scoring capabilities inside the example server application.
Specifically, this example uses the Pooled Task Runtime provided by the RBroker Framework.
We recommend experimenting with the size of the pool and the number of tasks executed and observe the effects on throughput. Simply update the example-pool-size
and simulate-task-size
properties in the build.gradle
configuration file.
See the following sections of the RBroker Framework tutorial for related details:
Copyright (C) 2010-2015 by Revolution Analytics Inc.
This program is licensed to you under the terms of Version 2.0 of the Apache License. This program is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details.