Before you get started learning about R algorithm development, make sure you go through our Getting Started Guide to learn how to create your first algorithm, understand permissions available, versioning, using the CLI, and more.
Table of Contents
- Available Libraries
- Write your First Algorithm
- Saving and Loading R Models
- Managing Dependencies
- I/O for your Algorithms
- Error Handling
- Algorithm Checklist
- Publish Algorithm
- Conclusion and Resources
Algorithmia makes a number of libraries available to make algorithm development easier.
The full R language and standard library version 3.3.1 is available for you to use in your algorithms.
Furthermore, algorithms can call other algorithms and manage data on the Algorithmia platform via the Algorithmia R language Client.
Write your First Algorithm
If you’ve followed the Getting Started Guide, you’ll notice in your algorithm editor, there is boilerplate code that returns “Hello” and whatever you input to the console.
The main thing to note about the algorithm is that it’s wrapped in the algorithm() function.
The algorithm() function defines the input point of the algorithm. We use the algorithm() function in order to make different algorithms standardized. This makes them easily chained and helps authors think about designing their algorithms in a way that makes them easy to leverage and predictable for end users.
Go ahead and remove the boilerplate code below that’s inside the algorithm() function because we’ll be writing a different algorithm in this tutorial:
Saving and Loading R Models
When you have an R model that has been serialized as an .rds file, you can deploy it easily on Algorithmia. All you need to do is save your model:
Then follow the instructions for how to work with data using the Data Api in the R Client Docs in order to upload your saved model to Algorithmia hosted data or you can store it in Dropbox or an S3 bucket. To find out more about working with data check out the Data Portal.
Here are a couple of demos to show you how to load your hosted .rds file inside your algorithm:
Now that you have created your algorithm, you can add dependencies.
Algorithmia supports adding 3rd party dependencies via the CRAN (Comprehensive R Archive Network) dependency file.
On the algorithm editor page there is a button on the top right that says “Dependencies”. Click that button and you’ll see a modal window:
As you can see in the dependency file above, there are four different ways you can load packages in R. If you want the latest version from CRAN, than you simply type in the dependency name (this example uses the package e1071):
If you wanted that same package in an older version, all you have to do is install from CRAN the exact version by finding it in the packages archive, which in this instance, should be listed in the e1071 docs found under “Old Sources”. There you’ll find the archived packages, so simply choose your version and load your dependency:
Or, if you need to pull a dependency off of GitHub instead, all you need to do is install the package with:
Similar to how you would install through dev.tools() in R.
And finally, if you’re having issues with version conflicts, for instance package A requires version of package B while package C requires a different version of package B, then you can install with in your dependency file using install.packages():
The last format will take longer to load the dependencies than the other formats, so it’s best to use it as a last resort.
Note, that if you do add dependencies, you will still need to import those packages via library() to your algorithm file as you would do for any R script.
For example, to make use of e1071, you would include the line:
in the dependencies file and the line
in the main file.
The Algorithmia dependency is already installed for your convenience and relies on R version 3.3.1. For more information about Algorithmia’s R package visit: Algorithmia’s CRAN package documentation.
This guide won’t depend on any external dependencies so you can close the dependencies window.
I/O for your Algorithms
Now let’s get started on the hands-on portion of the guide:
The first algorithm that we’ll create will take a JSON formatted object passed as input by the user which is deserialized into a R list before the algorithm is called.
It will output a JSON formatted object which the user will consume with an API call to the algorithm path which is found at the bottom of the algorithm description page.
This path is based on your Algorithmia user name and the name of your algorithm, so if you are “demo” and your algorithm is “TokenizeText”, then the path for version 0.1.1 of your algorithm will be demo/TokenizeText/0.1.1
Working with Basic Data Structures
Below is a code sample showing how to create an algorithm working with basic user input.
You’ll also see some error handling within the algorithm, but we recommend that you take a look at our Better Error Handling Guide for more information.
Go ahead and type or paste the code sample above in the Algorithmia code editor after removing the “Hello World” boilerplate code.
Now compile the new code sample and when that’s done test the code in the console by passing in the input and hitting enter on your keyboard:
You should see the minimum and maximum of the numbers in the list returned in the console:
Working with Data Stored on Algorithmia
This next code snippet shows how to create an algorithm working with a data file that a user has stored using Algorithmia’s Hosted Data Source.
While users who consume an algorithm have access to both Dropbox and Amazon S3 connectors, algorithm developers can only use the Algorithmia Hosted Data Source to host data for algorithm development.
If you wish to follow along working through the example yourself, create a text file that contains any unstructured text such as a chapter from a public domain book or article. We used a chapter from Burning Daylight, by Jack London which you can copy and paste into a text file. Or copy and paste it from here: Chapter One Burning Daylight, by Jack London. Then you will can upload it into one of your Data Collections.
This example shows how to create an algorithm that takes a user’s file which is stored in a data collection on the Algorithmia platform. It then splits up the text into sentences and then splits those sentences up into words:
After you paste the above code into the Algorithmia code editor you can compile and then test the example by passing in a file that you’ve hosted in Data Collections.
Following the example below replace the path to your data collection with your user name (it will appear already if you are logged in), data collection name, and data file name which you can find under “My Collections” in Data Collections:
The code above with return both the original text and the sentence split up into words.
This guide uses a chapter from the public domain book Burning Daylight, by Jack London, but for brevity we’ll only show the first sentence in “text” and “words”:
When you are creating an algorithm be mindful of the data types you require from the user and the output you return to them. Our advice is to create algorithms that allow for a few different input types such as a file, a sequence or a URL.
Working with JSON Data
Note that we use the rjson package to parse JSON within your algorithm.
Writing files for the user to consume
Sometimes it is more appropriate to write your output to a file than to return it directly to the caller. In these cases, you may need to create a temporary file, then copy it to a Data URI (usually one which the caller specified in their request, or a Temporary Algorithm Collection):
Calling Other Algorithms and Managing Data
To call other algorithms or manage data from your algorithm, use the Algorithmia R Client which is automatically available to any algorithm you create on the Algorithmia platform. For more detailed information on how to work with data see the Data API docs.
You may call up to 24 other algorithms, either in parallel or recursively.
Here is an example of creating an algorithm that relies on data from another algorithm:
This should return a split up list of strings:
As you can see from these examples, fields that are passed into your algorithm by the user such as scalar values and sequences such as lists, dictionaries, tuples and bytearrays (binary byte sequence such as an image file) can be handled as you would any Python data structure within your algorithm.
For an example that takes and processes image data check out the Places 365 Classifier’s source code.
In the above code examples we showed how to create an AlgorithmError function which you should use for handling errors within your algorithm. This way the user can tell the difference between a standard R library error and an error thrown by your algorithm:
And then raise the error with a helpful error message:
If you are creating an algorithm that relies on calling another algorithm you may use Algorithmia error messages for catching errors thrown by that algorithm:
For more information on error handling see the Better Error Handling Guide.
Both links will go over important best practices such as how to create a good algorithm description, add links to external documentation and other important information.
Once you’ve developed your algorithm, you can publish it and make it available for others to use.
On the upper right hand side of the algorithm page you’ll see a purple button “Publish” which will bring up a modal:
In this modal, you’ll see a Changes tab, a Sample I/O tab, and one called Versioning.
Changes shows you your commit history and release notes.
Sample I/O is where you’ll create your sample input and output for the user to try under Try the API in the Run tab. When you add a sample input, make sure to test it out with all the inputs that you accept since users will be able to test your algorithm with their own inputs.
Under the Versioning tab, you can select whether your algorithm will be for public use or private use as well as set the royalty. The algorithm can either be royalty-free or charge per-call. If you opt to have the algorithm charge a royalty, as the author, you will earn 70% of the royalty cost.
Check out Algorithm Pricing for more information on how much algorithms will cost to run.
Under Semantic Versioning you can choose which kind of release your change should fall under: Major, Minor, or Revision.
If you are satisfied with your algorithm and settings, go ahead and hit publish. Congratulations, you’re an algorithm developer!
Conclusion and Resources
In this guide we covered how to create an algorithm, work with different types of data and learned how to publish an algorithm.