R & Tableau

I recently needed to run some R code while inspecting a dataset in Tableau, and this post contains some notes & observations on the process of setting it up in OS X.  A lot of these observations are also found in the excellent r-bloggers post linked here.

  1. Install “Rserve” from CRAN
  2. Spin up the server from the R Console usin
Rserve(args='--vanilla')

If you just run “Rserve()” you will get a "Fatal error: you must specify '--save', '--no-save' or '--vanilla'” warning.

Head over to Tableau and go to the Menu bar “Help > Settings and Performance > Manage R Connection…"

Be sure to use the numeric localhost IP Address of “127.0.0.1”.  If everything went OK you should see

From the Tableau docs: "The integration uses so called table calculations to retrieve results from calculations within R."
The basic snippet for a connection is: “SCRIPT_INT( R code, Parameters )" within tableau
Using the IRIS dataset we could have:

SCRIPT_INT('result <- kmeans(x = data.frame(.arg1,.arg2,.arg3,.arg4), 3) result$cluster', SUM([Sepal#Length]),SUM([Sepal#Width]),SUM([Petal#Length]),SUM([Petal#Width]) )

Note that Rserve will continue to run in the background even if you quit the R console.  To stop Rserve, you can use the Activity Monitor (/Applications/Utilities/Activity Monitor.app) to quit the Rserve process.