How to Integrate R and Tableau?: Simplifying Statistical Analysis 101

By: Published: March 17, 2022

R and Tableau Feature Image

Tableau is a popular Business Intelligence tool. It helps its users to analyze their data and extract insights for decision-making. Tableau users can also create visualizations and combine them into dashboards. This makes it easy for anyone to extract insights from data, even non-technical users. When using Tableau, you may need to take advantage of Statistical Programming Languages like R to analyze your data. This is where R and Tableau integration comes in.

R is a Statistical Programming Language popular for doing Data Analysis tasks. It comes with a number of functions that you can use to analyze and visualize your data. Hence, there has been an increasing demand to connect R and Tableau. The good news is that this is possible through the Rserve package. In this article, we will be discussing how to integrate R and Tableau.

Table of Contents

What is Tableau?

R and Tableau: Tableau
Image Source

Tableau is a prominent Business Intelligence(BI)& Data Visualization tool used by companies and organizations all over the world for reporting and analyzing huge volumes of data. Developed by Pat Hanrahan, Christian Chabot, and Chris Stolte, Tableau helps in transforming raw data into a format that is easily understood by professionals at any level in an organization. Since its inception in 2003, Tableau has allowed leading industries to cut analysis time and make their organizations more data-driven.

Reading this you’re probably thinking Excel already does that, so what’s different about Tableau? Well, Tableau’s innovation was entirely focused on Visual Analysis, and it has a plethora of features and offers you so much more control over what your visualization looks like. While Excel allows users to evaluate data and do calculations, Tableau’s intuitive visualizations help you to convey your data graphically and in a visually attractive and understandable format. Tableau users can further interact and engage with data graphically to quickly uncover business insights.

Key Features of Tableau

  • Multipurpose Dashboard: Tableau comes with an intuitive dashboard that allows even non-technical users to perform quick Data analyses and create visualizations. Users can create high-level graphs and dashboards using its easy-to-use drag and drop functionality.
  • Extensive Data Sources: Tableau houses over 200 connectors and drivers that allow users to securely connect to multiple third-party applications and external data sources such as Relational Databases, Big Data, Cloud, Spreadsheets, and more.
  • Powerful Collaboration: Users working on different projects can collaborate with each other on Tableau. Users can further share the dashboard on the Cloud, making it accessible to anyone from anywhere.
  • Advanced Visualizations: Tableau has a vast collection of advanced Data Visualization techniques. Users can easily visualize data in the form of Charts, Tables, Graphs, Plots, Maps, etc.
  • Tableau Community: Tableau has a large community of active users, Developers, and Analysts. Being a part of this community allows you to continuously learn, upskill, build powerful connections, and offer mentorship to newcomers.

What is R?

R and Tableau- R logo
Image Source

R is a procedural programming language that divides a problem into stages, processes, and subroutines. This makes it simple for R to convert data into useful statistics, graphs, and statistical learning models for predictions and conclusions. Organizations may focus not just on good reporting and clean visuals using R in Data Science, but also construct Interactive Web Applications for reports using Packages.

Key Features of R

R in Data Science has quickly become one of the most popular programming languages among data scientists, having been built from the ground up for statistical analysis.

An R Package is a collection of functions, data, and documentation that improves the usability of R for Data Scientists.

R has the following unique features in Data Science that make it a superior programming language:

  • Quick Analysis
  • Better Integrated Development Environment (IDE)
  • Speed
  • Range of Database Support
  • Interactive Web-Based Dashboards

Simplify Tableau Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data is a No-code Data Pipeline that offers a fully managed solution to set up data integration from 100+ Data Sources (including 30+ Free Data Sources) and will let you directly load data to a Data Warehouse to be visualized in a BI tool such as Tableau. It will automate your data flow in minutes without writing any line of code. Its fault-tolerant architecture makes sure that your data is secure and consistent. Hevo provides you with a truly efficient and fully automated solution to manage data in real-time and always have analysis-ready data.

Get started with hevo for free

Let’s look at some of the salient features of Hevo:

  • Fully Managed: It requires no management and maintenance as Hevo is a fully automated platform.
  • Data Transformation: It provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Real-Time: Hevo offers real-time data migration. So, your data is always ready for analysis.
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100’s sources that can help you scale your data infrastructure as required.
  • Live Monitoring: Advanced monitoring gives you a one-stop view to watch all the activities that occur within Data Pipelines.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

Prerequisites

This is what you would need to establish a Tableau and R connection.

  • R
  • Tableau Server or Desktop

What is the need to set up R and Tableau Integration?

R is a free software environment and programming language for statistical computing and graphics. R is used by Data Miners and Statisticians for Data Analysis and developing graphical representations. R is also compatible with many Data Science libraries. Connecting Tableau with R allows you to access Tableau data with a pure R script. Using R with Tableau gives you finer control over your queries.

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

Advantages of R and Tableau Integration

The R and Tableau integration comes with the following benefits:

  • It becomes easy to extract patterns from data and build models with R. 
  • Tableau lets you make sense of your data using its Slice, Aggregate, and Filter features with a few clicks. You can optimize your models before writing code. 
  • You can easily visualize your findings by building dashboards using sliders, drop-downs, and other visual tools in minutes. 
  • Users can get full value from the analysis without handholding, helping you to focus on more impactful models. 

After performing the integration, you will be able to use both R and Tableau features in your Data Analysis tasks. Tableau offers 4 Scripting Functions that you can use to pass parameters and scripts to R. R runs the scripts and returns results. These functions include:

  • SCRIPT_REAL
  • SCRIPT_STR
  • SCRIPT_INT
  • SCRIPT_BOOL

The 4 functions differ in terms of the values they return. From their names, you can tell that they return Real, String, Integer, and Boolean Values respectively. 

Let us give an example of a scenario where you may need to use R and Tableau. Suppose you want to fit a Linear Regression line for a dataset that shows marks scored in an exam against study hours. Tableau doesn’t have inbuilt functions for calculating values for the slope and the intercept, so, you may have to write a lengthy calculation that uses mathematical formulas. R can save you from that. You simply have to pass an R script for creating the Linear Regression model into a calculated field. If you add the calculated field to the visual, you will get a linear regression line.

How to set up R and Tableau Integration?

If you are familiar with both R and Tableau, it’s easy to establish a connection between the 2 platforms. You should have both Tableau and R installed. You can then use the Rserve package to perform the integration.

Step 1: Install the Rserve Package

The Rserve package will let Tableau connect to R and use its functions, packages, libraries, and models. The package can be installed by running a single command on the R console. Open Rstudio on your computer and run the following command:

install.packages("Rserve")
R and Tableau: Install Rserve
Image Source

The command should install the package without raising issues. To use the package, you must load it using the following command:

library(Rserve)

You should now be able to use the Rserve package. Let’s move to the next step.

Step 2: Integrate R and Tableau

It’s now time to connect Tableau to R. Follow the steps given below:

  • Open Tableau. 
  • Click “Help” from the menu, choose “Settings and Performance”, and then “Manage Analytics Extension Connection…”.
R and Tableau: Analytics Extension Connection
Image Source
  • The “Analytics Extension Connection” window will be opened. Choose “Rserve” as the Analytics Extension and enter the server name and the port. Since you are running both on your computer, enter “localhost” for the server name. Once done, click the “Test Connection” button. 
  • You should get a message showing that the connection was established successfully. 
R and Tableau: Analytics Connection Successful
Image Source

If you get the above message, it means that you are done with connecting R and Tableau. You can now start to use R scripts in Tableau. 

Note: Tableau workbooks with R scripts can only be opened in Tableau Server after Tableau Server has been connected to Rserve. The Tableau Administrator should connect Tableau Server to Rserve. 

As of now, only Tableau Server and Tableau Desktop support integration with R. Tableau Public and Tableau Online users don’t have access to this feature. 

The R and Tableau integration is a powerful feature that allows Tableau users to use the Statistical modeling features of R inside Tableau. However, it comes with a number of limitations.

You can only use the 4 Scripting Functions to interact with R and you cannot import visualizations created in R into Tableau via calculated fields. And in case a calculated field that uses R scripts is used in another calculated field, they must both be included in the visual so that the second calculated field may work.

Using R Functions in Tableau

Let’s now demonstrate how you can use the 4 Scripting Functions in Tableau.

SCRIPT_BOOL

This function returns a Boolean result from an expression. When writing R expressions in Tableau, use .argn them to denote parameters. The following example demonstrates how to use the SCRIPT_BOOL function:

SCRIPT_BOOL("is.finite(.arg1)", SUM([Profit]))

In the above example, the .arg1 is equivalent to SUM([PROFIT])

Let’s give another example to check for the store IDs in the Washington state:

SCRIPT_BOOL('grepl(".*_WA", .arg1, perl=TRUE)',ATTR([Store ID]))

The function will return True for store IDs in Washington and False otherwise. 

SCRIPT_INT

This function returns an integer result from an expression. Let’s use this function to create 3 clusters from the Iris dataset:

SCRIPT_INT('output <- kmeans(data.frame(.arg1,.arg2,.arg3,.arg4), 3);result$cluster;', SUM([Petal length]), SUM([Petal width]),SUM([Sepal length]),SUM([Sepal width]))

SCRIPT_REAL

This function returns a real result from an expression. The following example demonstrates how to convert temperature values from degrees Celsius to Fahrenheit:

SCRIPT_REAL('library(udunits2);ud.convert(.arg1, "celsius", "degree_fahrenheit")',AVG([Temperature]))

SCRIPT_STR

This function returns a string value from an expression. The following example demonstrates how to extract a state abbreviation from a complicated string:

SCRIPT_STR('gsub(".*_", "", .arg1)',ATTR([Store ID]))

Conclusion

Tableau is a Business Intelligence tool. It helps users to analyze their data and create dashboards and visualizations from it. However, Tableau alone may not be enough to help you accomplish all your Data Analysis tasks. That’s why you may need to integrate it with a Statistical Programming Language like R. However, there are only 4 scripting functions that you can use to pass parameters and scripts to R. These include SCRIPT_REAL, SCRIPT_STR, SCRIPT_INT, and SCRIPT_BOOL

An R and Tableau integration comes with many benefits. It makes it easy for you to use R functions, packages, and models right from Tableau. This blog helped you connect R and Tableau USING R’s Rserve package. Tableau makes Business Analysis more efficient through intuitive, interactive, and easy-to-use services. Moreover, analyzing and visualizing your data by loading it from a Data Warehouse to Tableau can be cumbersome. This is where Hevo comes in.

visit our website to explore hevo

Hevo Data with its strong integration with 100+ Sources & BI tools allows you to not only export data from sources & load data to the destinations, but also transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs and perform insightful analysis using BI tools such as Tableau.

Give Hevo Data a try and sign up for a 14-day free trial today. Hevo offers plans & pricing for different use cases and business needs, check them out!

Share your experience of working with R and Tableau integration in the comments section below.

Nicholas Samuel
Technical Content Writer, Hevo Data

Skilled in freelance writing within the data industry, Nicholas is passionate about unraveling the complexities of data integration and data analysis through informative content for those delving deeper into these subjects. He has written more than 150+ blogs on databases, processes, and tutorials that help data practitioners solve their day-to-day problems.

No-code Data Pipeline For Tableau