10.3.3 ore.doEval関数の使用

ore.doEval関数は、入力関数によって生成されたデータを使用して指定された入力関数を実行します。

ore.frameオブジェクトまたはシリアライズRオブジェクトをore.objectオブジェクトとして返します。

ore.doEval関数の構文は次のとおりです。

ore.doEval(FUN, ..., FUN.VALUE = NULL, FUN.NAME = NULL, FUN.OWNER = NULL)

例10-2 ore.doEval関数の使用方法

この例では、RandomRedDotsによって、1つの引数を取り、2つの列を持つdata.frameオブジェクトを返し、50個のランダム標準値を表示する関数を取得します。次に、ore.doEval関数をコールして、RandomRedDots functionオブジェクトを渡します。イメージがクライアントに表示されますが、これはRandomRedDots関数を実行したデータベース・サーバーのRエンジンによって生成されます。

%r

res <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
                     FUN.VALUE= data.frame(id = 1, val = 1), ore.graphics=FALSE)
class(res)

結果は次のとおりです:

'ore.frame'

この例のリスト

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

図10-1 ランダムな赤い点の表示

図10-1の説明が続きます
「図10-1ランダムな赤い点の表示」の説明

例10-3 オプションの引数を指定したore.doEval関数の使用方法

doEval関数のオプションの引数として入力関数に引数を指定できます。この例では、RandomRedDots関数のdivisor引数をオーバーライドするオプションの引数を指定してdoEval関数をコールします。

ore.doEval(RandomRedDots, divisor = 50)

この例のリスト

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.

例10-4 FUN.NAME引数を指定したore.doEval関数の使用方法

入力関数がOML4Rスクリプト・リポジトリに格納されている場合は、ore.doEval関数をFUN.NAME引数を指定して呼び出すことができます。この例ではまず、myRandomRedDotsという名前のスクリプトがスクリプト・リポジトリに含まれないようにするために、ore.scriptDropをコールします。この例では、例10-2RandomRedDots関数をmyRandomRedDotsという名前でリポジトリに追加します。この例では、ore.doEval関数をコールし、myRandomRedDotsを指定します。結果は変数resに割り当てられます。

RandomRedDots関数の戻り値はdata.frameですが、この例では、ore.doEval関数はore.objectオブジェクトを返します。data.frameオブジェクトを取得するために、ore.pullをコールして結果をクライアントのRセッションにプルします。

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)

この例のリスト

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"

例10-5 FUN.VALUE引数を指定したore.doEval関数の使用方法

doEval関数でore.objectではなくore.frameオブジェクトが返されるようにするには、この例に示すように、引数FUN.VALUEを使用して結果の構造を指定します。

%r

res <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
                     FUN.VALUE= data.frame(id = 1, val = 1), ore.graphics=FALSE)
class(res)

出力は、次のようなものです。

'ore.frame'
例10-5のリスト
R> 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"

例10-6 ore.connect引数を指定したdoEval関数の使用方法

この例では、特殊なオプションの引数ore.connectを使用して埋込みR関数でデータベースに接続することにより、データストアに格納されているオブジェクトを使用できるようにする方法を示します。この例では、RandomRedDots2関数オブジェクト(例10-2RandomRedDots関数と似ているが、RandomRedDots2関数はデータストア名を指定する引数を取る)を作成します。この例では、myVar変数を作成して、datastore_1という名前のデータストアに保存します。次に、doEval関数をコールしてデータストア名を渡し、TRUEに設定したore.connect制御引数を渡します。

%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)

出力は、次のようなものです。


   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

この例のリスト

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.

例10-7 ora.type属性の使用方法

この例では、ora.type属性を使用して、FUN.VALUE引数で指定されたdata.frameオブジェクト内の列にデータベース・データ型のCLOBおよびBLOBを指定する方法を示します。

%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))

出力は、次のようなものです。

'function'

表10-5 data.frame: 1 x 1

x
<chr>
Hello, world
'data.frame'

表10-6 data.frame: 1 x 1

x
<chr>
Hello, world
'ore.frame'[1] "Hello, world"'Hello, world'

この例のリスト

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