Qlik Community

Qlik Server Side Extensions Discussions

Discussion Board for collaboration on Server-Side Extensions and Advanced Analytics Integration.

oguchihyn
New Contributor III

KPI measure off R.ScriptEval() as Expression

I need to grab a value from R using R.ScriptEval() for KPI measure. The R code works and produces a discrete value. The Expression is "OK" ; but the KPI produces null (nothing, just a dash). Yes, Rserve and SSEtoRServe are both on.

Any help is appreciated.

Here's the Expression I am using:

R.ScriptEval('library(TTR);library(forecast);newdata <-read.csv("sldsmp.csv");frcst <-forecast(ts(q$newdata$a1ccnt));frcst1 <-as.data.frame(q$frcst[2]);frcst2 <-round(q$frcst1[1,],0);q$frcst2')

Here's the R code:

library(TTR)

library(forecast)

newdata <-read.csv("C:/<...>/Documents/Qlik/sldsmp.csv")

frcst <-forecast(ts(newdata$a1ccnt))

frcst1 <-as.data.frame(frcst[2])

frcst2 <-round(frcst1[1,],0)

P.S:

  • removing "q$" in all but first instance does not make a difference.

Here's the .csv file I am referencing:

See attachment.

Thank you!

Oguchi

1 Solution

Accepted Solutions
Highlighted
tln
New Contributor II

Re: KPI measure off R.ScriptEval() as Expression

If you load the csv into qlik in the load script by the following:

LOAD

    "Month",

    crvcnt,

    htncnt,

    clrcnt,

    a1ccnt,

    trnd

FROM [lib://mycsv/sldsmp.csv]

(txt, codepage is 28591, embedded labels, delimiter is ',', msq);

In the KPI object you set the following expression, then it works:

R.ScriptAggr('library(TTR);

library(forecast);

frcst <- forecast(ts(q$a1ccnt));

frcst1 <- as.data.frame(frcst[2]);

frcst2 <- round(frcst1[1,],0);'

, a1ccnt, Month)

4 Replies
bvk
Contributor II

Re: KPI measure off R.ScriptEval() as Expression

Hi Oruchi,

The ScriptEval function takes a vector as input and returns a vector. This may not be what you want in a KPI object. Have a look here: sse-r-plugin/GetStarted.md at master · qlik-oss/sse-r-plugin · GitHub

I think for a KPI object it would be better to use the ScriptAggr function.

Another suggestion would be to load the data in the CSV into your qlik datamodel and then pass the values as a q$ parameter to your forcast call.

So it would look something like

R.ScriptAggr('library(TTR);library(forecast);frcst <-forecast(ts(q$newdata,$a1ccnt));frcst1 <-as.data.frame(q$frcst[2]);frcst2 <-round(q$frcst1[1,],0);q$frcst2', data as newdata);

Regards,

Bas.

oguchihyn
New Contributor III

Re: KPI measure off R.ScriptEval() as Expression

Hello, Bas:

Thanks for helping, but this did not work for me (yes, my csv file is script-loaded) because

<...data as newdata> at the end of the expression  triggers the Error, "Bad field name: data"

Highlighted
tln
New Contributor II

Re: KPI measure off R.ScriptEval() as Expression

If you load the csv into qlik in the load script by the following:

LOAD

    "Month",

    crvcnt,

    htncnt,

    clrcnt,

    a1ccnt,

    trnd

FROM [lib://mycsv/sldsmp.csv]

(txt, codepage is 28591, embedded labels, delimiter is ',', msq);

In the KPI object you set the following expression, then it works:

R.ScriptAggr('library(TTR);

library(forecast);

frcst <- forecast(ts(q$a1ccnt));

frcst1 <- as.data.frame(frcst[2]);

frcst2 <- round(frcst1[1,],0);'

, a1ccnt, Month)

oguchihyn
New Contributor III

Re: KPI measure off R.ScriptEval() as Expression

I appreciate your help, @Tobias: this is the correct solution.

Worked perfect!

Community Browser