0
votes

I would like to know if it is possible to reference an R calculated field in Tableau, from another R calculated field.

The following simplistic example (which really doesn't require R) illustrates what I'm talking about.

Consider the following workbook schema in Tableau:

Column 1: Date

Column 2: Apple

Column 3: Orange

The data basically shows the monthly prices of apples and orange. Example data could be:

Date, Apple, Orange

1/1/2016, 1.27, 2.32

1/2/2016, 1.22, 2.49

1/3/2016, 1.34, 2.48

And consider the following calculated field in Tableau named price:

SCRIPT_REAL(
"library(xts)
price <- xts( data.frame(c(.arg2), c(.arg3), order.by=as.Date(c(.arg1)) )
"
,MIN([Date]),MIN([Apple]),MIN([Orange])
)

And say I need to have another calculated field named differenceFromLastWeek. Basically it calculates the difference between this month's price for a particular fruit, compared to the previous month's. For example, for the case of Orange, differenceFromLastWeek for 1/2/2016 would be 2.49 - 2.32 = 0.17

SCRIPT_REAL(
"
  differenceFromLastWeek <- xts::diff.xts(x = .arg1, lag = 1, arithmetic = TRUE, log = TRUE)
"
,[price]
)

I have tried the first calculated field (price) and it works. However, I can't seem to reference price from differenceFromLastWeek .

Is that even possible?

1

1 Answers

0
votes

Yes, you can. But R scripts only accept fields wrapped into some kind of aggregation function, i.e. in your example it should be AVG([price]) or similar:

SCRIPT_REAL("your_R_func(.arg1"),AVG([price]))