In BellaDati ML Studio it is possible to use R libraries and scripts. To be able to do that, BellaDati ML Studio needs to connect to running R-Serve instance.
The easiest way to setup the environment is to install R Studio and use it to start R-Serve.
- Download R Studio from official page and install it.
- Launch R Studio.
Install R-Serve by using CRAN. Simply type this command to console:
Activate the library by using this command:
Create the configuration file Rserv.cfg in the installation folder of R-Serve (e. g. C:\Users\username\Documents\R\win-library\3.3\Rserve). Add following parameters:
Start R-Serve. In the start command, you have to specify path to the configuration file:
Once R-Serve is running, you have to configure BellaDati ML Studio. Go to Settings and R-serve host (localhost in case it is running locally) and R-serve port (specified in Rserv.cfg). Save the settings.
Now you are ready to start using R in BellaDati ML Studio.
Executing R Scripts
To execute R code, a special function executeR() is available in ML Studio. The script itself is placed between the parenthesis, enclosed in quotes. Below you can find simple example of adding two numbers in R:
Function toString() is used to get only the value itself.
For longer script, it is better to store them inside String variable and the execute the code by using this variable as parameter of the function executeR.
It is also possible to read data from files or create temporary tables. To be able to read a file, first it has to be loaded to the ML script outside of the R code by using loadFile() function. Then it is possible to access the file as in standard R code, e. g. by using read.table().
Function paste() is used to create a table. This functions concatenates a special string --data--, which is used to markup the start and end of the table, and the content of the table itself. It is also possible define the header of the table, by adding a new line entity \\n and comma-separated list of column names after the first --data-- string.
Below you can find an example of creating a table with four columns. The content of the table is sum of all values for first four columns of an attached file.
The result of the script is following:
You can also download sample ML project containing all three examples mentioned above. The file is compatible with BellaDati 188.8.131.52 and newer.