Engine Works

Under the hood of Alteryx: tips, tricks and how-tos.
pmaier1971
Alteryx
Alteryx

R is an open-source language and environment widely used for statistical analysis and visualization. Leveraging the R tool – part of the Predictive Tools package – users can run R code directly from Alteryx. As R provides a wide variety of advanced statistical techniques, this is a convenient way to execute cutting-edge algorithms. (In fact, some of Alteryx’s more advanced features are implemented by leveraging R.) 

 

One great feature of R is that it is highly extensible, and the Comprehensive R Archive Network (CRAN) features a large list of additional packages for users to install. That said, while installing packages and running R code on Alteryx Designer on the desktop is relatively straightforward, installing R packages so your workflows also run on the server can be a bit more challenging. Having installed a few custom R packages on the Server myself, here are some tips and tricks to get you and your workflows up and running in no time. 

 

Screen Shot 2023-05-10 at 12.00.42 PM.png

 

First off, if you don’t have the R tool (part of the “Predictive Tools” package), users of Alteryx Designer > 8.5 can go to “Help – Install Predictive Tools.” Even if you already have an existing R installation, Alteryx leverages a separate R installation, and existing R packages will most likely not be available in the R version Alteryx uses. Plus, the version installed by Alteryx may also differ, so you may need to re-install custom packages. 

 

Second, by far the easiest way to install a custom R package for Alteryx Designer on the desktop is to use the “Install R Packages” app found in the Community Gallery. Run the workflow, type in the name of the package, and you should be good to go. 

 

Installing a custom R package on Alteryx Server can be tricky, due to permissions and dependencies. Also, it requires admin rights on the server. The Information Lab outlines the key steps, but the devil may be in the details – or better: in user vs. admin libraries. Let me show you what I mean. 

 

Example: Installing the “Flextable” package 

 

Let’s run through an example: Let’s assume that you have R version 4.1.3 (the version shipped with Alteryx Designer v. 2022.3) and want to install the package “flextable,” which you intend to use in a workflow published on the server. Navigate to C:\Program Files\Alteryx\R-4.1.3\bin on your server, right-click on R.exe, and select “Run As Administrator.”  

 

On my own server, two sets of R libraries are used, as shown by the screenshot below: 

 

Screen Shot 2023-05-10 at 12.07.43 PM.png

 

When installing a new package, we need to make sure that it installs into the admin library. With R running with admin rights, let’s type

 

install.packages(“flextable”, lib = "C:/Program Files/Alteryx/R-4.1.3/library")

 

You will receive a prompt to pick a CRAN repository, and the installation process will kick off. Once the installation is completed, type “library(flextable)” in the R console to check that the package loads correctly. 

 

In all likelihood, all workflows now leveraging this package on the server should run as well. But, a challenge can be that some of the dependent variables may be installed in the user library, as opposed to the admin library. As a result, the package may not load, despite being installed, because a dependency cannot be loaded. 

 

Without going down too far into the rabbit hole of R package management, let’s show a relatively straightforward way to debug if the workflows returns an error. Let’s create a new Alteryx workflow, and only drag the R tool onto the canvas. In the config file for the R tool, add: 

 

x = .libPaths()
y = try(find.package("flextable"), silent = TRUE)
z = Sys.info()[["user"]]

x = paste("Library paths: ", x, collapse = “ //// “)
y = paste("Flextable path: ", y)
z = paste("User: ", z)

AlteryxMessage(x, msg.consts$INFO, priority.consts$LOW )
AlteryxMessage(y, msg.consts$INFO, priority.consts$LOW )
AlteryxMessage(z, msg.consts$INFO, priority.consts$LOW )

library(flextable)

 

Run this workflow locally to make sure it works, and then publish it to the server and test it using different accounts (Curators, Artisans, and as a “Public” workflow). All this workflow does is retrieve all current library paths to verify that the location specified during the installation above (the “lib = <R path>”) was indeed the location the server expects the package in. The second line tests if the package is found. The last line shows which user executes the workflow (i.e., which “permissions” are needed). This information is then output via an Alteryx message, so it shows up as server output. 

 

The next line finally attempts to load the package. Here is where you may encounter an error. Go to the job view in the gallery and expand: 

 

image008.png

  

Let’s make the section in red larger: 

 

image009.png

 

This shows that one of the dependencies failed to load because the package was outdated. All you have to do is to go back to the R console (which again needs to be run as admin), and type the following: 

 

install.packages(“flextable”, lib = "C:/Program Files/Alteryx/R-4.1.3/library")

 

And voilà! Now everything should work. 

 

For other packages, I encountered situations where the actual package was installed in the admin library, but a dependency was in the user library. This will show up as “fail to load package,” with the name displayed. Again go back into R, and repeat the step above with the name of the package. You may have to repeat this step multiple times, depending on the package and your installation. 

 

Now you should be good to explore the full power of R, including its many custom packages, in Alteryx! For instance, it opens up easy possibilities to post to Twitter with rtweet, or to create complex charts.  

 

If you’re new to using R in Alteryx, the help page for the R tool provides an overview of how to get data in and out of R. The excellent R Functions in Alteryx Cheat Sheet provides a simple, concise overview. 

 

Stay tuned for the next post, where we will walk through some cool applications!