Tableau and R Interaction
Posted by Diego em Janeiro 15, 2015
This post’s goal is to show how to make tableau and R talk to each other.
In order to do that, you’ll need RServe running. To install it, you can open RStudio (or even just R), install the package from CRAM and then run the command RServe() to start the process:
Once the server is running you can go to tableau and test the connection:
What we are going to do is look at the dbinom R function to calculate the probability of successes on X trials.
The Data Source that I’m using is the least important aspect of this demo, it just connects to an excel spreadsheet with rows numbered from 0 to 30. These are the number of successes we want to calculate the probability to
So when Number if successes = 1 the calculation result is the probability of getting 1 success on X number of trials.
For example, the probability of getting 1 success on 2 trials with 50% probability of success each trial is given by the formula:
> dbinom(x=1,size =2, prob = .5)
Which is 50%. If you toss 2 coins, you can get HH, HT, TH, TT, meaning that in 50% of the times you will get one success (heads being success for example)
Numbers of trials is defined by a parameter that goes from 0 to 30 so, clearly all values on “number of successes” bigger than “number of trials” will be zero after all the probability of getting 5 successes on 4 trials is 0.
The “Probability of Success” indicates how likely it is for ONE test to return positive. It’s set to 50% so we can imagine as if it was the toss of a coin for example.
In order to calculate the result, we’ll create a table calculation and use the SCRIPT_REAL function (because the output is of type REAL) which itself has two parameters:
· The first one is the R function with the and the parameters (x, size and prob) represented by .arg1, .arg2 and .arg3
· The second is the tableau values that will be passes to the parameters
Here’s the calculation:
dbinom(.arg1, size = .arg2, prob = .arg3)
SUM([Number of Sucesses]),
[Number of Trials],
[Probability of Sucess]
We can see it working by dragging X (which I renamed to “number of successes” to rows and our calculation to values (of course, you have to create the two tableau parameters) and it will look like something like this (size 2, probability 50%) :
We can also create a distribution graph. To do that, create a copy of the sheet, right click on number if success, select “continuous” and change the graph type to “line” on the “Show Me” pane; Then, under “Marks” select “Bar”:
I also created a Boolean calculation called “DisplayonGraph” with the formula [Number of Sucesses]<=[Number of Trials] and used it as a filter set it to TRUE (in the graph sheet only) so your graph will reflect exactly the number of trials, otherwise it’d have a huge tail on the right if you inform few trials
Here is the end result:
I’d like to publish it to tableau public but unfortunately it doesn’t support R Scripts so click on the link bellow to download a version of the dashboard.
FYI: I don’t know why but WordPress didn’t allow me to upload tableau files or even compressed files to the website so (please don’t judge me for this) I changed the file extension to .doc.
All you have to do is download the tableau_r.doc and rename it to .twbx (not twb – Its a package workbook)