9.3.3 Use the ore.doEval Function
The ore.doEval
function runs the specified input function
using data that is generated by the input function.
It returns an ore.frame
object or a serialized R object as an ore.object
object.
The syntax of the ore.doEval
function is the following:
ore.doEval(FUN, ..., FUN.VALUE = NULL, FUN.NAME = NULL, FUN.OWNER = NULL)
Example 9-2 Using the ore.doEval Function
In this example, RandomRedDots
gets a function that has an argument and that returns a data.frame
object that has two columns and that plots 50 random normal values. The example then calls ore.doEval
function and passes it the RandomRedDots
function
object. The image is displayed at the client, but it is generated by the database server R engine that runs the RandomRedDots
function.
%r
res <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
FUN.VALUE= data.frame(id = 1, val = 1), ore.graphics=FALSE)
class(res)
The result is:
'ore.frame'
Listing for This Example
R> RandomRedDots <- function(divisor = 100){ + id<- 1:10 + plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2 ) + data.frame(id=id, val=id / divisor) + } R> ore.doEval(RandomRedDots) id val 1 1 0.01 2 2 0.02 3 3 0.03 4 4 0.04 5 5 0.05 6 6 0.06 7 7 0.07 8 8 0.08 9 9 0.09 10 10 0.10
Example 9-3 Using the ore.doEval Function with an Optional Argument
You can provide arguments to the input function as optional arguments to the doEval
function. This example calls the doEval
function with an optional argument that overrides the divisor
argument of the RandomRedDots
function.
ore.doEval(RandomRedDots, divisor = 50)
Listing for This Example
R> ore.doEval(RandomRedDots, divisor = 50) id val 1 1 0.02 2 2 0.04 3 3 0.06 4 4 0.08 5 5 0.10 6 6 0.12 7 7 0.14 8 8 0.16 9 9 0.18 10 10 0.20 # The graph displayed by the plot function is not shown.
Example 9-4 Using the ore.doEval Function with the FUN.NAME Argument
If the input function is stored in the OML4R script repository, then you can invoke the ore.doEval
function with the FUN.NAME
argument. This example first calls ore.scriptDrop
to ensure that the script repository does not contain a script with the name myRandomRedDots
. The example adds the RandomRedDots
function from Example 9-2 to the repository under the name myRandomRedDots
. This example calls the ore.doEval
function and specifies myRandomRedDots
. The result is assigned to the variable res
.
The return value of the RandomRedDots
function is a data.frame
but in this example the ore.doEval
function returns an ore.object
object. To get back the data.frame
object, the example calls ore.pull
to pull the result to the client R session.
ore.scriptDrop("myRandomRedDots") ore.scriptCreate("myRandomRedDots", RandomRedDots) res <- ore.doEval(FUN.NAME = "myRandomRedDots", divisor = 50) class(res) res.local <- ore.pull(res) class(res.local)
Listing for This Example
R> ore.scriptDrop("myRandomRedDots") R> ore.scriptCreate("myRandomRedDots", RandomRedDots) R> res <- ore.doEval(FUN.NAME = "myRandomRedDots", divisor = 50) R> class(res) [1] "ore.object" attr(,"package") [1] "OREembed" R> res.local <- ore.pull(res) R> class(res.local) [1] "data.frame"
Example 9-5 Using the ore.doEval Function with the FUN.VALUE Argument
To have the
doEval
function return an ore.frame
object instead of an
ore.object
, use the argument FUN.VALUE
to specify the
structure of the result, as shown in this
example.
%r
res <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
FUN.VALUE= data.frame(id = 1, val = 1), ore.graphics=FALSE)
class(res)
The ouput is similar to the following:
'ore.frame'
Listing for Example 9-5R> res.of <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50, + FUN.VALUE= data.frame(id = 1, val = 1), ore.graphics=FALSE) R> class(res.of) [1] "ore.frame" attr(,"package") [1] "OREbase"
Example 9-6 Using the doEval Function with the ore.connect Argument
This example demonstrates using the special optional argument ore.connect
to connect to the database in the embedded R function, which enables the use of objects stored in a datastore. The example creates the RandomRedDots2
function object, which is the same as the RandomRedDots
function from Example 9-2 except the RandomRedDots2
function has an argument that takes the name of a datastore. The example creates the myVar
variable and saves it in the datastore named datastore_1
. The example then calls the doEval
function and passes it the name of the datastore and passes the ore.connect
control argument set to TRUE
.
%r
RandomRedDots2 <- function(divisor = 100, dsname = "ds-1"){
id <- 1:10
plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2 )
ore.load(dsname) # Contains the numeric variable myVar.
data.frame(id = id, val = id / divisor, num = myVar)
}
myVar <- 5
ore.save(myVar, name = "ds-1", overwrite=TRUE)
ore.doEval(RandomRedDots2, dsname="ds-1", ore.connect=TRUE)
The output is similar to the following:
id val num
1 1 0.01 5
2 2 0.02 5
3 3 0.03 5
4 4 0.04 5
5 5 0.05 5
6 6 0.06 5
7 7 0.07 5
8 8 0.08 5
9 9 0.09 5
10 10 0.10 5
Listing for This Example
R> RandomRedDots2 <- function(divisor = 100, datastore.name = "myDatastore"){ + id <- 1:10 + plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2 ) + ore.load(datastore.name) # Contains the numeric variable myVar. + data.frame(id = id, val = id / divisor, num = myVar) + } R>myVar <- 5 R>ore.save(myVar, name = "ds-1", overwrite=TRUE) R> ore.doEval(RandomRedDots2, datastore.name = "datastore_1", ore.connect = TRUE) id val num 1 1 0.01 5 2 2 0.02 5 3 3 0.03 5 4 4 0.04 5 5 5 0.05 5 6 6 0.06 5 7 7 0.07 5 8 8 0.08 5 9 9 0.09 5 10 10 0.10 5 # The graph displayed by the plot function is not shown.
Example 9-7 Using the ora.type Attribute
This example demonstrates using the ora.type
attribute to specify database data types of CLOB and BLOB for columns in the data.frame
object specified by the FUN.VALUE
argument.
%r
# NOTE FROM SL: I added spaces between each example
eval1 <- ore.doEval
eval2 <-
ore.doEval(function()
data.frame(x = "Hello, world", stringsAsFactors = FALSE))
eval3 <-
ore.doEval(function()
data.frame(x = "Hello, world", stringsAsFactors = FALSE),
FUN.VALUE = data.frame(x = character(), stringsAsFactors = FALSE))
out.df <- data.frame(x = character(), y = raw(), stringsAsFactors = FALSE)
attr(out.df$x, "ora.type") <- "clob"
attr(out.df$y, "ora.type") <- "blob"
eval4 <-
ore.doEval(function() {
res <- data.frame(x = "Hello, world",stringsAsFactors = FALSE)
res$y[[1L]] <- charToRaw("Hello, world")
res},
FUN.VALUE = out.df)
eval1
class(eval1) # ore.object
eval2
class(eval2) # ore.object
eval3
class(eval3) # ore.frame
eval4$x
rawToChar(ore.pull(eval4$y))
The output is similar to the following:
Table 9-5 A data.frame: 1 x 1
x |
---|
<chr> |
Hello, world |
Table 9-6 A data.frame: 1 x 1
x |
---|
<chr> |
Hello, world |
Listing for This Example
R> eval1 <- ore.doEval(function() "Hello, world") R> eval2 <- + ore.doEval(function() + data.frame(x = "Hello, world", stringsAsFactors = FALSE)) R> eval3 <- + ore.doEval(function() + data.frame(x = "Hello, world", stringsAsFactors = FALSE), + FUN.VALUE = + data.frame(x = character(), stringsAsFactors = FALSE)) R> out.df <- data.frame(x = character(), y = raw(), stringsAsFactors = FALSE) R> attr(out.df$x, "ora.type") <- "clob" R> attr(out.df$y, "ora.type") <- "blob" R> eval4 <- + ore.doEval(function() { + res <- data.frame(x = "Hello, world",stringsAsFactors = FALSE) + res$y[[1L]] <- charToRaw("Hello, world") + res}, + FUN.VALUE = out.df) R> eval1 [1] "Hello, world" R> class(eval1) [1] "ore.object" attr(,"package") [1] "OREembed" R> eval2 x 1 Hello, world R> class(eval2) [1] "ore.object" attr(,"package") [1] "OREembed" R> eval3 x 1 Hello, world Warning message: ORE object has no unique key - using random order R> class(eval3) [1] "ore.frame" attr(,"package") [1] "OREbase" R> eval4$x [1] "Hello, world" Warning message: ORE object has no unique key - using random order R> rawToChar(ore.pull(eval4$y)) [1] "Hello, world" Warning message: ORE object has no unique key - using random order
Parent topic: R Interface for Embedded R Execution