Build Plumber API in Linux Server

Hello,

With the help of this wonderful community, I've managed to learn a bit more about how to deploy a model developed in R as a web API.

As far as I could discover so far, the easiest way to achieve this would be using R Studio Connect and Plumber as described in this great video here.

However, R Studio Connect pricing begins at $25.000/year and I can't afford it, since this is a small POC project and it will have only a few API calls a day.

So, my host has installed R in my Linux server per my request, and I can see it is there by calling it from the terminal:

So this is my bottleneck right now.
Where to go from here? Should I upload the R script to a folder in the server? Which one, and how to call that script? And how to install Plumber and other R packages?

I'm sure there's still a long way to go and a lot to read during this lockdown, so I'd appreciate some guidance.

Thank you in advance!

Hi @Nuno.Nogueira , you're welcome.

You can install the packages from that terminal you have opened, using for example: install.packages('plumber')

Then, you should upload your script and all its dependencies (.RDS, .CSV, etc).

Supposing that your script is called plumber.R, you can create an other script myapi.R that contains the following configuration (to make the api available over the internet, on port 8000 for example):

r <- plumber::plumb("plumber.R")
r$run(host="0.0.0.0", port=8000)

and then you can call this script from the linux terminal using Rscript command using:
Rscript myapi.R

you might also add cronjobs or other scripts to automatically start this scrip when it crashes.
Good luck.

1 Like

Take a look at plumberDeploy, for a couple API calls, it might make sense to use a DigitalOcean droplet.

Thanks for your help!
I've installed plumber package, apparently successfully.
But, library('plumber') returns:

I also uploaded myapi.R and the command Rscript myapi.R returns:

Hi, it seems that the package was not successfully installed.
also, you need to run the command Rscript myapi.R from the shell terminal and not from R terminal.
I mean, the same way you opened "R", but instead of typing R, you type:
ls to show files, and make sure "myapi.R" is in the same directory, then you type: Rscript myapi.R
if it's not in the same directory , you can provide its path, like: Rscript ~/home/user/my_scripts/myapi.R.

The issue is related with the installation of plumber, indeed:
image

So, I tried:
install.packages('librcurl')

But got the error:

these are system packages not r packages.

sudo apt-get install {library}

According to your distro

Are you admin on this machine? You'll probably have a hard time if not since you need to install system libraries.

Yes, I'm the admin, but this is too complicated.

Learning often is. I'm sorry.

This is how we built plumber on Ubuntu.
plumber/Dockerfile at master · rstudio/plumber (github.com)

From a bash shell (Debian or Ubuntu)

sudo apt-get update -qq && apt-get install -y --no-install-recommends \
  git-core \
  libssl-dev \
  libcurl4-gnutls-dev \
  curl \
  libsodium-dev \
  libxml2-dev

From R shell

install.packages("plumber")

But then again it depends on your OS distro since lib have changed name.

I can provide live assistance if you'd like.

I've tried with an Azure ML and apparently I get R Studio and Plumber working:

Will I be able to create the API with this?

yes, if it has a public IP and open port then yes.

You can test that the API is working using:

you can change the port number (8000) if you want
you also have the terminal tab in case you want to write a bash script or create croj jobs.

1 Like

Not yet.

This seems very easy to run locally but too complicated to run on a server.

I'm getting a 404 error:

My plumber.R is the basic from https://www.rplumber.io/articles/quickstart.html

This is the response:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>RStudio Server</title>
   <script type="text/javascript">
      function getBaseUri()
      {
         var index = window.location.href.lastIndexOf("/echo");
         if (index > 0)
         {
            return window.location.href.substring(0, index);
         }
         return "/";
      }

      var cssLink = document.createElement("link");
      cssLink.href = getBaseUri() + "/css/page.css";
      cssLink.type = "text/css";
      cssLink.rel = "stylesheet";
 
      var iconLink = document.createElement("link");
      iconLink.href = getBaseUri() + "/images/favicon.ico";
      iconLink.rel = "shortcut icon";
   
      document.getElementsByTagName("head")[0].appendChild(cssLink);
      document.getElementsByTagName("head")[0].appendChild(iconLink);
   </script>
   <style type="text/css">
      #banner {
         margin-bottom: 0px;
      }
      p {
         margin: 20px 20px 10px 20px;
      }
   </style>
</head>

<body>

   <div id="view" class="container">

   <div class="header-box">
       <img class="logo-image" id="logo" src="/images/rstudio-logo.png" />
       <h2>RStudio Server</h2>
   </div>
     
   <p>The requested page was not found.</p>

   <p>Please contact your system administrator for assistance, or click <a href="#" onClick="history.back();">here</a> to go back.</p>

   </div>

</body>

<script type="text/javascript">
   
   document.getElementById("logo").src = getBaseUri() + "/images/rstudio-logo.png";

</script>

</html>

For me on my server, if I run it from Rstudio, the API url has some characters before the /echo
can you try adding them manually, so it become something like:
https://... azureml.ms/p/ffee3d60/echo?msg=hello
(the ones that appear in the url of the documentation link, next to "open in browser" button)

Otherwise, can you run it and then open the link in the browser specifying the port number after the hostname, like:
https:// ...... azureml.ms:8000/echo?msg=hello

@Nuno.Nogueira, this post by @skyetetra may be useful for you.

It has code sample to deploy plumber-based API using docker, which makes it OS agnostic :slightly_smiling_face:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.