×
Community Blog Develop Function Compute by Running R Applications

Develop Function Compute by Running R Applications

In this tutorial, you'll learn how to develop Function Compute by running R applications.

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:

brew cask install docker
brew tap vangie/formula
brew install fun
brew install fcli

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.

$ fun --version
2.10.1

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.

fun init vangie/rlang-example

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

import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri

def handler(event, context):  
    pandas2ri.activate()
    return str(robjects.r('paste0("1 + 1 = ", 1 + 1)'))

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.

$ fun local invoke onePlusOne
skip pulling image aliyunfc/runtime-python3.6:1.4.0...
['1 + 1 = 2']

RequestId: 6e1f2402-9443-4392-9f6a-d87b4f79887a      Billed Duration: 7543 ms      Memory Size: 1998 MB      Max Memory Used: 123 MB

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

$ fun deploy
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********KbBS
using timeout: 60

Waiting for service rlang to be deployed...
    Waiting for function onePlusOne to be deployed...
        Waiting for packaging function onePlusOne code...
        package function onePlusOne code done
    function onePlusOne deploy success
service rlang deploy success

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

$ fcli function invoke -s rlang -f onePlusOne
['1 + 1 = 2']

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
0 0 0
Share on

Alibaba Cloud Serverless

99 posts | 7 followers

You may also like

Comments