Hack-R Hack-R - 1 month ago 14
R Question

How to use table data as a parameter in Tableau's R integration?

I would like to know how to use table data as a parameter in Tableau's R integration.

Example

Tableau has a builtin data set called "Superstore" used for reproducible examples. Suppose I've used the Superstore data set to create a "text table" (i.e. spreadsheet) with

Region
as rows and
SUM(Profit)
as the data.

That looks like this:

enter image description here

Now, suppose I wanted to pass the data in this table to R for a calculation. I would start Rserve

library(Rserve)
Rserve()


and establish a connection with Tableau's UI.

Next I would want to create a calculated field to send the data to R and retrieve the results. I'm not sure how to do this.

My attempt looks like this:

SCRIPT_REAL('
output <- anRFunction(.arg1)
',
[someTableauMeasure])


which should be fine, except that I don't know how to represent the table data where it currently says
someTableauMeasure
. This is just an arbitrary example, but a reason I might want to do this is that I might provide the user with a filter, such as
Country
, so that they could filter the results at will and get an updated result from R.

For testing purposes that function
anRFunction
could be replaced with something like
abs
.

Answer

Tableau will pass the aggregated values to R, depending on the settings of your worksheet.

So in your case if you use:

SCRIPT_REAL('
    output <- anRFunction(.arg1)
',
sum(Profit))

You will get the output according to the dimensions you have on your worksheet, in your case [Region] if you set up a filter by country, R will only receive and return the values for a certain country and if you choose to use [Category] instead, you will get the results of your R function broken down by category.