Use Conda Environments

Conda is an open-source package and environment management system that enables the use of environments containing third-party R libraries.

Prerequisites

To run the OML4R embedded R execution REST APIs using conda environments, you need to save the R scripts you want to run in the OML4R script repository or have the R scripts granted from other users. The conda environment also needs to be created and uploaded to Object Storage by the ADMIN user.

Environment Used in Examples

ADMIN user is required to create and manage environments. The following creates the myrenv environment based on R version 4.0.5 and uploads it to the Object Storage owned by the PDB. The data visualization package ggplot2 is installed in the environment. The ggplot2 package uses the channel conda-forge.

create -n myrenv -c conda-forge r-base=4.0.5 r-ggplot2 
upload myrenv -t application OML4R

Obtain token

Exchange database user credential for an access token using Oracle Machine Learning User Management Cloud Service REST endpoint /oauth2/v1/token. For more information, see Authenticate.

$ curl -X POST --header 'Content-Type: application/json' --header 'Accept:
application/json'
-d '{"grant_type":"password", "username":"'${username}'", "password":"'${password}'"}'
"<oml-cloud-service-location-url>/omlusers/api/oauth2/v1/token"

Data that Some Scripts Use

Table creation

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

%r

ore.create(iris, table = 'IRIS')
ore.sync(table = "IRIS")
dim(IRIS)

Script Creation

test_ggplot2_noinp

The script imports the ggplot2 library and creates a distribution plot. The following defines the script and stores it in the script repository.

begin
  sys.rqScriptCreate('test_ggplot2_noinp',
    'function() {
      library(ggplot2);
      mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
      labels=c("3gears","4gears","5gears"));
      gf <- qplot(mpg, data=mtcars, geom="density",
      fill=gear, alpha=I(.5), main="Distribution of Gas Milage",
      xlab="Miles Per Gallon", ylab="Density");
      plot(gf);
      res <- "hello world"
      res
  }',FALSE,TRUE);
end;
/

The following cURL command runs the script test_ggplot2_noinp saved in the user's OML4R script repository using the REST API for embedded R execution. The conda environment name parameter envName is set to myrenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"envName": "r-env", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/do-eval/test_ggplot2_noinp"

The output contains both the PNG image and data returned by the script.

{
 "result":[
   {
    "IMAGE":"VJ3W6Y29saMw...................AAAABJRU5ErkJggg==",
    "DATA":"\"hello world\"",
    "ID":1
   }
  ]
}

test_ggplot2_inp

The script imports the ggplot2 library and creates a histogram plot with the input data. The following defines the script and stores it in the script repository.

begin
  sys.rqScriptCreate('test_ggplot2_inp',
    'function(dat) {
      library(ggplot2);
      histogram <- ggplot(data=dat, aes(x=Sepal.Width)) +
      geom_histogram(binwidth=0.2, color="black", aes(fill=Species)) +
      xlab("Sepal Width") + ylab("Frequency") + ggtitle("Histogram of
      Sepal Width")
      plot(histogram);
      res <- "hello world"
      res
    }',FALSE,TRUE);
end;
/

The following cURL command runs the script test_ggplot2_inp saved in the user's OML4R script repository using the REST API for embedded R execution. The conda environment name parameter envName is set to myrenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"input": "IRIS", "envName": "myrenv", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/table-apply/test_ggplot2_inp"

The output contains both the PNG image and data returned by the script.

{
  "result":[
       [
        {
         "IMAGE":"To+O0X2umWN27mfWA......Q91XUfJggg==",
         "DATA":"\"hello world\"",
         "ID":1,
         "CHUNK":"1"
        }
      ],
  ]
}

test_ggplot2_idx

The script imports the ggplot2 library and produces a histogram plot with title. The following defines the script and stores it in the script repository.

begin
  sys.rqScriptCreate('test_ggplot2_idx',
    'function(idx) {
      library(ggplot2);
      row <- iris[idx,]
      histogram <- ggplot(data=row, aes(x=Sepal.Width)) +
      geom_histogram(binwidth=0.2, color="black", aes(fill=Species)) +
      xlab("Sepal Width") + ylab("Frequency") + ggtitle("Histogram of
      Sepal Width")
      plot(histogram);
      res <- "hello world"
      res
  }',FALSE,TRUE);
end;
/

The following cURL command runs the script test_ggplot2_idx saved in the user's OML4R script repository using the REST API for embedded R execution. The conda environment name parameter envName is set to myrenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"times": 2, "envName": "myrenv", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/index-apply/test_ggplot2_idx"

The output is similar to the following:

{
 "result":
   [
    [
     {
      "IMAGE":"iVBAElFTkS..................uQmCC",
      "DATA":"\"hello world\"",
      "TIME":"1",
      "ID":1
     }
    ],
    [
     {
      "IMAGE":"iVBORw................AElFTkSuQmCC",
      "DATA":"\"hello world\"",
      "TIME":"2",
      "ID":1
     }
    ]
 ]
}