In this tutorial, you'll learn how to develop Function Compute by running R applications.
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.
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 appropriate amount of computing resources necessary 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
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.
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
fun config to initialize the configuration.
fun --version 2.10.1
Running an R Application
fun initcommand to easily initialize this template project to a local environment.
fun init vangie/rlang-example
index.pytest 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)'))
rpy2and performing a simple addition operation in the R language. You can use the
fun localcommand 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
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
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.