Using the R extension in a Qlik Sense application

After our preparation in the previous section, the Qlik Sense box is ready to use R as SSE. Let's carry out a simple test to check if the R installation is running properly:

  1. Open Qlik Sense Desktop (or Enterprise) and create a new application, or open an existing one.
  2. Add a KPI with this simple expression that prints a math result (1+2): R.ScriptEvalStr('print(1+2)').
  3. Verify that the Qlik Sense KPI has an output that looks like this:

If it does not print exactly 3, we have to check the logs so that we have an idea about what went wrong. Close Qlik Sense and check the logs, particularly [yourmachine]_SSE_Engine_[date].log, where there will be useful troubleshooting information.

Let's use a more complex test to check if our scripts are accepting aggregations as parameters. Using the application that we created in Chapter 2Loading Data in Qlik Sense, please add a Key Performance Indicator (KPI) object with the expression Count(OrderDate), which should show 830 as a value:

Let's use this value as a parameter for R and make a simple calculation. Please add another KPI and use the following as an expression:

R.ScriptEval('print(q$value*2)' ,count(CustomerID) as value)

Let's try to understand every part of this expression:

  • R.ScriptEval: We are asking R to process a function that receives a number and returns a number (check the following table for the four functions that the plugin can process)
  • print(q$value*2): An R script that receives a parameter, multiplies it by 2, and then prints the result
  • count(CustomerID) as value: Qlik code that calculates an aggregation (count) and then renames it as a parameter to R 

The following table contains functions:

Function name

Function type

Argument type

Return type

ScriptEval

Scalar, Tensor

Numeric

Numeric

ScriptEvalStr

Scalar, Tensor

String

String

ScriptAggr

Aggregation

Numeric

Numeric

ScriptAggrStr

Aggregation

String

String

 

As you can see, you can now pass any R script as a parameter, provide any value as a parameter, and ask R to calculate the result.

Just out of curiosity, check the SSEtoRserve screen and look at the logs for every interaction. The logs should look like this:

2018-10-29 21:10:09.3160|INFO|SSEtoRserve.RServeEvaluator|EvaluateScript call with hashid(54802698) got Param names: value
2018-10-29 21:10:09.3160|DEBUG|SSEtoRserve.RServeEvaluator|Evaluating R script, hashid (54802698): print(q$value*2)
2018-10-29 21:10:09.3160|INFO|SSEtoRserve.RServeEvaluator|Rserve result: 1 rows, hashid (54802698)
2018-10-29 21:10:09.3160|DEBUG|SSEtoRserve.RServeEvaluator|Took 3 ms, hashid (54802698)

Now, let's look at some more complex use cases of the R integration. Of course, as a user, they can write their own R scripts, but there's an extension that implements a lot of the hard jobs for us. This extension was released on GitHub at https://github.com/mhamano/advanced-analytics-toolbox. Download and install this.

If you have trouble installing Qlik Sense Extensions, please check the documentation at https://help.qlik.com/en-US/sense-developer/September2018/Subsystems/Extensions/Content/Sense_Extensions/Howtos/deploy-extensions.htm.

After installing the extension, we can drag and drop it from the Custom objects, as follows:

Now, we're presented with a set of scripts of data science that are ready to be used with the R environment:

There is a very useful algorithm for a forecast called Autoregressive Integrated Moving Average (ARIMA). Deeper knowledge about this is beyond the scope of the book, but you can learn more about it here at https://datascienceplus.com/forecasting-with-arima-part-i/. Locate this under Time series analysis, as shown in the following screenshot:

We have to provide which dimension we want to analyze (time series) and the variable (measure). As an example, we can use OrderDate.autoCalendar.Week as the dimension and Sum(SalesAmmount) as the measure.

After we have filled in these parameters, we will see a graph like the following:

The blue line (light gray) is the observed value for the measure (the actual values of that week), and we can see the following other three values:

  • Fit: The forecast value
  • Upper: Upper limit according to confidence interval
  • Lower: Lower limit according to confidence interval

We can set a lot of parameters (including Confidence level) at the Analysis Settings sections of our object settings, as depicted in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset