5 Replies Latest reply on Nov 23, 2017 1:34 AM by Mohammed Abdul Mateen

    Connect Tableau to R and Using R scripts in Tableau

    Pawan Kumar

      How to start using “R” Scripts inside Tableau : Step by Step

      1.Tableau with R

      • This blog is intended to show how to leverage R to extend Tableau capabilities and visualize outputs from R.
      • This is not a Tutorial on R.


      2.What is R

      • R is an open source programming language and software environment for statistical computing and graphics
      • The R language is widely used among statisticians and data miners for developing statistical software and data analysis.



      3.What is R Studio

      • RStudio is a free and open-source Integrated Development Environment (IDE) for R.
      • RStudio is available in two editions: RStudio Desktop, where the program is run locally as a regular desktop application; and RStudio Server, which allows accessing RStudio using a web browser while it is running on a remote Linux




      4.Installing R & R Studio

      • Download R from the Link:

      http://cran.us.r-project.org/

      • Download R Studio from the Link:

      https://www.rstudio.com/products/rstudio/download/
      5.Setting Up Tableau Desktop with RTableau communicates with R using a package called Rserve.
      You can install and initiate Rserve from R command line by entering:
      install.packages("Rserve");
      library(Rserve);
      Rserve()

      6.Configure an Rserve Connection on TableauOn the Help menu in Tableau Desktop choose Settings and Performance > Manage R Connection to open the Rserve connection dialog box.






      The below screen opens up:
      Enter or select a server name using a domain or an IP address. The drop-down list includes localhost and the server you most recently connected to.
      Specify a port. Port 6311 is the default port for Rserve servers. If the server requires credentials, specify a Username and Password.
      Click Test Connection. Click OK.

      7. Pass Expressions to R

      • In order to let tableau know that the calculations need to go to R, it must be passed through one of the 4 functions.
      • These 4 functions are : SCRIPT_BOOL , SCRIPT_INT , SCRIPT_REAL , SCRIPT_STR
      • R Functions are computed as Table calculations in Tableau.
      • Since these are table calculations, all the Fields being passed to R must be aggregated like Sum(PROFIT), MIN(Profit), Max (Profit), ATTR( Category) etc.

      8. R Functions in Tableau
      8. Run an R script on Tableau8a → SCRIPT_BOOLReturns a Boolean result from the specified expression. The expression is passed directly to a running external service instance. In R expressions, use .argn (with a leading period) to reference parameters (.arg1, .arg2, etc.).SCRIPT_BOOL(" .arg1 > 0 ", SUM([Profit]))

      • In this R example, .arg1 is equal to SUM([Profit])
      • All the Fields being passed to R must be aggregated like Sum(PROFIT), MIN(Profit), Max (Profit), ATTR( Category) etc..


      8b → SCRIPT_INTSCRIPT_INT(" .arg1 / 2 ", SUM([Profit]))
      Returns an integer result from the specified expression. In this R example, .arg1 is equal to SUM([Profit])

      8c→ SCRIPT_REAL
      SCRIPT_REAL(" log(.arg1) ", SUM([Profit]))
      Returns a real result from the specified expression. In this R example, .arg1 is equal to SUM([Profit])
      8d → SCRIPT_STRSCRIPT_STR(" paste(.arg1 ,.arg2)",
      attr([Category]),
      attr([Sub-Category])
      )
      Returns a string concatenate result using “R” paste function. In this R example, .arg1 is equal to attr([Category]) SCRIPT_STR(" paste(.arg1 ,.arg2)",
      attr([Category]),
      attr([Sub-Category])
      )
      Returns a string concatenate result using “R” paste function. In this R example, .arg1 is equal to attr([Category])











      9. Share a Workbook That Requires an Rserve ConnectionYou may need to send a workbook that contains R functionality to other users, who may be using different copies of Tableau Desktop on other computers. Or, users may download a workbook from Tableau Server that contains R functionality. Before users will be able to use the R functionality in workbooks they have received or downloaded to Tableau Desktop, they must Configure Rserve connections on their Computers.



      10. Publishing Workbooks with an Rserve Connection

      • Publishing a workbook with R functionality to Tableau Server requires Server to have a connection to Rserve.
      • If multiple users need to access Rserve simultaneously, Rserve should be installed on Linux.
      • Rserve on Linux, unlike Windows, handles each user independently and does not allow users to overwrite other’s objects.
      • Before you publish a workbook that relies on an Rserve connection to Tableau Server, you should configure Tableau Server to have its own Rserve connection.
      • You do this by configuring settings with tabadmin set.
      • The settings are equivalent to the values you set in the Rserve Connection dialog box. They are:
      • vizqlserver.rserve.host
      • vizqlserver.rserve.port
      • vizqlserver.rserve.username
      • vizqlserver.rserve.password
      • Note: The settings vizqlserver.rserve.username and vizqlserver.rserve.password should be omitted if Tableau Desktop connects to RServe without a username and password.



      11. About R integration with Tableau [ As of Tableau 10.1 (April 2017)]

      • You cannot publish workbooks containing R scripts to Tableau Public.
      • You cannot view workbooks containing R scripts in Tableau Reader.
      • You cannot publish a workbook that contains R scripting to Tableau Online.
      • Tableau has been tested with R versions 3.1 and 3.2, and with Rserver version 0.6-8.
      • Tableau cannot verify that workbooks that use R will render properly on Tableau Server. There might be scenarios where a required statistical library is available on a user’s machine but not on the Rserve instance that Tableau Server is using.
      • For views that cannot be rendered in Tableau Server because of an R script error, you see a warning error when you publish the workbook .
      • Because Tableau Server provides an authentication mechanism, it can be more secure to expose Rserve functionality to users through Tableau Server than in Tableau Desktop.

       

      If you have any questions / concerns you can reach out to me at pawanpurbey@gmail.com or post a question at my blogging website tableauhive.com