Process Mining
2021.10
false
Banner background image
Process Mining
Last updated Apr 2, 2024

Example: Creating an R Script

Introduction

This example explains how to interface the UiPath Process Mining platform with external R scripts to implement external data processing.

Installing R

Follow these steps to be able to use R-script in the platform.

Step

Action

1

Download the latest version of the R package from

https://cran.r-project.org/bin/windows/base/.

2

Install R on the server.

Note: this must be the server on which UiPath Process Mining is installed.

3

Locate the installation directory and find path of Rscript.exe.

For example: C:/Apps/Rscript.exe

R is installed on the server, and developers can connect to it with a connection string.

The installation path is needed to create connection strings for an R script.

Important:

Start with some dummy data, to test your workspace setup. For example, use the “Hello World” example as described in Example: Creating a Python Script.

The dummy R script will than contain:

write("Hello world!", stderr()); quit("default", 1)

High-level Overview

In this example an R script is created which clusters cases based on their traces.

Steps

  1. Setting up the Server Settings;
  2. Writing the script.
  3. Setting up the data source;
  4. Setting up a script data source;

Setting up the Server Settings

The generic script datasource requires handlers for all external processes that you want to run.

Follow these steps to add the script handler for R script.

Step

Action

1

Go to the Superadmin Settings tab.

2

Add a field GenericScriptHandlers with as value an object with one key, “r”, which has as value the path to your python executable. For example:

"GenericScriptHandlers": {"r": "C:/Apps/Rscript.exe",}

3

Click on SAVE.

Writing the Script

In your text editor, start a blank text file and enter the following code.

## get command line arguments
args <- commandArgs(trailingOnly=TRUE)
inputfile <- args[1]
## read csv file
input <- file(inputfile, 'r')
df <- read.table(input, header=TRUE, sep=";")
## pre-processing
df <- table(df)
df <- as.data.frame.matrix(df)
df <- df[, sapply(data.frame(df), function(df) c(length(unique(df)))) > 1] #remove columns with unique value 
## cluster
df <- scale(df)
kc <- kmeans(df, centers = 5)
cluster <- kc$cluster
## output
resultdata <- cbind(rownames(df), cluster)
colnames(resultdata)[1] <- 'Case ID'
write.table(resultdata, row.names = FALSE, sep=";", qmethod = "double")## get command line arguments
args <- commandArgs(trailingOnly=TRUE)
inputfile <- args[1]
## read csv file
input <- file(inputfile, 'r')
df <- read.table(input, header=TRUE, sep=";")
## pre-processing
df <- table(df)
df <- as.data.frame.matrix(df)
df <- df[, sapply(data.frame(df), function(df) c(length(unique(df)))) > 1] #remove columns with unique value 
## cluster
df <- scale(df)
kc <- kmeans(df, centers = 5)
cluster <- kc$cluster
## output
resultdata <- cbind(rownames(df), cluster)
colnames(resultdata)[1] <- 'Case ID'
write.table(resultdata, row.names = FALSE, sep=";", qmethod = "double")

Follow the steps below.

Step

Action

1

Save the text file as script.r.

2

Upload the script.r file to your workspace.

Setting up the Data Source

To define input data, create an attribute that generates a .CSV like string. It should be placed in the Globals table since it will serve as input in a table definition.
Note: You can use the csvtable function to define input data.

For this example, we have an application with the an Events table. See illustration below.



Follow these steps to create a lookup expression R_input_data from the Globals table to Events.

Step

Action

1

Open the app in your development environment, and go to the Data tab.

2

Select the Globals table. Right-click on the Globals table in the table item list and select New expression….

3

Set the type to Lookup.

4

Select Events as input table.

5

Enter the following expression:

csvtable( 'CaseID', records.text(Case_ID) , 'Activity', records.text(Activity) )

6

Enter R_input_data in the name field.

7

Click on OK to save the expression attribute in the Globals table.

The expression attribute is created in the Globals table. See illustration below.



Setting up a Script Data Source

Next, set up a datasource table in the application which will call the script.

Follow these steps to set up the script data source.

Step

Action

1

In the Data tab, create a new Connection string table.

2

Rename the New_table to RscriptExample.

3

Right click on the RscriptExample table and click Advanced > Options….

4

In the Table Options dialog, set the Table scope to Workspace.

5

Double click on the RscriptExample table to open the Edit Connection String Table window.

6

Enter the following as Connection string:

``'driver={mvscript

7

Enter the following as Query:

'' +'&scriptFile=' + urlencode("script.r") +'&inputData=' + urlencode(R_input_data)

See illustration below.

8

Click on OK, and click on YES to reload the data.



When loading the data, new attributes are detected. Click on YES(2x) and click on OK.



The Rscript_example table now has two datasource attributes, Case_ID and cluster.

See illustration below.



Defining the R Script in the Query Field

Instead of using a separate file containing the R script, you can also define the R script in the Query field of the Edit Connection String dialog. In this case you use the scriptText parameter in stead of the scriptFile parameter.

See illustration below.



Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.