Canopy Labs strives to make it as simple as possible for any individual to understand their customers by browsing profiles and viewing dashboards. We also know that it’s not always possible to predict what customer analytics problems our clients will be looking to solve. With this in mind, we are introducing the R Console for Canopy Labs. The goal behind the R console is to make all your centralized customer data and model results in a scriptable, customizable form. We’re hoping that this will allow you to build even more advanced and interesting models. In this blog post, we’ll show you how to start using the R console yourself.

The Canopy Labs R Console versus R itself

Using the R console requires familiarity with the R statistical programming language, as the basic functionality of the R console is somewhat similar. The entire console works in the same way as the R programming language, aside from two major differences:

  1. Printing to screen. If you’d like to print a set of values or results, you need to explicitly use the print() function.
  2. No input/output operations. Unlike R, you cannot load CSV files or make connections to other databases in our console. You only have access to the data that you’ve collected through Canopy Labs. Similarly, plotting needs to be done using our own dashboarding tools.

Saving and Loading Your Customer Data Into R

To facilitate data access in R, you can use the canopy.import() and canopy.export() functions. These are similar to R’s functions for reading external files or from databases.

To start, either login into your existing Canopy Labs account or sign up for a demo account with dummy data. Once you are in, navigate to the Console (R) interface on the top of the screen. Use the import function with the following parameters: canopy.import(collection_name) where the collection_name is the database collection you will be loading data from.

Exporting from the R console is just as easy. Use the export function with the following parameters: canopy.export(r_object, collection_name). In this case, the r_object must be a list of lists and collection_name is the name of the database collection to save to.

Example: Counting Tags

Once you have data loaded into the R console, use the same approach to analyze the data as you would use in standard R scripts and code. As an example, below is a very simple script that (a) counts the number of customers in your data set, and (b) counts the number of tags each customer has.

# Count the number of customers in your data set:
num_customers <- canopy.count('customers')

# Import the data in the “customers” data set into R
customers <- canopy.import("customers")

# A simple count to see how many customers there are
numCustomers <- length(customers)

# Loop through each customer to see how many tags the customer has.
numTags = 0
for (i in 1:length(customers)){
    tags <- customers[[i]][['tags']]
    numTags = numTags + length(tags)

# Print the average number of tags each customer has
print(paste("Number of tags per customer: ", numTags/numCustomers))


Advanced Example: Tagging Customers

The R console we have developed allows for fairly advanced manipulation of customer data as well. Suppose you want to tag all customers that have more than 15 actions in the database (i.e., are very active in purchasing and engaging).

query <- list()
query[['$where']] <- 'this.actions.length > 15'
tags <- list('active')

This code is slightly more complex, as it requires an understanding of Mongo and JavaScript. We first create a query object, which uses standard JavaScript to help filter customers. In this case, this.actions.length > 15 is the filter we use, and we write this using JavaScript syntax. You can extend this filter or change it using other JavaScript syntax.

The tags list is the actual list of tags to be applied to customers that meet the filtering criteria. You can have multiple tags here.

The canopy.tag code iterates through each customer and applies tags to those who meet the filtering criteria.


As you can see, running advanced customer analytics projects through the Canopy Labs R console is easy and simple. You can use standard APIs to import data from places like Shopify, Magento, and numerous other web-based services. Give it a try today! If you need any help, let us know.