# Why do we need R - Tableau Integration?

Updated: Sep 6, 2019

During my Tableau training sessions, many participants asked me about R - Tableau Integration and it's applications. The first and most important question was

Why do we need R - Tableau integration?

In this blog, I am trying to address some of the questions asked by these participants

**What is R?**

R is a popular statistical language used to perform sophisticated statistical analysis and predictive analytics, such as linear and nonlinear modeling, statistical tests, time-series analysis, classification, clustering, etc.

The R-console primarily utilizes a command-line interface, but there are many GUI tools available for download to make it easier to write R programs (most of them are free). With R, users can create variables, formulas, functions, and graphs to visualize their analysis and predictions.

As a free, open-source language, there is a community of contributors who continually create new packages (extensions) for R that define advanced statistical functions that were not originally built into R. These packages can be downloaded into R to expand R’s capabilities. Most of these packages are also developed and made available for free. It is for this reason that R has become so popular and why it continues to gain functionality over time.

**Why R?**

R provides a powerful way to do statistical analysis on large sets of data. It is also free, which is a compelling factor to its growth.

Because it is open source, new functions and packages are created all the time, so if you can’t find a capability initially, you can search for a package that can do it or even create a package of your own.

Following are some of the important statistical analysis use cases which can be implemented using R

**-Descriptive Analysis**

**-Predictive Analytics**

**-Linear and Nonlinear Modeling**

**-Statistical tests like P Test, T Test, Z Test, Chi Square Test**

**-Time-Series Analysis**

**-Classification**

**-Clustering**

**-Sentiment Analysis**

**-Text Mining **

**-Correlation Analysis**

**-R - Tableau Integration**

**Why R – Tableau Integration:**

Pair the easy-to-use interface of Tableau for exploring and manipulating your data with the power of R's statistical libraries and packages

**Method:**

R functions and models can now be used in Tableau by creating new calculated fields that dynamically invoke the R engine and pass values to R. The results are then returned back to Tableau for use by the Tableau visualization engine

**Pre-requisites:**

- Users must have proficiency with the R language to write the appropriate scripts and functional calls they require

- Users must have access to an R server to access R functions from Tableau Desktop or Tableau Serve

- R server to access R functions (R serve to run R function calls)

- Tableau Desktop or Tableau Server (8.1 or later versions)

**Who is this feature intended for**

This feature is primarily targeted for users who are already proficient at R. It is NOT meant for beginners with R. Anyone who wishes to use the new functions must first learn how to use R in order to leverage its capabilities in Tableau.

Users who are already proficient with R will find the integration beneficial for several reasons:

- They will be able to do statistical analysis on their Tableau data

- They will be able to access any R package or function that has been installed in an R server which they can access

- They will be able to take advantage of all of the visualization capabilities in Tableau to further analyze and understand their data without having to manipulate their data in R (which can be cumbersome) for the same effect

**Setting R**

Download and Install R from The R Project for Statistical Computing

Download and Install Rserve Library

Run following commands on R command prompt

library(Rserve)

Rserve()

**Connecting to Tableau**

- To Connect Tableau to R follow steps

- Open Tableau Desktop (8.1 or later version)

- Go to the Help menu and select “Manage R Connection”

- Enter a server name as “Localhost” (or “127.0.0.1”) and a port of “6311”

- Click on the “Test Connection” button to make sure the connection is established and working properly

- You will see a successful message on successful connection of R and Tableau

- Click OK to close

**R Functions in Tableau**

There are four new built-in functions that are used to call specific R models and functions.

The functions are:

- SCRIPT_REAL – Returns a numeric result from a given R expression

- SCRIPT_STR – Returns a string result from a given R expression

- SCRIPT_INT – Returns an integer result from a given R expression

- SCRIPT_BOOL – Returns a Boolean result from a given R expression

The arguments you pass into each of these functions include R-language scripts and function calls.

You can pass 1 or more arguments to R, which are then passed dynamically via Tableau. For instance, you can pass the sales of each customer on a viz.

When using Desktop, you will need to have an R server (via R serve) that Tableau can access to successfully call the new functions.

**Finding Outliers in Tableau**

-if Script_Real("

-library(mvoutlier);

-sign2(cbind(.arg1))$wfinal01",

-SUM([Profit]))=0

-then "Outlier"

-else "Normal"

-End

**K Means Clustering in Tableau**

SCRIPT_INT(

'set.seed(42);

result <- kmeans(data.frame(.arg1,.arg2,.arg3,.arg4), 3);result$cluster;',

SUM([Petal length]), SUM([Petal width]),SUM([Sepal length]),SUM([Sepal width]))

Have more questions about Tableau? Reach out to us on __info@bitraininghub.com__