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

*(This is a copy of a blog post of the same title:* [*https://algolytics.com/tutorial-how-to-publish-script-in-r-as-a-web-service-2/*](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/tutorials/implementation-of-ml-models/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? <a href="#h-how-to-deploy-a-model-in-r-into-scoring-one" id="h-how-to-deploy-a-model-in-r-into-scoring-one"></a>

[Scoring.One](http://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](https://demo.scoring.one/user/registration)!

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](https://algolytics.com/wp-content/uploads/2018/07/Tutorial-How-to-publish-script-in-R-as-a-Web-Service.zip).

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FWOhUh5M6GK1wphna1NLR%2Fimage.png?alt=media&#x26;token=59cfd6af-2220-4ddb-9156-eacdf683adf7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://algolytics.com/wp-content/uploads/2018/07/Script-in-R-as-a-Web-Service-1.png" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FNWC0J8hVBq51eE4WP3lV%2Fimage.png?alt=media&#x26;token=fba57164-b454-4c33-abfc-98c52f847fac" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2Fzl3Saw8MaJzOUdaRxcHm%2Fimage.png?alt=media&#x26;token=1ec61c58-9fef-404e-a6ce-90f89d9a245b" alt=""><figcaption></figcaption></figure>

#### Creating the scenario in Scoring.One <a href="#h-creating-the-scenario-in-scoring-one" id="h-creating-the-scenario-in-scoring-one"></a>

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.

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2F5bjUWLgfKqfrrg3U0XDO%2Fimage.png?alt=media&#x26;token=a473d85b-8dc7-4aa0-a1bc-7c168d86119f" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FPNhKPx4jSvRWAIjcrVEe%2Fimage.png?alt=media&#x26;token=7adeceab-57d3-46dc-b913-dc515e314479" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FrkvGc0AArkqhIK0vUakw%2Fimage.png?alt=media&#x26;token=12b28f37-0ec2-47f3-b34b-d177efd07b6f" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2F2Z0tR3oPRzeiwoSsdeTQ%2Fimage.png?alt=media&#x26;token=53e330ca-814b-4976-93fb-79a48e52490a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FVPvM6VjAIxf14p0P1PFq%2Fimage.png?alt=media&#x26;token=56e435eb-e239-466d-abb7-23ea2425c1f4" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FdnsHcpKdEjkqOMbUeuof%2Fimage.png?alt=media&#x26;token=ba147879-f20e-4478-8ef3-f106c5e9fce1" alt=""><figcaption></figcaption></figure>

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?

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FR2T8JVxsP57vcAbrNzfc%2Fimage.png?alt=media&#x26;token=33a380b3-3dde-434e-a9b0-f2303dbed035" alt=""><figcaption></figcaption></figure>

It is hidden in User Settings 🙂 .

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FsVQkvHpFLmlKB3Qy6GxK%2Fimage.png?alt=media&#x26;token=ba4c175f-1db1-40a0-9d99-9d0753f3f531" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2784301415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZKEXabVqsSUGdA72pv7B%2Fuploads%2FM0Kuq9ccGYPFL20fyskA%2Fimage.png?alt=media&#x26;token=380735f2-260b-49dc-8c8e-f33774495a2f" alt=""><figcaption></figcaption></figure>
