プライマリ・コンテンツに移動
Oracle® R Enterpriseユーザーズ・ガイド
リリース1.5.1
E88296-01
目次へ移動
目次
索引へ移動
索引

前
次

6.2.3 ore.doEval関数の使用方法

ore.doEval関数は、入力関数によって生成されたデータを使用して指定された入力関数を実行します。ore.frameオブジェクトまたはシリアライズRオブジェクトをore.objectオブジェクトとして返します。

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

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

関連項目:

関数ore.doEvalの引数の詳細は、「スクリプトを実行する関数の引数」を参照してください。

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

この例では、RandomRedDotsによって、1つの引数を取り、2つの列を持つdata.frameオブジェクトを返し、100個のランダム標準値を表示する関数を取得します。次に、ore.doEval関数を呼び出して、RandomRedDots functionオブジェクトを渡します。イメージがクライアントに表示されますが、これはRandomRedDots関数を実行したデータベース・サーバーの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)
}
ore.doEval(RandomRedDots)
例6-6のリスト
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

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

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

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

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

ore.doEval(RandomRedDots, divisor = 50)
例6-7のリスト
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.

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

入力関数がOracle R EnterpriseのRスクリプト・リポジトリに格納されている場合は、FUN.NAME引数を指定してore.doEval関数を呼び出すことができます。この例ではまず、myRandomRedDotsという名前のスクリプトがRスクリプト・リポジトリに含まれないようにするために、ore.scriptDropを呼び出します。この例では、例6-6RandomRedDots関数を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)
例6-8のリスト
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"

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

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

res.of <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
                     FUN.VALUE= data.frame(id = 1, val = 1))
class(res.of)
例6-9のリスト
R> res.of <- ore.doEval(FUN.NAME="myRandomRedDots", divisor = 50,
+                        FUN.VALUE= data.frame(id = 1, val = 1))
R> class(res.of)
[1] "ore.frame"
attr(,"package")
[1] "OREbase"

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

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

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)
}
myVar <- 5
ore.save(myVar, name = "datastore_1")
ore.doEval(RandomRedDots2, datastore.name = "datastore_1", ore.connect = TRUE)
例6-10のリスト
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> 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.

例6-11 ora.type属性の使用方法

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

eval1 <- ore.doEval(function() "Hello, world")
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))

例6-11のリスト

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