Back to docs


On this page

Getting started

Once you have a deployed a R language Shaip, you can access the code in the source repository and can pull it to a folder to run on your machine, check out this link for a refresher on accessing the source cloud repository. The next step is to start developing your algorithm. The following provides details on using RStudio to develop and commit your R code as well as providing details on how to manage the environment and test your code.

Using RStudio to manage and develop your R Shaip code

Untitled.png


git.png

Package management using renv

https://rstudio.github.io/renv/

#Restore the packages from the cache to run in your local environment
renv::snapshot()
#Install a package.
renv::install("package-name")

#Update the lockfile - this is the file that keeps a record of all the packages you are using.
renv::snapshot()

#Make sure the packages you are using are upto date with the lockfile
renv::restore()
git pull
#Update packages.
renv::restore()

Developing and testing the API using plumber

Setting up and running plumber

https://www.rplumber.io/

restR.png

plumberR.png

Testing the API using Postman

Postman documentation overview | Postman Learning Center

postman.png

# Add any packages that might be needed.
library(plumber)
library(jsonlite)
library(httr)

# Plumber decoration that is used to configure the API including defining endpoint and what format the data is returned.

#* Echo the parameter that was sent in
#* @serializer unboxedJSON
#* @post /shaip

# The main function that receives the JSON body as req$body and returns the result as res$body. The @seriealizer converts the dataframe to JSON. 
# The JSON return body allows users to interact with the API using restful API standards.
# When developing the local API it can be useful to print the input data to see the format. The print output will show up in the RStudio console when the API is accessed.
# The plumber function provides a wrapper for any R code to be developed and any code within this function will run when the API is called.

function(req, res) {
test1 <- toJSON(req$body)    
test <- fromJSON(test1) 

print(test)    

res$body <- test
}

Summary

  1. Pull your code from your source repository to a local folder.

  2. Open the folder as a project in RStudio, make sure the Git option is available in RStudio.

    git.png

  3. Setup renv to manage packages.

  4. Open the file ./rAlgoAPI/API/rest_controller.R and use the ‘Run API’ button to activate the base code making sure to note the port number allocated to the local host API.

  5. Use Postman either via the web app with the downloaded agent or a local downloaded app to connect and run your API.

  6. You are now ready to start developing and testing a local version of your R Shaip.