Run R Script

Verb: rScript

Available from: <Enterprise>

Runs an "R-language" script.

In order for this command to work properly, the R package version should match the IBM RPA Studio and the operating system's architectures (x64). The platform supports R versions from 2.14.2 to 3.4.4.

Syntax

rScript --script(String)

Inputs

Script Designer Required AcceptedTypes Description
--script Script Required Text, Number, Data Table, ICollection<Text>, ICollection<Number> Script written in the R Language to be executed, in this script the variables of the IBM RPA Studio can be used to set and get data during execution.

Example

Example 1: Uses the Execute Script R command to calculate a salesperson's commissions per product and the store's earnings per sale. The script, when executed, asks for the number of products sold, their prices, and the percentage of the salesperson's commission on these products.

defVar --name quantityText --type String
defVar --name quantity --type Numeric
defVar --name count --type Numeric
defVar --name currentPriceText --type String
defVar --name currentPrice --type Numeric
defVar --name prices --type List --innertype Numeric
defVar --name commissionText --type String
defVar --name commission --type Numeric
defVar --name productsCommision --type List --innertype Numeric
defVar --name earnings --type Numeric
inputBox --title "Products quantity" --prompt "Insert the products quantity" quantityText=value
convertStringToNumber --text "${quantityText}" quantity=value
for --variable ${count} --from 1 --to ${quantity} --step 1
	inputBox --title Price --prompt "Insert the product price" currentPriceText=value
	convertStringToNumber --text "${currentPriceText}" --allowdecimalpoint  currentPrice=value
	add --collection "${prices}" --value "${currentPrice}"
next
inputBox --title Commission --prompt "Enter the percentage of the seller\'s commission. \r\nOnly numbers, example 10 = 10%" commissionText=value
convertStringToNumber --text "${commissionText}" --allowdecimalpoint  commission=value
rScript --script "productsCommision<-c(${prices})*(${commission}/100)\r\nearnings<-sum(productsCommision)\r\n${productsCommision}<-productsCommision\r\n${earnings}<-earnings"
logMessage --message "Earnings: ${earnings}\r\nProducts Commision: ${productsCommision}" --type "Info"

Example 2: Creates a graph that compares the best-selling products of the day, combining both WAL and R languages. WAL connects to the database, filters data, and sets the path to export the graph. R generates the graph using data from the WAL variables and exports the graph to JPEG. The graph is then displayed in the IBM RPA Studio console.

defVar --name dbConnection --type DbConnection
defVar --name sqlTable --type DataTable
defVar --name count --type Numeric
defVar --name product --type String
defVar --name quantity --type Numeric
defVar --name myPicturesPath --type String
defVar --name pictureName --type String
defVar --name myPicturesPathR --type String
defVar --name products --type List --innertype String
defVar --name quantities --type List --innertype Numeric
defVar --name currentDate --type String --value "2021-01-31"
// Get data from the database
sqliteConnect --connectionString "Data Source=\"Caminho do arquivo\";Version=3;UseUTF16Encoding=True;" dbConnection=connection
sqlExecuteReader --connection ${dbConnection} --statement "SELECT * from v_sales \r\nWHERE sales_date = \'${currentDate}\'\r\nORDER BY quantity DESC\r\nLIMIT 5" sqlTable=value
// Retrieves data from the database to use them later with R
for --variable ${count} --from 1 --to ${sqlTable.Rows} --step 1
	mapTableRow --dataTable ${sqlTable} --row ${count} --mappings "name=product=${product},name=quantity=${quantity}"
	add --collection "${products}" --value "${product}"
	add --collection "${quantities}" --value "${quantity}"
next
// Prepares the path and the name of the graph image
getSpecialFolder --folder "MyPictures" myPicturesPath=value
createRandomText --uselowercaseletters  --minimumlength 10 --maximumlength 10 pictureName=value
replaceText --texttoparse "${myPicturesPath}" --textpattern "\\" --replacement "\\\\" myPicturesPathR=value
// Runs the R script that generates the graph, saving it to the previously prepared path
rScript --script "products <- c(${products})\r\nquantities <- c(${quantities})\r\nmyPicturesPathR <- ${myPicturesPathR}\r\npictureName <- ${pictureName}\r\nbarplot(quantities, main=paste(\"Top 5 products sellers in \",${currentDate},sep=\"\"), xlab=\"Products\", ylab=\"Unit quantities\", names.arg=products, col=c(\"#0a5477\",\"#053b54\"), ylim=c(0,70))\r\ndev.copy(jpeg,filename=paste(myPicturesPathR,\"\\\\\",pictureName,\".jpg\",sep=\"\"))\r\ndev.off()"
// The logMessage command displays the path to the graph image
logMessage --message "${myPicturesPath}\\${pictureName}.jpg" --type "Info"

Download File

For the script to work properly, you must download the file and enter its path in the Connection String parameter of the SQLite Connection command

Remarks

It is possible to import packages from the R library as well as install them using this command.