1 Reply Latest reply on Oct 18, 2016 4:14 PM by Bora Beran

    Question on using Tableau's integration with R to create and display parameter driven contour maps

    Paul Ader

      I have been struggling to find an effective way to use Tableau's integration with R to create and display contour maps for XY plots defined by user-selected parameters and filters.   I have read a large number of integration-related  posts on the forum to see if they could help, however as a mid-level user of Tableau and a newbie to R many of them include technical detail that I do not understand.   


      My first attempt to create contour maps involved the use of a SCRIPT_BOOL function to run a ggplot geom_density2 function to produce a png image file with contour lines that could be used as a background image under the XY plot.  However, this does not work because Tableau cannot dynamically refresh the source files it uses for background images; this means that the image remains fixed even though the parameters and filters on the XY plot have been changed.


      Tableau Support were unable to help but suggested that I spoke with Ben Moss, now at The Information Lab in the UK, who had posted a relevant example on YouTube showing how line marks in Tableau could be used to draw contour maps.   I spoke with Ben to ask for more detail and he was very helpful; however, his solution (which he has given me permission to attach to this message) reads source data directly into R, uses a ggplot geom_density2 function to write data to a CSV file and then loads the CSV file as source data into Tableau.  In other words, Ben's solution uses R and Tableau but not in an integrated way.   Also, even if I could initiate and run Ben's R script from a Tableau calculated field, I suspect that the CSV file it creates could not be updated while Tableau is open and thus this solution would suffer from the same problem as the creation of png image files in my first attempt.


      So, what I would like to understand is how I can use a SCRIPT function in Tableau to provide filtered XY data to R with the relevant value of the selected across and down dimension and to return data directly from R that allows Tableau to draw contour lines and also use a dual axis to show the original XY data points.    The visualisation that I would like to be able to create 'on demand' is illustrated - albeit with incorrect contour maps - in the "Multiple+Background" worksheet in the attached "Test Workbook".  


      My research to date has identified three issues (and I am sure there are many more).  These three issues are:

      1. How do I construct a SCRIPT function in Tableau that provides the values of the across and down dimensions as well as the XY data;
      2. How do I construct a ggplot geom_density2 function that creates data (as illustrated in Ben's output file) for separate sets of contour lines for each combination of the across and down dimensions; and
      3. How do I return data from R and convert this data into the measures and dimensions needed by Tableau.  


      (NB.  I understand that R returns data to Tableau as a vector - but I am not sure what this means or how it differs from data held / used by Tableau).


      Having looked through the various examples posted about the integration of Tableau and R for other purposes, I imagine that what I am trying to do is possible and, for the relatively small data sets (typically < 5000 records with ~200 fields) it would not create too much load for Tableau or R.


      If you would like me to describe my issue in more detail or clarify any aspects, I would be very pleased to connect with you over Skype or GoToMeeting.


      I hope that you can help and look forward to hearing from you.


      Kind regards