Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Billpete002
Contributor II
Contributor II

R variable dates Issues

Hey All,

I am very confused at why my script is not working. What I would like to do is remove the hard start and end period encoding (e.g. start =c(2018,1) to using my dates which works in R Script (see below) but not in QlikSense :

QLIKSENSE

R.ScriptEval(
'library(dplyr);'&
'library(forecast);'&
'data = ts(na.omit(q$Measure),start = c(min(q$MDYear),min(q$MDMonth)), end = c(max(q$MDYear)-1,max(q$MDMonth)), frequency = 12);' &
'fit = auto.arima(data);' &
'res = forecast(fit, level = 0.95, h = $(Periods));' &
'resmean <- as.double(res$mean);' &
'results <- append(data, resmean);' &
'results',
Sum(Bookings) as Measure,
YearDate as MDYear,
MonthDate AS MDMonth
)

RSTUDIO

data = ts(na.omit(q$SumBookings),start = c(min(q$YearStart),min(q$MonthStart)), end = c(max(q$YearStart)-1,max(q$MonthStart)), frequency = 12)
fit = auto.arima(data)
res = forecast(fit, level = 0.95, h = 12)
resmean <- as.double(res$mean)
results <- append(data, resmean)
results

There is no material difference between the two and the RSTUDIO version works and the QS version does not. If I remove the variables and hard code 2018,1 for start and 2019, 12 for end it will work and be displayed, but I don't want to manually change this every month!

1 Solution

Accepted Solutions
Billpete002
Contributor II
Contributor II
Author

UPDATE #2 

Found that if I create variables and use sliders I can set the start and end periods!

Just simply create in the Qlik code 4 variables (StartYear, StartMonth, EndYear, EndMonth) and make a slider for each with min/max amounts based on your forecast / data time horizons.

the results can be quite finicky and sometimes the appended results show errors in the historical data but I suppose that is better than not having anything!

 

R.ScriptEval(
'library(dplyr);'&
'library(forecast);'&
'data = ts(na.omit(q$Measure), start = cbind($(StartYear),$(StartMonth)), end = c($(EndYear),$(EndMonth)), frequency = 12);' &
'fit = auto.arima(data);' &
'res = forecast(fit, level = 0.95, h = $(Periods));' &
'resmean <- as.double(res$mean);' &
'results <- append(data, resmean);' &
'results',
COUNT(DISTINCT I_ConfirmationNumber) as Measure
)

View solution in original post

2 Replies
Billpete002
Contributor II
Contributor II
Author

UPDATE: 

I have tried the following thinking maybe it is a formatting issue:

MIN(DISTINCT YearDate)

DATE(MIN(DISTINCT YearDate),'YYYY')

TEXT(MIN(DISTINCT YearDate))

I also loaded an inline table with Year column and 2018 as the value... this too does not work.... This seems to be a pretty big flaw in how Qlik communicates with R Server.

Thoughts? Or has anyone gotten variables dates to work in Qlik R?

 

I will also pause to say that I wish I could see the logs for this - as the server has restricted access.. The error message in QS is pointless as it only states that an argument was invalid. However, when performed in R it works...

 

Billpete002
Contributor II
Contributor II
Author

UPDATE #2 

Found that if I create variables and use sliders I can set the start and end periods!

Just simply create in the Qlik code 4 variables (StartYear, StartMonth, EndYear, EndMonth) and make a slider for each with min/max amounts based on your forecast / data time horizons.

the results can be quite finicky and sometimes the appended results show errors in the historical data but I suppose that is better than not having anything!

 

R.ScriptEval(
'library(dplyr);'&
'library(forecast);'&
'data = ts(na.omit(q$Measure), start = cbind($(StartYear),$(StartMonth)), end = c($(EndYear),$(EndMonth)), frequency = 12);' &
'fit = auto.arima(data);' &
'res = forecast(fit, level = 0.95, h = $(Periods));' &
'resmean <- as.double(res$mean);' &
'results <- append(data, resmean);' &
'results',
COUNT(DISTINCT I_ConfirmationNumber) as Measure
)