Using 3rd Party R Packages

Installing from CRAN

We're going to use randomForest for feature selection, so go ahead and install it and load it into your workspace.


Build Your Model

We're going to train a model to classify flower species form the iris dataset. We're going to try and predict Species using all the other columns in the dataset.

fit <- randomForest(Species ~ ., data=iris)
 randomForest(formula = Species ~ ., data = iris)
               Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 2

        OOB estimate of  error rate: 4%
Confusion matrix:
           setosa versicolor virginica class.error
setosa         50          0         0        0.00
versicolor      0         47         3        0.06
virginica       0          3        47        0.06

Prepping for Deployment

Deploying to ŷhat is simple in this case. Just include the randomForest in the model.require function. Add a line of code to make your predictions in the model.predict function.

The input to model.transform will be a "raw" record (a new flower record) coming from the API. The output of this function will be sent into the model.predict function.



# NOTE: model.require is deprecated as of yhatr 0.13.9
# model.require <- function() {
#   library(randomForest)
# }

model.predict <- function(df) {
  data.frame("predictedSpecies"=predict(fit, newdata=df))

Deploying to Yhat

The actual deployment is totally painless. Just add your username and apikey to the R session and execute the yhat.deploy command.

yhat.config  <- c(


You can now make predictions using the ŷhat API. Give it a try using the R function yhat.predict. You can instantly validate that your results are the same on your local machine as they are on the ŷhat API.

yhat.predict(model_name="irisPredictor", iris[1,])
predict(fit, iris[1,])


This function is available starting with yhatr 0.13.9

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


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


  • 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.


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

results matching ""

    No results matching ""