R CLI

When constructing a model, you will need to add a special "Yhat" part to your model. This is done by importing the yhatr library for R users, or the yhat package for python.

Pre-deployment

  • yhat.library - list libraries required for deployment
  • yhat.unload - remove libraries not required for deployment
  • model.predict - the function that will process prediction requests

Deployment

  • yhat.config - the authentication and URL to deploy the model to ScienceOps
  • yhat.deploy - deploy the model and dependencies to ScienceOps specified in the yhat.config

Post-deployment

  • yhat.predict - send a request to the model for a prediction

yhat.library()

List any packages you need to execute your model. Packages can be installed from CRAN or from Github using devtools

Usage

yhat.library <- function(name, src="CRAN", version=NULL, user=NULL, install=TRUE)

Arguments

  • name: name of the package to be added
  • src: (optional): source from which the package will be installed on ScienceOps (github or CRAN)
  • version: (optional): version of the package to be added
  • user: (optional): Github username associated with the package
  • install: (optional): Whether the package should also be installed on the ScienceOps server; this is typically set to False when the package has already been added to the ScienceOps base image.

Examples

yhat.library("MASS")
yhat.library(c("rjson", "stringr"))
yhat.library("cats", src="github", user="hilaryparker")
yhat.library("hilaryparker/cats")
yhat.library("my_proprietary_package", install=FALSE)

yhat.unload()

Removes a library from the Yhat model's dependency list, for example a library that is only related to plotting

Arguments

  • name: name of the package to be excluded during deployment

Examples

 yhat.unload("ggplot2")

model.predict()

This is the core of the API endpoint

The model.predict function receives the the API request, executes the necessary code to make a prediction, and formats the response. In the example below, we invoke our fit model LMModel and then format the response into a data.frame.

Arguments

  • input: type: data frame
  • output: type: data frame

Example

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)
}

yhat.config()

Set the configuration/authentication for your user

Sets the authentication for deploying and sending predictions to ScienceOps.

Example:

install.packages("yhatr")
library(yhatr)

yhat.config  <- c(
  username="kermit",
  apikey="123456789101112131415",
  env="https://sandbox.yhathq.com/"
)

yhat.deploy()

Deploy a model to ScienceOps

This function captures the model.predict function and deploys a model on ScienceOps which can be called from any programming language via a REST API.

Usage

yhat.deploy(model_name)

Arguments

  • model_name: the name of your model

Examples

yhat.config <- c(
 username = "your username",
 apikey = "your apikey",
 env = "http://sandbox.yhathq.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"))
}

yhat.deploy("irisModel")

yhat.predict()

Send data to a model via REST API request from R for a prediction.

Usage

yhat.predict(model_name = , data = , model_owner = , raw_input = , silent = )

Arguments

  • model_name: the name of the model to send the request to
  • data: JSON data to be sent to the model
  • model_owner: (optional): the name of the model owner
  • raw_input: (optional): If TRUE, data returned by the model will not be coerced into a data.frame
  • silent: (optional): Defaults to FALSE. If TRUE, url of request will be printed to the console.

Examples

data <- rjson::fromJSON('{"name":"Sam"}')
yhat.predict("HelloWorld",data)

#Output:
#       greeting
#1 Hello Sam!

#################### model_owner and silent example #############################

# send request to another users model, with silent = FALSE
yhat.predict("HelloWorld",data, 'brandon',silent = FALSE)

# Output:

# https://sandbox.yhathq.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')
}

yhat.deploy('non_tabular_JSON')

# this will fail:
yhat.predict('non_tabular_JSON',jsonTest)

# this will succeed because we are sending non-tabular data:
yhat.predict('non_tabular_JSON',jsonTest,raw_input = TRUE)

model.require is deprecated in yhatr 0.13.9. If you are using yhatr > 0.13.9, please use yhat.library to specify model dependencies."

model.require() DEPRECATED

Define any packages you need to execute your model. These will be automatically installed on your Yhat server when your model is deployed.

Arguments

None

Example

model.require <- function() {
  # accepts any package loading format
  library(randomForest)
  library("plyr")
  require("reshape2")
}

results matching ""

    No results matching ""