Last weekend I finally got a chance in the middle of the night again to play with the new Tableau 8.1 Beta and R integration with RServe. After a Diet Mountain Dew to wake me up and some tasty Betty Crocker vanilla frosting to ease my frustration with RServe on a Windows laptop (more to come on that), I did get functional with this cool combined solution and WOW – Tableau with R is a total blast! Here is what I did to get it up and running. I am going to walk through the install, connecting Tableau with RServe, the classic Hello World example and one example of calling an R function with a parameter from Tableau to visualize the results in Tableau.
Getting R to work with Tableau 8.1 should not be difficult but the key issue for me was to ensure my firewall and ports were configured to allow communications to RServe. Before you install RServe, you do need to have base R installed as a prerequisite. You can download base R from http://www.r-project.org. Note that an R GUI is not required for using R with Tableau. However, I am finding that using R Studio does help testing/debugging my R scripts before putting them into Tableau.
Once you have base R installed, then you can go ahead install RServe. RServe is a TCP/IP server which allows other programs like Tableau or a web app to use R without the need to initialize R or link to R library. RServe supports remote connection, authentication and file transfer. The RServe download contains code samples for popular languages such as C/C++ and Java. It even includes nice RServe web app code examples for PHP. In addition to Tableau 8.1+, SAP Predictive Intelligence, Oracle and many other well known enterprise applications use this same RServe approach to integrate R computation of statistical models and visualizations into their applications today.
Read the instructions and FAQs first to fully understand what you are installing and the little tid bits that may bite you if you are on a Windows platform – RServe is not 64 bit, you need to copy a few files to the 32 bit R bin versus the typical R library install routine, you need to double-click to start the RServe.exe process or use a Windows command line, etc. I thought JSoftware and Sudo had some nice reference material too on this topic. Do note the many warnings that RServe is not ideal with Windows – it shines on Unix, Linux and other platforms. There are connection limitations and many other annoyances with Windows. However if you simply want to learn how to use this combined solution like I am doing, Windows should be ok. Serious RServe implementations would not be rolled out on Windows…hmmm, maybe that’s why Microsoft has not fully embraced R yet, don’t know.
For my RServe install on a 64 bit Windows 8 laptop, I needed to open the RServe default port 6311 and also allow communications to the RServe app in Windows firewall. I also had to copy a few RServe files to the R program 32 bit folders for it to run.
To ensure it was running, I looked in Windows Task Manager for the RServe.exe process. I also used a Telnet trick to connect to it: telnet localhost 6311. With a functional RServe.exe process ready to go, it is now time to connect Tableau Desktop to this RServe and send it some R functions!
Connecting Tableau to RServe
Here is the super easy part that used to be much more challenging (see end of this blog) prior to Tableau 8.1. To connect Tableau with RServe, simply navigate to Help > Manage R Connection and enter your RServe URL and port. The defaults are populated automatically. Note that RServe is NOT a data source and thus it is not in the list of data sources. R is an web server app so the connection info is located under Help. To ensure your connections works, click Test Connection. If you get a success message then continue on. Otherwise, you most likely need to check your firewall settings.
Hello World and Parameterized R calls from Tableau
Here the real fun with R begins! To use R with Tableau, you are going to create a Calculated Field with the R function call and possibly use Tableau Parameters to store Results. The R Calculated Field will be treated in a similar manner as Tableau Table Calculations. In Tableau 8.1 there are several new Calculated Field functions that are related to R called SCRIPT_STR, SCRIPT_REAL, SCRIPT_BOOL, and SCRIPT_INT. These new functions include examples in the help descriptions of how to format your R calls properly. I find it is easy to first test and run your R function in RStudio or another R tool. Then copy over the functioning R code snippet into the Tableau Calculated Field window. In classic programming 101, Hello World is always the first example taught so here is the Hello World example in R with Tableau.
You preface the R function with SCRIPT_STR since the R result you will get back from RServe is a string data type in this example. Then you enter in the R function and the parameter where you want to store the returned value (‘hello<-“Hello world!”‘,ATTR([R Result])). With that created, you can now test it and visualize your exciting R Result in Tableau just as any you would any other Calculated Field.
That was nifty and certainly helps start putting this puzzle together. In all reality, you will most likely be passing parameters from Tableau Calculated Field R functions and getting numeric values back or other really amazing things like data mining results that we will cover in future blogs. (By the way if you went to TCC13 last month, Bora Beran had a great session on more advanced R statistics, what if analysis and R data mining with Tableau that is a must see for R enthusiasts. It was called “Making the most of your expanded analysis toolbox – Stats 2”.)
The next beginner Tableau with R example involves passing a Tableau field as a parameter to a simple Tableau Calculated Field R function and displaying the result. What I like about this example is you can easily get fancier and pass in a Tableau parameter as an R parameter (.arg argument in R world) for what if analysis.
Here you will use SCRIPT_INT since you will be getting a number back from RServe. In this sample you are passing the R function a parameter that is a Tableau field called [CONSTANT_VAR]. This does not have to be a constant value – I am using a constant for testing ease. You could also use an aggregate like SUM([Sales]) where I am using [CONSTANT_VAR]=10. When I visualize my new RServeTest2 field I see indeed R is computing the value 20 and Tableau is displaying that result.
There you have it. Easy breezy visualization of R functions with Tableau using an RServe connection…I totally love it. The new Tableau 8.1 R Integration features are A LOT EASIER than my previous blogs and decks on this combined solution topic Visualizing R Models in Tableau and How To Predictive in Tableau. That wraps up Getting Started with Tableau 8.1 Beta & R – Part 1. In the next part of this series, we will showcase visualizing much more exciting R data mining routines in Tableau.