How to pass each row as an argument to R script from Tableau calculated field

1.6k Views Asked by At

I am trying to do sentiment analysis on a table that I have.

I want each row of string data to be passed to the R script, but the problem is that Tableau is accepting only aggregate data as params for:

SCRIPT_STR(
  'output <- .arg1; output', [comments]
)

This gives me an error message:

# All fields must be aggregate or constant.
2

There are 2 best solutions below

0
On BEST ANSWER

From the Tableau and R Integration documentation:

Given that the SCRIPT_*() functions work as table calculations, they require aggregate measures or Tableau parameters to work properly. Aggregate measures include MIN(), MAX(), ATTR(), SUM(), MEDIAN(), and any table calculations or R measures. If you want to use a specific non-aggregated dimension, it needs to be wrapped in an aggregate function.

In your case you could do:

SCRIPT_STR(
  'output <- .arg1; output', ATTR([comments])
)

ATTR() is a special Tableau aggregate that does the following:

IF MIN([Dimension]) = MAX([Dimension]) THEN 
[Dimension] ELSE * (a special version of Null) END

It’s really useful when building visualizations and you’re not sure of the level of detail of data and what’s being sent

Note: It can be significantly slower than MIN() or MAX() in large data sets, so once you get confident your results are accurate then you can switch to one of the other functions for performance.

0
On

Try MIN([comments]) and make sure you have appropriate dimensions on your viz to partition the data fine enough to get a single comment for each combination of dimensions.