Test Case Title |
TC7.4 - From R Studio process to MicroService for IOT application, data analytics, machine learning |
Goal |
Create a MicroService starting from R Studio processes, for data analytics, anomaly detection and machine learning. |
Prerequisites |
A simple skill for using R Studio to execute Data Analytics and machine learning algorithms Using a PC or Mobile with a web browser. Conquer a minimal skill on producing R programme if you need to change the program or develop a new one. The following functionalities are available only for specific Snap4city users with specific privileges. |
Expected successful result |
Create a MicroService for IOT app with data analytics, detecting anomalies, creating correlation, predictions, estimate the descriptive statistics, model identification, produce the graphics for data trends, comparing trends, etc. |
Steps |
Please note that to correctly perform this Test Case you need and access to the R Studio Virtual Machine as described below. To have access to a Virtual Machine to perform R Studio please contact snap4city@disit.org. |
From R Studio process to MicroService
- In this test case we see how to create a MicroService starting from R Studio processes, for data analytics, anomaly detection and machine learning.
- The installation of the R-package "plumber" is a necessary requirement.
Plumber package converts the R code into a web API (https://www.rplumber.io/). Each R script contains a relative function: a single specific function is related to a single data analytic application.
Fig: ‘Anomaly detection.R’ script and relative function.
- The R script function presented above is called from the specific R script ‘Stat4cityFunction.R’:
- #' @get /anomaly is the requested annotation by plumber package to create the web API related to the R script. In particular the annotation presented creates a get web API type that can be called by the following link: https://rstudio.snap4city.org/api/anomaly?sensorTypeList=carpark&anomalyDate=2018-05-25
Note that, there is a specific annotation related to each R script (only the functions related to the anomaly detection R script are reported in the example). After the annotation there is the function that is executed with the caller of the rest API.
Fig: ‘Stat4cityFunction.R’ script.
- The R script ‘RunRestApi.R’ has to be executed to activate the call reception and pull-up the service.
Fig: ‘RunRestApi.R’ script to pull-up the service.
- The data analytic results are saved in JSON format: the relative instruction is at the end of the R script related to the data analytic application.
Fig: R instruction to save results in JSON format.
- In the last instruction, the R object to be returned via the API must be returned. Plumber will automatically transform it into a JSON.
- Once the API has been created, it is possible to create the microservice using a service provided by Snap4City inside the ProcessLoader Tool under the Uploaded Resources.
- Click on button “upload new resource”
- And remember the link of the rest api http://rstudio.snap4city.org/api/anomaly?sensorTypeList=carpark&anomalyDate=2018-05-25
- It is possible to fill the form and click Confirm. A zip file containing the desired microservice is created.
- The zip contain a node that can be installed on NodeRed
- With desired configuration:
- And it can be used in simple flow like:
- To get the JSON result
- That can be used in a more advanced flow