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.

Environment setup

The easiest way to setup the environment is to install R Studio and use it to start R-Serve.

  1. Download R Studio from official page and install it.
  2. Launch R Studio.
  3. Install R-Serve by using CRAN. Simply type this command to console:

    install.packages("Rserve")
  4. Activate the library by using this command:

    library(Rserve)
  5. 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:

    remote enable
    port 6311
  6. Start R-Serve. In the start command, you have to specify path to the configuration file:

    Rserve(args="--RS-conf C:\\Users\\username\\DOCUME~1\\R\\WIN-LI~1\\3.3\\Rserve\\Rserv.cfg")
  7. 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.

  8. 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:

executeR('toString(2+3)')

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.

String script= 'toString((3+4)*5)'
executeR(script)

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.

String script2= '''
externalData=read.table('DATA_4.03_MNT.csv', sep=',',header=TRUE)
a = "--data--\\nlifetime,broken,pressureInd,moistureInd"
paste(a,toString(colSums(externalData[,c(-5,-6,-7)])),a, sep = "\\n")
'''
loadFile('DATA_4.03_MNT.csv')
executeR(script2)

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 2.8.6.3 and newer.