埋込みRの実行関数で、Oracle R EnterpriseのRスクリプト・リポジトリにスクリプトとして格納されているR関数を呼び出すことができます。このトピックで説明するR関数を使用すると、スクリプトを作成および管理できます。
「実行するための入力関数」に記載されているように、埋込みRの実行関数はFUN.NAME
引数を取ることができます。その引数には、Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前を指定します。Rスクリプト・リポジトリ内のスクリプトは、埋込みRの実行用のSQL APIインタフェースで使用することもできます。
スクリプトを管理するためのR関数は次のとおりです。
ore.grant
ore.revoke
ore.scriptCreate
ore.scriptList
ore.scriptLoad
ore.scriptDrop
これらの関数については、次の各項で説明します。
これらの関数の使用例は、例6-5を参照してください。
スクリプトの追加
Oracle R EnterpriseのRスクリプト・リポジトリにスクリプトとしてR関数を追加するには、ore.createScript
関数を使用します。この関数を呼び出すには、RQADMINロールを持っている必要があります。ore.createScript
関数の構文は次のとおりです。
ore.scriptCreate(name, FUN, global, overwrite)
引数について、次に説明します。
引数 | 説明 |
---|---|
name |
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前。 |
fun |
R関数。 |
global |
スクリプトがパブリック(グローバル)であるか、プライベートであるかを示す論理値。FALSE (デフォルト)は、スクリプトがパブリックではなく、所有者または所有者が読取りアクセス権を付与したユーザーにのみ表示されることを示します。TRUE は、スクリプトがパブリックであるため、すべてのユーザーに表示されることを示します。 |
overwrite |
スクリプトのR関数を、fun 引数で指定した関数に置き換えるかどうかを示す論理値。TRUE は、関数を置き換えることを示します(存在する場合)。FALSE (デフォルト)は、既存の内容を置き換えることができないことを示します。 |
overwrite = FALSE
であるときに、同じ名前のスクリプトがRスクリプト・リポジトリにすでに存在する場合、エラー条件が発生します。それ以外の場合、ore.scriptCreate
はNULL
を返します。
スクリプトへの読取りアクセス権の付与または取消し
スクリプトの作成者は、ore.grant
関数を使用してスクリプトへの読取りアクセス権を付与したり、ore.revoke
関数を使用してそのアクセス権を取り消すことができます。これらの関数の構文は次のとおりです。
ore.grant(name, type = "rqscript", user) ore.revoke(name, type = "rqscript", user)
引数について、次に説明します。
引数 | 説明 |
---|---|
name |
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前。 |
type |
スクリプトの場合、タイプはrqscriptです。 |
user |
読取りアクセス権を付与する、または取り消すユーザー。 |
name
およびtype
引数は必須です。引数user
を指定しなかった場合、読取りアクセス権がすべてのユーザーに付与されるか、すべてのユーザーから取り消されます。
次のいずれかに該当する場合、エラーが発生します。
指定したスクリプトがRスクリプト・リポジトリ内にありません。
type
引数が指定されていません。
ユーザーが見つかりません。
読取り権限がすでにユーザーに付与されているか、ユーザーから取り消されています。
指定したスクリプトがパブリックです。
使用可能なスクリプトのリスト
自分が使用できるスクリプトをリストするには、ore.scriptList
を使用します。名前、パターンまたはタイプに基づいてスクリプトをリストできます。RQADMINロールを持っている場合は、システム・スクリプトもリストできます。関数の構文は次のとおりです。
ore.sriptList(name, pattern, type)
引数について、次に説明します。
引数 | 説明 |
---|---|
name |
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前。引数pattern を指定する場合は使用できません。 |
pattern |
正規表現パターン。パターンと一致するスクリプトがリストされます。引数name を指定する場合は使用できません。 |
type |
スクリプトのタイプとして、次のいずれかを指定できます。
|
ore.scriptList
関数は、Rスクリプト・リポジトリ内のスクリプトおよびスクリプト内の関数の名前を含むdata.frame
を返します。
R環境へのスクリプトのロード
R環境にスクリプトのR関数をロードするには、ore.scriptLoad
を使用し、その構文は次のとおりです。
ore.sriptLoad(name, owner, newname, envir)
引数について、次に説明します。
引数 | 説明 |
---|---|
name |
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前。 |
owner |
スクリプトの所有者。 |
newname |
スクリプトをロードする新しい関数名。 |
envir |
スクリプトをロードするR環境。 |
ore.scriptLoad
を呼び出すユーザーにスクリプトへのアクセス権が付与されている場合、スクリプトの所有者を指定すると便利です。
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前が有効なR関数名でない場合、新しい関数名を指定すると便利です。
次のいずれかに該当する場合、エラーが発生します。
スクリプトがRスクリプト・リポジトリ内にありません。
現行のユーザーにスクリプトへの読取りアクセス権がありません。
name
引数で指定した関数が有効なR関数名ではありません。
newname
引数が有効なR関数名ではありません。
スクリプトの削除
Oracle R EnterpriseのRスクリプト・リポジトリからスクリプトを削除するには、ore.scriptDrop
関数を使用します。この関数を呼び出すには、RQADMINロールを持っている必要があります。ore.scriptDrop
関数の構文は次のとおりです。
ore.scriptDrop(name, global, silent)
引数について、次に説明します。
引数 | 説明 |
---|---|
name |
Oracle R EnterpriseのRスクリプト・リポジトリ内のスクリプトの名前。 |
global |
スクリプトがグローバル(パブリック)であるか、プライベートであるかを示す論理値。TRUE は、グローバル・スクリプトを削除することを示します。FALSE (デフォルト)は、現行のユーザーが所有しているスクリプトを削除することを示します。 |
silent |
ore.scriptDrop でエラー条件が検出された場合にエラー・メッセージを表示するかどうかを示す論理値。TRUE は、エラー・メッセージを表示することを示します。FALSE (デフォルト)は、表示しないことを示します。 |
次のいずれかに該当する場合、エラー条件が発生します。
スクリプトがRスクリプト・リポジトリ内にありません。
global = TRUE
の場合に、スクリプトがプライベート・スクリプトです。
global = FALSE
の場合に、スクリプトがパブリック・スクリプトです。
成功すると、ore.scriptDrop
はNULL
を返します。
例6-5 Rスクリプト管理関数の使用方法
# Create an ore.frame object from the data.frame for the iris data set. IRIS <- ore.push(iris) # Create a private R script for the current user. ore.scriptCreate("myRandomRedDots", function(divisor = 100){ id <- 1:10 plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2 ) data.frame(id = id, val = id / divisor) }) # Create another private R script. ore.scriptCreate("MYLM", function(data, formula, ...) lm(formula, data, ...)) # Create a public script, available to any user. ore.scriptCreate("GLBGLM", function(data, formula, ...) glm(formula = formula, data = data, ...), global = TRUE) # List only my private scripts. ore.scriptList() # List my private scripts and the public scripts. ore.scriptList(type = "all") # List my private scripts that have the specified pattern. ore.scriptList(pattern = "MY") # Grant read access to a private script to all users. ore.grant("MYLM", type = "rqscript") # Grant read access to a private script to a specific user. ore.grant("myRandomRedDots", user = "SCOTT", type = "rqscript") # List the granted scripts. ore.scriptList(type = "grant") # Use the MYLM script in an embedded R execution function. ore.tableApply(IRIS[1:4], FUN.NAME = "MYLM", formula = Sepal.Length ~ .) # Use the GLBGLM script in an embedded R execution function. ore.tableApply(IRIS[1:4], FUN.NAME = "GLBGLM", formula = Sepal.Length ~ .) # Load an R script to an R function object ore.scriptLoad(name = "MYLM") # Invoke the function. MYLM(iris, formula = Sepal.Length ~ .) # Load another R script to an R function object ore.scriptLoad(name = "GLBGLM", newname = "MYGLM") # Invoke the function. MYGLM(iris, formula = Sepal.Length ~ .) # Drop some scripts. ore.scriptDrop("MYLM") ore.scriptDrop("GLBGLM", global = TRUE) # List all scripts. ore.scriptList(type = "all")例6-5のリスト
R> # Create an ore.frame object from the data.frame for the iris data set. R> IRIS <- ore.push(iris) R> R> # Create a private R script for the current user. R> ore.scriptCreate("myRandomRedDots", 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> R> # Create another private R script. R> ore.scriptCreate("MYLM", + function(data, formula, ...) lm(formula, data, ...)) R> R> # Create a public script, available to any user. R> ore.scriptCreate("GLBGLM", + function(data, formula, ...) + glm(formula = formula, data = data, ...), + global = TRUE) R> R> # List only my private scripts. R> ore.scriptList() NAME SCRIPT 1 MYLM function (data, formula, ...) \nlm(formula, data, ...) 2 myRandomRedDots function (divisor = 100) \n{\n id & lt\n -1:10\n plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2)\n data.frame(id = id, val = id/divisor)\n} R> R> # List my private scripts and the public scripts. R> ore.scriptList(type = "all") OWNER NAME SCRIPT 1 RQSYS GLBGLM function (data, formula, ...) \nglm(formula = formula, data = data, ...) 2 RQUSER MYLM function (data, formula, ...) \nlm(formula, data, ...) 3 RQUSER myRandomRedDots function (divisor = 100) \n{\n id & lt\n -1:10\n plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2)\n data.frame(id = id, val = id/divisor)\n} R> R> # List my private scripts that have the specified pattern. R> ore.scriptList(pattern = "MY") NAME SCRIPT 1 MYLM function (data, formula, ...) \nlm(formula, data, ...) R> R> # Grant read access to a private script to all users. R> ore.grant("MYLM", type = "rqscript") R> R> # Grant read access to a private script to a specific user. R> ore.grant("myRandomRedDots", user = "SCOTT", type = "rqscript") R> R> # List the granted scripts. R> ore.scriptList(type = "grant") NAME GRANTEE 1 MYLM PUBLIC 2 myRandomRedDots SCOTT R> R> # Use the MYLM script in an embedded R execution function. R> ore.tableApply(IRIS[1:4], FUN.NAME = "MYLM", + formula = Sepal.Length ~ .) Call: lm(formula = formula, data = data) Coefficients: (Intercept) Sepal.Width Petal.Length Petal.Width 1.8560 0.6508 0.7091 -0.5565 R> R> # Use the GLBGLM script in an embedded R execution function. R> ore.tableApply(IRIS[1:4], FUN.NAME = "GLBGLM", + formula = Sepal.Length ~ .) Call: glm(formula = formula, data = data) Coefficients: (Intercept) Sepal.Width Petal.Length Petal.Width 1.8560 0.6508 0.7091 -0.5565 Degrees of Freedom: 149 Total (i.e. Null); 146 Residual Null Deviance: 102.2 Residual Deviance: 14.45 AIC: 84.64 R> R> # Load an R script to an R function object R> ore.scriptLoad(name="MYLM") R> R> # Invoke the function. R> MYLM(iris, formula = Sepal.Length ~ .) R> R> # Load another R script to an R function object R> ore.scriptLoad(name = "GLBGLM", newname = "MYGLM") R> R> # Invoke the function. R> MYGLM(iris, formula = Sepal.Length ~ .) R> R> # Drop some scripts. R> ore.scriptDrop("MYLM") R> ore.scriptDrop("GLBGLM", global = TRUE) R> R> # List all scripts. R> ore.scriptList(type = "all") OWNER NAME SCRIPT RQUSER myRandomRedDots function (divisor = 100) \n{\n id & lt\n -1:10\n plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2)\n data.frame(id = id, val = id/divisor)\n}
関連項目:
myRandomRedDots
スクリプトの使用例は、「ore.doEval関数の使用方法」を参照してください。
ore.scriptCreate
およびore.scriptDrop
の他の使用例は、例6-14を参照してください。