Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Sv translation
languageen

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:

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

    Code Block
    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:

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

    Code Block
    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.

    Image Modified

  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:

Code Block
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.

Code Block
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.

Code Block
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:

Image Modified

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.

 

Sv translation
languagede

Verwendung von R [Original Seitentitel]

In BellaDati ML Studio ist es möglich, R-Bibliotheken und Skripte zu verwenden. Um dies zu erreichen, muss sich BellaDati ML Studio mit der laufenden R-Serve-Instanz verbinden.

Umgebungseinstellungen

Der einfachste Weg, die Umgebung einzurichten, besteht darin, R Studio zu installieren und damit R-Serve zu starten.

  1. Laden Sie R Studio von der offiziellen Seite herunter und installieren Sie es.
  2. Starten Sie R Studio.
  3. Installieren Sie R-Serve mit CRAN. Geben Sie einfach diesen Befehl in die Konsole ein:

    Code Block
    install.packages("Rserve")
  4. Aktivieren Sie die Bibliothek mit diesem Befehl:

    Code Block
    library(Rserve)
  5. Erstellen Sie die Konfigurationsdatei Rserv.cfg im Installationsordner von R-Serve (z. B. C:\Users\username\Dokumente\R\win-library\3.3\Rserve). Fügen Sie folgende Parameter hinzu:

    Code Block
    remote enable
    port 6311
  6. Starten Sie die R-Serve. Im Startbefehl müssen Sie den Pfad zur Konfigurationsdatei angeben:

    Code Block
    Rserve(args="--RS-conf C:\\Users\\username\\DOCUME~1\\R\\WIN-LI~1\\3.3\\Rserve\\Rserv.cfg")
  7. Sobald R-Serve läuft, müssen Sie BellaDati ML Studio konfigurieren. Gehen Sie zu Einstellungen und R-Serve Host (localhost, falls er lokal läuft) und R-Serve Port (angegeben in Rserv.cfg). Speichern Sie die Einstellungen.

    Image Added

  8. Jetzt sind Sie bereit, R in BellaDati ML Studio zu verwenden.

Ausführen von R-Skripten

Um R-Code auszuführen, steht im ML Studio eine spezielle Funktion executeR() zur Verfügung. Das Skript selbst befindet sich zwischen der Klammer, die in Anführungszeichen eingeschlossen ist. Nachfolgend finden Sie ein einfaches Beispiel für das Hinzufügen von zwei Zahlen in R:

Code Block
executeR('toString(2+3)')

Die Funktion toString() wird verwendet, um nur den Wert selbst zu erhalten.

Für längere Skripte ist es besser, sie in der String-Variablen zu speichern und den Code auszuführen, indem man diese Variable als Parameter der Funktion executeR verwendet.

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

 

Es ist auch möglich, Daten aus Dateien zu lesen oder temporäre Tabellen zu erstellen. Um eine Datei lesen zu können, muss sie zunächst außerhalb des R-Codes mit dem Befehl loadFile() in das ML-Skript geladen werden. Dann ist es möglich, wie im Standard-R-Code auf die Datei zuzugreifen, z. B. mit read.table().

 

Die Funktion paste() wird verwendet, um eine Tabelle zu erstellen. Diese Funktion verknüpft eine spezielle Zeichenkette --data--, die verwendet wird, um den Anfang und das Ende der Tabelle sowie den Inhalt der Tabelle selbst zu markieren. Es ist auch möglich, den Header der Tabelle zu definieren, indem man eine neue Zeilenentität \\n und eine kommagetrennte Liste von Spaltennamen nach der ersten Zeichenkette --data-- hinzufügt.

Nachfolgend finden Sie ein Beispiel für das Erstellen einer Tabelle mit vier Spalten. Der Inhalt der Tabelle ist die Summe aller Werte für die ersten vier Spalten einer angehängten Datei.

Code Block
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)

Das Ergebnis des Skripts ist folgendes:

Image Added

The file is compatible with BellaDati 2.8.6.3 and newer. Sie können auch ein Muster-ML-Projekt herunterladen, das alle drei oben genannten Beispiele enthält. Die Datei ist kompatibel mit BellaDati 2.8.6.3 und neuer.