Call R functions from any application with the AzureML package

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you've developed a useful function in R (say, a function to make a forecast or prediction from a statistical model), you may want to call that function from an application other than R. For example, you might want to display the forecast (calculated in R) as part of a desktop, web-based or mobile application. One solution is to install R alongside the application and call it directly, but that can be difficult — or impossible, in the case of mobile apps. (You also need to be careful to comply with R's open-source GPL2 license.)

Oftentimes, an easier way is to install R on a cloud-based server, and call your R function via a remote API. If you manage such a server yourself, one solution is to install DeployR on the server, and publish your function that way. But now there's an even simpler alternative: use the AzureML package (now available on CRAN) to publish your function directly to the Microsoft Azure cloud service, and then call that function using a simple REST call.

To get started, you'll need your Azure Workspace ID wsID and Workspace Authorization Token wsAuth (this Technet blog post by Raymond Laghaeian provides the details, and if you don't yet have an Azure subscription a free trial is available). Then, use the publishWebService function to publish the function to the cloud. Here's the example from the blog post:

irisWebService <- publishWebService(
  “predictSpecies”,        # R function to publish
  “irisSpeciesWebService”, # service name
  list(“sep_len”=”float”, “sep_wid”=”float”,
     “pet_len”=”float”,”pet_wid”=”float”), # parameters and types
  list(“species”=”int”),   # result and type
  wsID, wsAuth             # authorization ID/token  

All you need to do is specify the function to publish (here, a user-defined R function called predictSpecies), its input parameters (and their types), and the result type (along with a name for your service and authentication info). The AzureML package handles delivering the contents of your function (and any dependencies) to the Azure cloud, and setting up a web service for you according to your specifications. You can then manage this web service as a standard Azure service, test it out, and even monitor how often it's called:


Using the AzureML package, you can make any R function you create available to any application connected to the web, as long as the inputs and outputs are simple data types supported by the API. You can find more details in the AzureML package vignette and in the blog post linked below.

Technet Machine Learning Blog: Build & Deploy Predictive Web Apps Using RStudio and Azure ML

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)