Develop Function Compute by Running R Applications

Image for post
Image for post

By Yi Xian

This tutorial discusses how to develop Function Compute by running R Applications, but before we get down into it, let’s first take a look at several important concepts mentioned in this article.

Background Information

First, there’s Alibaba Cloud’s Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the necessary amount of computing resources to run user code. The user only pays for the resources required to run their code.

Next, there’s Fun, which is a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file.

Note: The techniques described in this article are applicable to Fun 2.10.1 or later.

Dependent Tools

This tutorial discusses how develop Function Compute by running R Applications. The project outlined in this tutorial is developed on macOS but it also involves platform-independent tools, which are compatible with Linux and Windows. Before proceeding to this example, make sure that Docker, Fun, and Fcli are correctly installed, updated to the latest version and properly configured. You can download, update, and configure these tools with the instructions and resources on the pages linked here.

Fun and Fcli depend on Docker to simulate the local environment. MacOS users can use homebrew to install these tools:

Windows and Linux users can refer to Installation to learn how to install these tools. After the installation, don’t forget to first run fun config to initialize the configuration.

Note: If you have already installed Fun, make sure it is version 2.10.1 or higher.

Running an R Application

First you can initialize the project. You can use the fun init command to easily initialize this template project to a local environment.

Then, you can conduct local testing by using the index.py test code, whose content is as follows:

The preceding code works by importing rpy2 and performing a simple addition operation in the R language. You can use the fun local command to test the function locally. To perform this step, Docker must be correctly installed in the local environment.

Then, you can deploy the application using the fun deploy command.

Then, you can run the application with the following command:

R Language Compilation

The pre-compiled R language environment, rpy2 library, and related apt dependency files have been put under the .fun directory. Normally, you do not need to compile the R language yourself. From my own experience, compilation can take around half an hour each time. If existing R language compilation options cannot meet your business requirements, you can see the fun.yml file, which provides a complete list of the compilation and installation methods. You can make some adjustments accordingly and then use the fun install command to complete the installation.

References

  1. Building R from source
  2. R Sources
  3. Documentation for rpy2

Original Source

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store