R Client
For an R model to be deployed to Promote, the model will need to include information from the Promote library. The library includes a number of methods used in the various stages of deployment.
Please visit our Github repository for more information on how to deploy models using the R client.
promote.library: List the libraries that are required for model deployment.
Package installation
Packages can be installed from CRAN or from Github using devtools.
promote.library <- function(name, src="CRAN", version=NULL, user=NULL, install=TRUE)
name: The name of the package to be added.
src: Optionally indicate the source from which the package will be installed on Promote, such as github or CRAN.
version: Optionally indicate the version of the package to be added.
user: Optionally indicate the Github username associated with the package
install: Optionally indicate if the package should be installed on the Promote server. Set this to False if the package is already part of the base image.
promote.library("MASS")
promote.library(c("rjson", "stringr"))
promote.library("cats", src="github", user="hilaryparker")
promote.library("hilaryparker/cats")
promote.library("my_proprietary_package", install=FALSE)
promote.metadata: A method that assigns related metadata to the model so the metadata is visible from within the Promote platform. Metadata is assigned individually, with a maximum of six metadata values per model.
promote.metadata("name",value)
name: The name of the metadata.
value: A string or object that displays in Promote. Must be 50 characters or less.
promote.metadata("aic",my.glm$aic)
promote.unload: Remove the libraries that are not required for deployment from the model's dependency list.
promote.unload("name")
name: The name of the package to be excluded during deployment.
promote.unload("ggplot2")
model.predict: Process received prediction requests. The API endpoint receives an API request, executes the prediction code, and formats the response.
input: The API reads in a data frame input.
output: The API returns a data frame output.
model.predict <- function(data) {
#LMModel, a linear regression model trained previously, is executed
pred <- predict(LMModel, newdata=data.frame(data))
result <- data.frame(pred)
print(pred)
}
promote.config: Configure the user authentication and URL to deploy the model to Promote.
install.packages("promote")
library(promote)
promote.config <- c(
username="kermit",
apikey="123456789101112131415",
env="https://promote.yourcompany.com"
)
promote.deploy: Deploy the model and related R Dependencies to the specified Promote URL. The deployed model can be called from any programming language via a REST API.
promote.deploy(model_name)
model_name: The name of the model.
promote.config <- c(
username = "your username",
apikey = "your apikey",
env = "https://promote.yourcompany.com"
)
#simple example model
iris$Sepal.Width_sq <- iris$Sepal.Width^2
fit <- glm(I(Species)=="virginica" ~ ., data=iris)
model.require <- function() {
require("randomForest")
}
model.predict <- function(df) {
data.frame("prediction"=predict(fit, df, type="response"))
}
promote.deploy("irisModel")
promote.predict: Send a request to the model for a prediction.
promote.predict(model_name = , data = , model_owner = , raw_input = , silent = )
model_name: The name of the prediction model.
data: The JSON data to be sent to the model.
model_owner: Optionally indicate the name of the model owner.
raw_input: Optionally indicate if data returned by the model is coerced into a data.frame. Be default, the data is returned as a data.frame.
silent: Optionally indicate if the request url is printed to the console. By default, the url is returned.
data <- rjson::fromJSON('{"name":"Sam"}')
promote.predict("HelloWorld",data)
#Output:
# greeting
#1 Hello Sam!
#################### model_owner and silent example #############################
# send request to another users model, with silent = FALSE
promote.predict("HelloWorld",data, 'brandon',silent = FALSE)
# Output:
# https://promote.yourcompany.com/brandon/models/HelloWorld/?username=colin&apikey=123455667781238912389
#greeting
#1 Bye Sam!
############################## raw_input example ################################
jsonTest <- '{
"name": "colin",
"favorite_nums": [1, 2, 3, 4, 5],
"favorite_words": ["cat", "dog", "mouse",null]
}'
model.require <- function(){
library("RJSONIO")
}
# If we want to manually preprocess the JSON data
model.predict <- function(data){
data <- list(json=data)
# Below, parsing our JSON object manually with RJSONIO
d <-RJSONIO::fromJSON(data$json,nullValue = 'my_na_value')
}
promote.deploy('non_tabular_JSON')
# this will fail:
promote.predict('non_tabular_JSON',jsonTest)
# this will succeed because we are sending non-tabular data:
promote.predict('non_tabular_JSON',jsonTest,raw_input = TRUE)
Use RPostgreSQL for R to connect to an external database.
For R models, data parsing is done with the package rjson. Prior to model.predict being called, the data is parsed and transformed into an R object.
This means that an R object is actually being passed to model.predict.
Generally, data is processed in the following sequence:
- API request
- data.frame(jsonlite::fromJSON(data),stringsAsFactors=TRUE)
- model.predict(data)
- rjson::toJSON()
- response
In Step 2, jsonlite can be changed to use rjson by changing the JSONLITE environment variable to equal FALSE in the model's Advanced tab within the Promote web application.
# below (for examples sake) is incoming JSON parsed BEFORE it gets sent to model.predict()
#API Requests --> model.predict(rjson::fromJSON(df))
model.predict <- function(df) {
lg <- df
m <- lapply(lg[[1]]$users, function(x) c(paste(x$user['name'],sample(1:2,1)), x$user['user_id'], x['ts']))
m <- do.call(rbind, m)
# below, we split the dataframe so it is returned as JSON arrays
m <- unname(split(m, 1:nrow(m)))
m
}
# rjson::toJSON() --> API Response
Local test
To locally test JSON data, parsing must be done in the Promote model.