TERR Code for Installing & Attaching CRAN Packages

  • Are you working with a team that shares DXPs amongst team members?
  • Do your TERR data functions require CRAN packages to be installed?
  • Are you worried your teammates won’t be able to run your data function(s)?

This post focuses on a template recently released on the Exchange that addresses the how to build TERR code that performs all the “pre-work” required to use CRAN packages in TERR data functions.  The template provides code so that users don’t have to install or attach packages before running a data function.  This is especially important if you are working with users who aren’t familiar with TERR. To get started, let me explain the “pre-work”.

How TERR Works with Packages

If your TERR data function uses CRAN package(s), you must perform the two steps below.

  1. Install the package
  2. Attach the package to the library

Without these steps, there will be errors.  Errors with Packages

Installing and attaching the package is easy via the Tools — TERR Tools menu.

  1. Click on the Package Management tab
  2. Select a CRAN repository
  3. Search for the package
  4. Click the Install button.TERR Tools Packages Menu

Easy!  It’s that simple to install the package.  When you install via this menu, the package is also attached to the library.

 

The Difficulty with Packages

So….why is this difficult or confusing?  Well, there are several problems lurking here.

  1. First, other users might not know they need to install packages.
  2. Second, installing multiple or many packages is a bit cumbersome, even with this nice interface.
  3. More importantly, Spotfire discards the package from the library when the analysis session ends.  The user must reattach the package each time Spotfire opens.  This takes time, and users not familiar with TERR definitely won’t know they need to do this.

You might be asking why the package has to be reattached to the library.  Well, there’s a simple answer for that — memory.  Any package attached to the library takes up memory.  Over time, it’s possible to accumulate a very large repository of packages, and they all take up space.  Therefore, installing them and attaching them to the library for use are two separate steps.  Just to be crystal clear, once installed, the package stays installed.  Only the attchment step needs repeating.  Fortunately, all of these problems are solveable with code.

The Code for Installing & Attaching Packages

To continue, download and open the template (link above).  Go to Edit — Data Function Properties, and look at the script for the data function.  Read the comments below for an explanation of what each line does.

##create an object that includes all the package names
mypackages <- c(“easypackages”, “devtools”, “remotes”, “readr”)

##installs any packages needed
if (!require(mypackages)) install.packages(mypackages)

##attaches the easypackages package to the library to gain access to the libraries() function
library(easypackages)

##the easypackages package creates the libraries function, which allows you to install multiple packages
libraries(mypackages)

In order to replicate in your file, simply alter the list of packages in the first line defining the mypackages object.  NOTE: You must leave in the easypackages package.  This is required to generate the libraries function in the last line of code.  If you add this code to all of your TERR data functions, users will never have to worry about installing packages or attaching them to the library!!  Your users will thank you!

 

Leave a Comment

Your email address will not be published. Required fields are marked *