9.6.2.4 rqEval2 Function

The function rqEval2 when used in Oracle Autonomous Database, runs a user-defined R function that explicitly retrieves data or for which external data is to be automatically loaded for the function.

The function rqEval2 runs the R function in the script specified by the SCR_NAME parameter.

Syntax

rqEval2 (
    PAR_LST VARCHAR2,
    OUT_FMT VARCHAR2,
    SCR_NAME VARCHAR2,
    SCR_OWNER VARCHAR2 DEFAULT NULL,
    ENV_NAME  VARCHAR2 DEFAULT NULL
    )

Parameters

Parameter Description

PAR_LST

A JSON string that contains additional parameters to pass to the user-defined R function specified by the SCR_NAME parameter. Special control arguments, which start with ore, are not passed to the function specified by SCR_NAME, but instead control what happens before or after the invocation of the function.

For example, to omit rows with missing values from input table, use: '{"ore.na.omit":true}'

See also: Special Control Arguments.

OUT_FMT

The format of the output returned by the function. It can be one of the following:

  • A JSON string that specifies the column names and data types of the table returned by the function. Any image data is discarded.
  • The string 'JSON', which specifies that the table returned contains a CLOB that is a JSON string.
  • The string 'XML', which specifies that the table returned contains a CLOB that is an XML string. The XML can contain both structured data and images, with structured or semi-structured R objects first, followed by the image or images generated by the R function.
  • The string 'PNG', which specifies that the table returned contains a BLOB that has the image or images generated by the R function. Images are returned as a base 64 encoding of the PNG representation.

See also: Output Formats.

SCR_NAME

The name of a user-defined R function in the OML4R script repository.

SCR_OWNER

The owner of the R script. The default value is NULL. If NULL, will search for the R script in the user's script repository.

ENV_NAME

The name of the conda environment that should be used when running the named user-defined R function.

Return Value

Function rqEval2 returns a table that has the structure specified by the out_fmt parameter value.

Examples

Example 9-31 Using rqEval2

This example defines a R function and stores it in the OML4R script respository. The PL/SQL block, creates the script RandomRedDots2 and add it to the script repository. Specify that the script is private and overwrite the script with the same name. It calls the rqEval2 function on the user defined R function.


BEGIN
  sys.rqScriptCreate('RandomRedDots2',
    '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)}',
       v_global => FALSE,
       v_overwrite => TRUE);
END;
/

Example 9-32 JSON Output

The PAR_LST argument specifies using LOW service level with the special control argument oml_service_level. In the OUT_FMT argument, the string 'JSON', specifies that the table returned contains a CLOB that is a JSON string. The SCR_NAME parameter specifies the RandomRedDots2 function in the script repository as the R function to call. The JSON output is a CLOB. You can call set long [length] to get more output.

%script
set long 500
SELECT * FROM table(rqEval2(
    par_lst => '{"ore_service_level":"LOW"}',
    out_fmt => 'JSON',
    scr_name => 'RandomRedDots2'));

The result is:

NAME VALUE 
[{"val":0.01,"id":1},{"val":0.02,"id":2},{"val":0.03,"id":3},{"val":0.04,"id":4},{"val":0.05,"id":5},{"val":0.06,"id":6},{"val":0.07,"id":7},{"val":0.08,"id":8},{"val":0.09,"id":9},{"val":0.1,"id":10}]

Example 9-33 PNG Output.

The PAR_LST argument specifies using LOW service level with the special control argument oml_service_level. In the OUT_FMT argument, the string 'PNG' to include images returned by reqEval2. The SCR_NAME parameter specifies the RandomRedDots2 function in the script repository as the R function to call. The JSON output is a CLOB.

%script
SELECT * FROM table(rqEval2(
    par_lst => '
    {"ore_graphics_flag":true, "ore_service_level":"LOW"}',
    out_fmt => 'PNG',
    scr_name => 'RandomRedDots2'));

The result is:

---------------------------
NAME   ID   VALUE   IMAGE   
       1            89504E470D0A1A0A0000000D49484452000001E0000001E008060000007DD4BE950000200049444154789C

Note:

Here, only a portion of the output is shown. To determine the length of the output use the parameter set long [length].

Example 9-34 XML Output.

The PAR_LST argument specifies using LOW service level with the special control argument oml_service_level. In the OUT_FMT argument, the string 'JSON', specifies that the table returned contains a CLOB that is a JSON string. The SCR_NAME parameter specifies the RandomRedDots2 function in the script repository as the R function to call. The JSON output is a CLOB.

%script
set long 1000
SELECT * FROM table(rqEval2(
    par_lst => '{"ore_service_level":"LOW"}',
    out_fmt => 'XML',
    scr_name => 'RandomRedDots2'));

The result is:

NAME VALUE
      <root><frame_obj><ROW-frame_obj><id>1</id><val>0.01</val></ROW-frame_obj><ROW-frame_obj><id>2</id><val>0.02</val></ROW-frame_obj><ROW-frame_obj><id>3</id><val>0.03</val></ROW-frame_obj><ROW-frame_obj><id>4</id><val>0.04</val></ROW-frame_obj><ROW-frame_obj><id>5</id><val>0.05</val></ROW-frame_obj><ROW-frame_obj><id>6</id><val>0.06</val></ROW-frame_obj><ROW-frame_obj><id>7</id><val>0.07</val></ROW-frame_obj><ROW-frame_obj><id>8</id><val>0.08</val></ROW-frame_obj><ROW-frame_obj><id>9</id><val>0.09<

Note:

Here, only a portion of the output is shown. To determine the length of the output use the parameter set long [length]. The set long 1000 gives the complete output.

Example 9-35 XML Output

Run the Select statement to get an XML output. ore_graphics_flag is set to true so that both structured data and images are included in the XML

%script


set long 1000

SELECT * FROM table(rqEval2(
    par_lst => '{"ore_graphics_flag":true, "ore_service_level":"LOW"}',
    out_fmt => 'XML',
    scr_name => 'RandomRedDots2'));

The result is:


---------------------------
NAME   VALUE                
<root><R-data><frame_obj><ROW-frame_obj><id>1</id><val>0.01</val></ROW-frame_obj><ROW-frame_obj><id>2</id><val>0.02</val></ROW-frame_obj><ROW-frame_obj><id>3</id><val>0.03</val></ROW-frame_obj><ROW-frame_obj><id>4</id><val>0.04</val></ROW-frame_obj><ROW-frame_obj><id>5</id><val>0.05</val></ROW-frame_obj><ROW-frame_obj><id>6</id><val>0.06</val></ROW-frame_obj><ROW-frame_obj><id>7</id><val>0.07</val></ROW-frame_obj><ROW-frame_obj><id>8</id><val>0.08</val></ROW-frame_obj><ROW-frame_obj><id>9</id><val>0.09</val></ROW-frame_obj><ROW-frame_obj><id>10</id><val>0.1</val></ROW-frame_obj></frame_obj></R-data><images><image><img src="data:image/pngbase64"><![CDATA[iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAYAAAB91L6VAAAgAElEQVR4nOzdd3hT5fvH8Xe6m6QDSgtl07KRjciQIZvKkiECCspQBNkiICBLUBRxACoqU0RAkT0EREGGTCm77LJll2Z0pc/vD9Qf8u04QJPTcb+uq9clzZM8n9Qkd845zzAopRRCCCGEcCk3vQMIIYQQOZEUYCGEEEIHUoCFEEIIHUgBFkIIIXQgBVgIIYTQgRRgIYQQQgdSgIUQQggdSAEWQgghdCAFWAghhNCBFGAhhBBCB1KAhRBCCB1IARZCCCF0IAVYCCGE0IEUYCGEEEIHUoCFEEIIHU 

Example 9-36 Relational Output

Run the Select statement to get a Relational output. The OUT_FMT argument specifies a JSON string that contains the column names and data types of the table returned by rqEval2.

%script
SELECT * FROM table(rqEval2(
    par_lst => '{"ore_service_level":"LOW"}',
    out_fmt => '{"val":"NUMBER","id":"NUMBER"}',
    scr_name => 'RandomRedDots2'));

The result is:

val id 
0.01 1 
0.02 2 
0.03 3 
0.04 4 
0.05 5 
0.06 6 
0.07 7 
0.08 8 
0.09 9 
0.1 10 

10 rows selected.

Example 9-37 Passing arguments using rqEval2:

Run the Select statement to get an XML output by passing arguments to the rqEval2 function.

%script
set long 500
SELECT * FROM table(rqEval2(
        par_lst => '{"ore_service_level":"LOW", "divisor":50, "numDots":500}',
        out_fmt => 'XML',
        scr_name => 'RandomRedDots2'));

The result is:

NAME VALUE
       <root><frame_obj><ROW-frame_obj><id>1</id><val>0.01</val></ROW-frame_obj><ROW-frame_obj><id>2</id><val>0.02</val></ROW-frame_obj><ROW-frame_obj><id>3</id><val>0.03</val></ROW-frame_obj><ROW-frame_obj><id>4</id><val>0.04</val></ROW-frame_obj><ROW-frame_obj><id>5</id><val>0.05</val></ROW-frame_obj><ROW-frame_obj><id>6</id><val>0.06</val></ROW-frame_obj><ROW-frame_obj><id>7</id><val>0.07</val></ROW-frame_obj><ROW-frame_obj><id>8</id><val>0.08</val></ROW-frame_obj><ROW-frame_obj><id>9</id><val>0.09<

The following code runs a SQL query to retrieve all columns from the result of an rqEval2 function call, which generates a PNG output based on a specified script and environment.

select *
from table(rqEval2(
par_lst => '{"ore_graphics_flag":true}',
out_fmt => 'PNG',
scr_name => 'test_ggplot2_noinp',
scr_owner => 'OMLUSER',
env_name => 'myrenv'));

The output appears as follows:

NAME ID VALUE IMAGE
---------- ---------- -------------------- ------------------------------
1 "Hello World" 89504E470D0A1A0A0000000D494844
                52000001E0000001E008060000007D
                D4BE950000200049444154789CECDD
                795854F51EC7F1F79919F655164190
                4D0141DC01714D3335772D5B6CB72C
                AB9B65FBDE