Scripts Used in Examples

These scripts contain the R code for creating the user-defined functions that are used in the REST API for Embedded R Execution examples. For use in REST API for Embedded R Execution function endpoints, a script must be saved in the Oracle Machine Learning for R (OML4R) script repository as a string, rather than as a R function.

For more examples of creating scripts and of using OML4R functions, see the OML Notebooks in your Autonomous Database service.

Data that Some Scripts Use

The following R code creates the IRIS data frame proxy object, which contains data that some of the scripts use.

# Load libraries and suppress warnings
library(ORE)
options(ore.warn.order=FALSE)
# Drop IRIS table if it already exists
try(ore.drop(table="IRIS"))
ore.create(iris, table = "IRIS")
# print the first 6 lines of IRIS
head(IRIS)

compute.randomMean

The compute.randomMean function returns the mean of a set of random numbers. The following defines the script and stores it in the script repository.

compute.randomMean <- function(index)
 {
  set.seed(index)
  x <- rnorm(10)
  res <- mean(x)
  res
   }
ore.scriptCreate("compute.randomMean", compute.randomMean, global=TRUE, overwrite=TRUE)

group.count

The group.count function returns the counts for each category in the Species column. This example creates a group.count function and saves it in the script repository. The function uses data from the IRIS object.


group.count <- function(dat){
  x <- data.frame(table(dat$Species))
  names(x) <- c("Species", "Count")
  x
}
ore.scriptCreate("group.count", FUN=group.count, global=TRUE, overwrite=TRUE)

my.predict

The my.predict function returns a prediction based on a LinearRegression model.

The following creates a LinearRegression model and stores it in an OML4R datastore. It then defines the my.predict function and stores it in the script repository.

iris = ore.pull(IRIS)
regr = lm(Sepal.Length ~ ., data=iris)
coef(regr)
ore.save(regr, "regr", name="ds.regr", overwrite=TRUE)
my.predict <- function(dat)
  {
    ore.load(name="ds.regr")
    pred = cbind(PRED=predict(regr, newdata=dat, supplemental.cols="Sepal.Length"), dat["Sepal.Length"])
    pred
    }
ore.scriptCreate("my.predict",
                  my.predict,
                  global=TRUE,
                  overwrite=TRUE)

RandomRedDots

The RandomRedDots function creates a simple data.frame and generates a plots of random red dots. This example defines the function and stores it as a script in the script repository.


RandomRedDots <- function(divisor = 100, numDots = 100) {
  id <- 1:10
  plot(1:numDots, rnorm(numDots), pch = 21, bg = "red", cex = 2 )
  data.frame(id = id, val = id / divisor)}
ore.scriptCreate("RandomRedDots", FUN=RandomRedDots, global=TRUE, overwrite=TRUE)