How to get started with R-Script integration within Qlik Sense
The architecture of the integration at a High-level looks like this:
Qlik Sense Advanced Analytics integration is essentially an extension to Qlik Sense’s expression syntax, and as such it can be used in both Chart Expressions, and in Load Script Expressions.
With this new capability, we are now able to add syntax to a chart expression that tells Qlik Sense that particular expression should not be evaluated on the Qlik Sense server, but instead, all the information and data needed to calculate that expression should be sent via the server side extension on to the backend R system for calculation.
After the advanced analytic calculations are completed, the data is sent back to the Qlik Sense Server and to the client for visualization.
In order to start displaying a simple "Hello World" in Qlik Sense using a R-Script, we will do the following:
1. Have R & R-studio installed in your system. (RGui included with R for Windows can also be used) R can be downloaded at https://cloud.r-project.org/
2. We need a package in R to extend R functionality to applications via TCP/IP. The package name is "Rserve()" Install the package using the below command in RStudio GUI:
3. Now we need to invoke that library and start Rserve. In order to do so, execute the below scripts:
4.The communication method from Sense to R is taken care using gRPC. R is not a supported language in gRPC by default.
So a possible solution for this is to develop a connector in any of the supported languages of gRPC. Qlik provides an open-source connector developed in C# which in turn access Rserve to be able to run R scripts. qlik-oss/sse-r-plugin Once you built the connector, start the SSEtoRserve.exe (ideally on the Rserve server itself)
Note: Qlik Support does not support this plugin directly. Inquiries should be submitted via GitHub under sse-r-plugin - Issues
The settings.ini file is located in this location:
For Qlik Sense Enterprise/Server:
a. In the QMC, add a new Analytic Connection. b. Restart the Qlik Sense Engine service.
Please refer to the screenshot below for creating a new connection.
Note: If the R-Plugin (SSEtoRserve.exe) was installed on the R-Server (where Rserve runs) or another machine, point to that machine name instead of 'localhost'. Also, in multi-node environments with multiple Qlik Sense Engines, even if the plugin was installed on the Central node, make sure to add the Central node's hostname instead of 'localhost' as the other Rim node Engine services need the correct DNS/Netbios name to reach the plugin.
6. Now Open a Qlik Sense App and add a KPI object in the sheet. This can be one of the Apps included with the plugin itself under <storage path>\sse-r-plugin-master\sense_apps
Note that the example apps also need data connections to be created to the data files included with these apps files in the above location.
7. Otherwise, a new app can be created and any data may be loaded for the SSE example below.
8. For the measure, add the following expression which contains an R-script:
R.ScriptEvalStr('paste(q$firstWord, q$secondWord);', 'Hello' as firstWord, 'World' as secondWord)
9. If everything is configured properly, the R-script shown in bold above should be executed fine and it should display a "Hello World" message.
Understanding the R-script & Function:
R.ScriptEvalStr('paste(q$firstWord, q$secondWord);', Only([First Word]) as firstWord, Only([Second Word]) as secondWord)
Eight script functions are automatically added to the functionality of the plugin. What is needed to be covered on the plugin side to fulfill the functionality is to implement the EvaluateScript rpc function.
Where the Script is an R-Script to be evaluated & Parameter is the data sent from Qlik's end.
Here, we use the ScriptEvalStr function which accepts argument of type String & returns a String. The 'paste' function in R concatenates vectors after converting to character. We pass two data fields of type string from Qlik (First Word & Second Word). The R-script then references these data fields through the q dataframe (structure already taken care in R) (q$firstWord and q$secondWord). The script/function finally returns a String back to Qlik Sense.