{"id":639,"date":"2013-07-18T13:30:36","date_gmt":"2013-07-18T17:30:36","guid":{"rendered":"http:\/\/insights.canopylabs.com\/?p=526"},"modified":"2018-06-05T16:05:09","modified_gmt":"2018-06-05T20:05:09","slug":"analyze-your-customers-and-api-data-in-r-through-the-canopy-labs-console","status":"publish","type":"post","link":"https:\/\/staging.oddbee.com\/canopy-labs\/analyze-your-customers-and-api-data-in-r-through-the-canopy-labs-console\/","title":{"rendered":"Analyze your customers and API data in R through the Canopy Labs Console"},"content":{"rendered":"<p>Canopy Labs strives to make it as simple as possible for any individual to understand their customers by <a href=\"https:\/\/beta.canopylabs.com\/profiles.php\" target=\"_blank\" rel=\"noopener\">browsing profiles<\/a> and <a href=\"https:\/\/beta.canopylabs.com\/dashboards.php\" target=\"_blank\" rel=\"noopener\">viewing dashboards<\/a>. We also know that it&#8217;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 <a href=\"https:\/\/beta.canopylabs.com\/r-proto.php\" target=\"_blank\" rel=\"noopener\">R Console for Canopy Labs<\/a>. The goal behind the R console is to make all your centralized customer data and model results in a scriptable, customizable form. We&#8217;re hoping that this will allow you to build even more advanced and interesting models. In this blog post, we&#8217;ll show you how to start using the R console yourself.<\/p>\n<p><b><u>The Canopy Labs R Console versus R itself<\/u><\/b><\/p>\n<p>Using the R console <a href=\"http:\/\/www.cran.r-project.org\/doc\/manuals\/R-intro.pdf\" target=\"_blank\" rel=\"noopener\">requires<\/a> <a href=\"http:\/\/www.r-tutor.com\/r-introduction\" target=\"_blank\" rel=\"noopener\">familiarity<\/a> 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:<\/p>\n<ol>\n<li><b>Printing to screen.<\/b> If you&#8217;d like to print a set of values or results, you need to explicitly use the <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">print()<\/span> function.<\/li>\n<li><b>No input\/output operations.<\/b> 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&#8217;ve collected through Canopy Labs. Similarly, plotting needs to be done using our own dashboarding tools.<\/li>\n<\/ol>\n<p><b><u>Saving and Loading Your Customer Data Into R<\/u><\/b><\/p>\n<p>To facilitate data access in R, you can use the <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">canopy.import()<\/span> and <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">canopy.export()<\/span> functions. These are similar to R&#8217;s functions for reading external files or from databases.<\/p>\n<p>To start, either login into your existing Canopy Labs account or sign up for a demo account with <a href=\"https:\/\/beta.canopylabs.com\/signup.php\" target=\"_blank\" rel=\"noopener\">dummy data<\/a>. Once you are in, navigate to the Console (R) interface on the top of the screen. Use the import function with the following parameters: <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">canopy.import(collection_name)<\/span> where the <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">collection_name<\/span> is the database collection you will be loading data from.<\/p>\n<p>Exporting from the R console is just as easy. Use the export function with the following parameters: <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">canopy.export(r_object, collection_name)<\/span>. In this case, the <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">r_object<\/span> must be a list of lists and <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">collection_name<\/span> is the name of the database collection to save to.<\/p>\n<p><b><u>Example: Counting Tags<\/u><\/b><\/p>\n<p>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.<\/p>\n<pre style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\"># Count the number of customers in your data set:\r\nnum_customers &lt;- canopy.count('customers')\r\nprint(num_customers)\r\n\r\n# Import the data in the \u201ccustomers\u201d data set into R\r\ncustomers &lt;- canopy.import(\"customers\")\r\n\r\n# A simple count to see how many customers there are\r\nnumCustomers &lt;- length(customers)\r\nprint(numCustomers)\r\n\r\n# Loop through each customer to see how many tags the customer has.\r\nnumTags = 0\r\nfor (i in 1:length(customers)){\r\n    tags &lt;- customers[[i]][['tags']]\r\n    numTags = numTags + length(tags)\r\n}\r\n\r\n# Print the average number of tags each customer has\r\nprint(paste(\"Number of tags per customer: \", numTags\/numCustomers))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><b><u>Advanced Example: Tagging Customers<\/u><\/b><\/p>\n<p>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).<\/p>\n<pre style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">query &lt;- list()\r\nquery[['$where']] &lt;- 'this.actions.length &gt; 15'\r\ntags &lt;- list('active')\r\ncanopy.tag(query,tags)\r\n<\/pre>\n<p>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, <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">this.actions.length &gt; 15<\/span> is the filter we use, and we write this using JavaScript syntax. You can extend this filter or change it using other JavaScript syntax.<\/p>\n<p>The <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">tags<\/span> list is the actual list of tags to be applied to customers that meet the filtering criteria. You can have multiple tags here.<\/p>\n<p>The <span style=\"font-family: Courier, 'Courier New', monospace; font-size: 75%; background: #ebebeb; padding: 2px;\">canopy.tag<\/span> code iterates through each customer and applies tags to those who meet the filtering criteria.<\/p>\n<p><b><u>Conclusion<\/u><\/b><\/p>\n<p>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. <a href=\"https:\/\/beta.canopylabs.com\/signup.php\">Give it a try today<\/a>! If you need any help, <a href=\"mailto:info@canopylabs.com\">let us know<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"_links":{"self":[{"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/posts\/639"}],"collection":[{"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":1,"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":5852,"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/posts\/639\/revisions\/5852"}],"wp:attachment":[{"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/media?parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.oddbee.com\/canopy-labs\/wp-json\/wp\/v2\/categories?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}