Tableau's R Integration
Ever wondered how data scientists and data analysts use Tableau for predictive analytics? The ability to integrate R into Tableau is powerful functionality. For those familiar with using R, it can be tricky to get started. Here's how to get started with the R Integration.
Step 1. Set Up R on Your Computer
First, you will need to have a user interface for R on your computer. We recommend R Studio Desktop.
Step 2. Install RServe Package
Next, you will need to install the RServe package. To do this, click on Packages -> Install. Then, type in RServe and it will find the package for you to install.
Step 3. Set Up Rserve Connection
Now you will need to run the following code to start up the Rserve connection:
Step 4. Set Up the External Connection in Tableau
There is one more thing you will need to do prior to writing in R in Tableau, but to do this you will need to switch over to Tableau. Tableau needs to have the external connection set-up in order to run R. Go to the Help -> Settings and Performance -> Manage External Connections.
In the pop-up, type in localhost for the Server name. Click on Test Connection to verify it is now connected.
Step 5. Start Using R Integration
At this point, we can now start taking advantage of the R integration. The integration uses calculated fields to pass R code. There are four different types of calculations used in the R integration:
Which one you use depends on what type of value you expect to get as a result of your R Code. SCRIPT_BOOL would be used if you expected a TRUE/FALSE value returned. SCRIPT_INT would be used if you expected to have an integer returned. SCRIPT_REAL would be used if you expected a numeric value returned. SCRIPT_STR would be used if you expected a string value to be returned.
The basic set-up of any R calculated field is as follows:
Tableau fields being passed in
The R code would be encased by quote marks and the parenthesis would encase both the R code and any Tableau measures/dimensions that will be used inside the R code. You can pass in multiple Tableau fields, you will just need to separate the field names using a comma.
Two important items to know is that inside the R code, you do not use the Tableau field name. You will use .arg and you cannot mix aggregate and non-aggregate arguments. Here is an example below.
Within my R code, I would need to refer to sum([Profit]) as .arg1 and ATTR([Department]) as .arg2. Also, I made Department an Attribute in order to use both it and Profit.
Example of R and Tableau in Action
Now that you have the basics of the calculated field, here's a real life example using the Superstore dataset. We'll be looking at the correlation between Profit and Discount. The returned value will be a numeric value, so I will be using SCRIPT_REAL.
Now, use that field to visualize the correlation coefficient between Customer Segment and Supplier. A value close to -1 indicates a negative linear relationship between the variables. A value to close +1 indicates a positive linear relationship between the variables.
This is just a starter in using the R integration. Hopefully, this will help you get started using this at your own association. If you need help developing predictive models or using R, contact us.