4.4 Using Conda Environments with the SQL and REST APIs for Embedded R execution

This topic explains usage of conda environment by running the user-defined functions (UDFs) in SQL and REST APIs for embedded R execution.

Running UDFs in the SQL and REST APIs for embedded R execution

The conda environments can be used by OML4R R, SQL, and REST APIs. To use the SQL and REST API for embedded R execution, the following information is needed.

  1. The token URL from the OML service console in Autonomous Database. For more information on how to obtain the token URL and set the access token see Access and Authorization Procedures and Functions (Autonomous Database).

  2. A script containing a user-defined R function in the Oracle Machine Learning for R (OML4R) script repository. For information on creating a script and saving it to the script repository, see Manage Scripts in R.

Note:

To use a conda environment when calling OML4R script execution endpoints, specify the conda environment in the env_name field when using SQL, and the envName field when using REST.

Run the R UDF using the SQL API for Embedded R Execution - Synchronous mode.

Run a SELECT statement that calls the rqEval2 function. The PAR_LST argument specifies the special control argument ore_graphics_flag to true so that the web server can capture images rendered in the invoked script. In the OUT_FMT argument, the string 'PNG', specifies that the table returns the response in a table with fixed columns (including an image bytes column). The SCR_NAME parameter specifies the function multi.forecast stored in the script repository. The ENV_NAME specifies the environment name myrenv in which the script is called.

%script

SELECT name, id, value, dbms_lob.substr(image,100,1) image 
FROM table(rqEval2(
    par_lst => '{"ore_graphics_flag":true}',
    out_fmt => 'PNG',
    scr_owner=> NULL,
    scr_name => 'multi.forecast',
    env_name => 'myrenv'));

The output is similar to the following:

NAME ID VALUE IMAGE 
1 [[0.4425,8.8345,6.3514,0.1377,2.8709,0.2175,0.0039,"NA"],[6.849,26.4716,19.5012, 89504E470D0A1A0A0000000D49484452000001E0000001E008060000007DD4BE950000200049444154789CECDD7798646599F7F1EFA99C3B4D6402306406040902CA0A282A410517D41103088BA8BB882BAEAE2B665D71511731A02F8318D015C54436EF ---------------------------

Run the R UDF using the REST API for embedded R execution

The following example runs the script named multi.forecast from the OML4R REST API for embedded R execution using the do-eval endpoint. The environment name parameter envName is set to myrenv. The graphicsFlag parameter is set to true to return the PNG image and the data from the function in JSON format.

$ curl -i -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json'
       --header 'Accept: application/json' -d '{"input":"DF", "envName":"myrenv", "graphicsFlag":true}'
       "${omlserver}/oml/api/r-scripts/v1/do-eval/multi.forecast"