R SaaS

Tutorial: How to publish script in R as a Web Service?

Imagine that you have built a predictive model with R and you would like to predict in real time, whether it is profitable to grant a loan and you wonder how to publish the script written in the R language as a Web Service.

At first glance, this task seems to be complex, however, it turns out to be a piece of cake. With the help of predictive models it is possible to predict occurrence of specific events on the basis of historical data. Predictive models have various applications, yet in this example we will use a model that assesses the probability of loan repayment.

How to deploy a model in R into Scoring.One?

Scoring.One is a tool which enables quick implementation of predictive models, creating decision-making scenarios with extremely high efficiency (processing hundreds of thousands of queries for many predictive models at the same time).

Sign up for free!

In order to use a R model in Scoring.One you need a .zip file containing the:

  1. .rds file with the model
  2. .csv file with variables included in the model. You can create it on your own or by using “Start” button when creating scenario, and then export it to .csv file (do not forget to save regularly edited block!)
  3. .R file containing the variable with the result to be returned by the model; the variable must be named rResult

You can download the ZIP file used in this tutorial here, and read Scoring.One documentation.

Script in R as a Web Service 1

The worst part’s over, and now is the time for sheer pleasure – deploying your model!

Go to the “Scoring Code Management” tab and click on the “Upload new scoring code” button.

Script in R as a Web Service 2

Enter the name for your scoring code and drop the .zip file in the box or click to upload it.

Script in R as a Web Service 3

Creating the scenario in Scoring.One

After saving the code it is time for a scenario. Choose “Scenario” tab and create new one. Drag&drop blocks to the center of the grey area. In this case (although probably like in all cases – the name says it all 🙂 ) we start with the “Start” block.

You do not need to enter the variables, since they have been imported in .zip file. Choose “Scoring Code” and “End” buttons using drag&drop method and connect them in order.

Click on the “Scoring Code” and select the previously uploaded code from which you also import the variables. Save and click “Deploy”. Then, the scenario will be processed and you can select in the “End” block the variables to be returned as a result of the scenario. Then click “Deploy” button once again.

Script in R as a Web Service 4

You can see that creating scenario is like a walk in the park!

Let’s test your scenario: choose “Forms”, find your scenario and enter the data. As a result you will obtain scoring results saved as rResult variable. Now the model has been queried.

Script in R as a Web Service 5

You can add more variables, for example, in order to categorise on the basis of results. To add more variables go back to the scenario and add one more block.

Script in R as a Web Service 6

In “Expressions” you can add a code in R or Groovy. The variables created there will also be returned by the model.

Script in R as a Web Service 7 Script in R as a Web Service 8

If you add the variable “category”, you will get not only the scoring results, but also the category to which its value belongs.

Script in R as a Web Service 9

In the place where the URL should be entered, insert:

https://demo.scoring.one/api/scenario/code/remote/score/?name=NAME_OF_THE_SCENARIO&key=SCORE_TOKEN

 

How to find this mysterious Score Token?

Script in R as a Web Service 10

It is hidden in User Settings 🙂 .

 

As Body of the query enter the variables needed for the model in the JSON form.

Script in R as a Web Service 11

Press “Send” button. And that’s all it takes. You have finished the task!

Script in R as a Web Service 12

Pin It on Pinterest