Reader small image

You're reading from  Mastering Tableau 2023 - Fourth Edition

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781803233765
Edition4th Edition
Right arrow
Author (1)
Marleen Meier
Marleen Meier
author image
Marleen Meier

Marleen Meier is an accomplished analyst and author with a passion for statistics and data. By using traditional methodologies and approaches such as Machine Learning and AI, Marleen is dedicated to driving meaningful insights. Currently working as the APAC Data CoE Lead for ABN AMRO Clearing, Marleen is at the forefront of innovation and implementing data-driven strategies in a global financial environment. She has lived and worked in multiple countries, including Germany, the Netherlands, the USA, and Singapore, allowing her to bring a diverse and global perspective to her work. Through her writing and speaking engagements, she aims to empower individuals and organizations to unlock the full potential of their data assets.
Read more about Marleen Meier

Right arrow

Integrating Programming Languages

After 14 chapters full of Tableau functionality and exercises, what are we still missing? Correct, the one feature that opens a whole new universe of opportunities: programming integration! If you need a function that isn’t covered natively by Tableau’s calculated fields and you know how to code it, you can fall back on programming integrations. To be precise, two languages are supported: R and Python.

Imagine the following scenario: you deal with a financial dataset and want to add an options pricing formula to your Tableau dashboard. You look up the formula and see that you have all the required variables, like strike price, stock price, and volatility, but you also need a probability density function, which you can’t find in Tableau. In this case, you can fall back to the programming integration and run the calculation in R or Python and send back the output—great, isn’t it? Now imagine a second scenario:...

Integrating programming languages

How does integration empower Tableau? It happens through calculated fields. Tableau dynamically interfaces with Rserve or TabPy to pass values and receive results. And Tableau Prep Builder also has R and Python integration as we saw in Chapter 3, Using Tableau Prep Builder! So, let’s not waste any time and jump right in.

Basic Tableau-to-R and Tableau-to-Python integration is quite simple: the view shows data based on a calculated field, with the help of which Tableau pushes data to Rserve or TabPy respectively and then retrieves the results via a table calculation:

Figure 15.1: Tableau external services

Naturally, whether you are viewing a workbook on Tableau Desktop or via Tableau Server, if you wish to run R and Python calculations, then Rserve or TabPy must be accessible.

For a proper understanding of the integration, let’s also look at the Tableau/R workflow as an example. The terms used in the following diagram...

R installation and integration

To adequately understand how Tableau and R work together, it’s important to grasp the big picture. To facilitate that understanding, we’ll cover high-level concepts and information in this section before delving into calculated fields and R scripting details.

Installing R is typically not difficult, but it does involve more than simply double-clicking on an executable. To successfully connect Tableau with R, you will need to make sure that permissions are correctly set and that various components—some required and some that are just nice to have—are correctly implemented. We will cover the basics, review a couple of the typical challenges faced during installation, and provide troubleshooting guidance.

Perform the following steps to install R:

  1. Download R by visiting http://www.r-project.org/, click on the download R hyperlink, and choose a CRAN mirror. Note that R works best in a Linux or Unix environment...

Implementing R functionality

Now that we have successfully connected Tableau with R, let’s write some code in Tableau to invoke R. Within Tableau, open the Calculated Field Editor. Notice the class of functions beginning with SCRIPT_, as shown in the following screenshot:

Figure 15.8: SCRIPT_ functions

The SCRIPT_ functions are used by Tableau to invoke R. The function names communicate the data type of the returned results; SCRIPT_REAL returns float values, SCRIPT_BOOL returns true or false values, and so forth.

The syntax of a SCRIPT_ function is represented in the following diagram:

Figure 15.9: R script syntax

The preceding example code calculates the average profit, but we will get to more examples in the next sections. Let’s start by reproducing Tableau functionality using the R integration and hence prove that R is working properly.

Reproducing native Tableau functionality in R

For our first exercise, we will use the AVG,...

Python installation and integration

Python is an interpreted programming language and is very well known for its readability. The first release was in 1991, so quite some time ago (longer than most people would guess), and it was developed by Guido van Rossum. TabPy is an external service that will allow you to connect Python and Tableau—similar to Rserve. By using TabPy, you will be able to parse fields from your Tableau dashboard to Python, execute a calculation, and send back the result as a newly calculated field to Tableau. Or you can also call functions that you implemented in Python, again in a calculated field. A more extensive article on TabPy can be found here: https://tableaumagic.com/tableau-and-python-an-introduction.

Installing Python is typically not difficult, but it does involve more than simply double-clicking on an executable. To successfully connect Tableau with Python, you might have to install some libraries and execute comments on the command line...

Implementing Python functionality

Just like R, TabPy makes use of the SCRIPT_ functions in Tableau. In the next sections, we will practice working with TabPy and will look at multiple use cases. Tableau calculations using TabPy look very similar to R’s. For TabPy, it is important to add a return statement to the calculated field and notice that arguments are noted with an underscore instead of a dot:

Figure 15.31: Python TabPy syntax

This will be manifested in the next exercises; we will first investigate random number generators.

Random and random normal

Many calculations are easily accessible via the calculated fields, others via the table calculations—and then there are some hidden functions. Hidden because those are not (yet) fully supported or tested; therefore, use them with care. If you tried to find the function Random, for example, you would not be able to. But you can still use the Random() function, as can be seen here:

Figure...

Summary

This chapter just scratched the surface regarding the options of working with R and Python. After finishing this chapter, you should now be able to connect to Python and R from Tableau and recognize and write the basic syntax for both programming languages in Tableau. Most importantly, you are now skilled enough to leverage the power of R and Python functions in Tableau from a simple mean calculation to regressions, all the way to implementing your own machine learning model. Although we covered the installation, integration, and workflow, as well as some of the more popular functions and use cases, there is much more to explore. In fact, the possibilities of Tableau’s programming integration remain largely uncharted territory in the BI community.

The intrepid in data visualization are pushing the envelope, but there is much to be done. For those readers looking to enhance their career options, expertise in both packages could offer great advantages. And there...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Tableau 2023 - Fourth Edition
Published in: Aug 2023Publisher: PacktISBN-13: 9781803233765
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Marleen Meier

Marleen Meier is an accomplished analyst and author with a passion for statistics and data. By using traditional methodologies and approaches such as Machine Learning and AI, Marleen is dedicated to driving meaningful insights. Currently working as the APAC Data CoE Lead for ABN AMRO Clearing, Marleen is at the forefront of innovation and implementing data-driven strategies in a global financial environment. She has lived and worked in multiple countries, including Germany, the Netherlands, the USA, and Singapore, allowing her to bring a diverse and global perspective to her work. Through her writing and speaking engagements, she aims to empower individuals and organizations to unlock the full potential of their data assets.
Read more about Marleen Meier