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

前
次

2.2.5 データベースでのRオブジェクトの保存および管理

Oracle R Enterpriseには、Oracle R Enterpriseプロキシ・オブジェクト、および任意のRオブジェクトをOracle Databaseに保存するために使用できるデータストアが用意されています。1人以上のユーザーについてデータストアへの読取りアクセス権を付与したり、取り消すことができます。保存したオブジェクトを別のRセッションにリストアできます。データストアのオブジェクトは、RインタフェースおよびSQLインタフェースの両方を介して埋込みRの実行からもアクセスできます。

この項では、データストアの作成および管理に使用可能なOracle R Enterpriseの関数について説明します。この項の内容は次のとおりです。

2.2.5.1 Oracle R Enterpriseオブジェクトの永続について

Oracle R Enterpriseのデータストアを使用すると、Rオブジェクトをデータベースに保存できます。

Oracle R Enterpriseプロキシ・オブジェクトを含むRオブジェクトは、明示的に保存しないかぎり現行のRセッションの間存在します。Rオブジェクトの保存およびリストア用の標準のR関数(saveおよびload)は、ファイルに格納するためにRメモリー内のオブジェクトをシリアライズし、メモリーにリストアするためにデシリアライズします。ただし、Oracle R Enterpriseプロキシ・オブジェクトに対しては、それらの関数はプロキシ・オブジェクトに関連付けられているデータベース・オブジェクトをOracle Databaseに保存しないため、保存されたプロキシ・オブジェクトは別のRセッションでは正しく動作しません。

Oracle R Enterpriseプロキシ・オブジェクト、および任意のRオブジェクトは、ore.save関数を使用して保存できます。ore.save関数はOracle R Enterpriseデータストアを指定します。データストアは、Rセッションの終了時にデータベースに保持されます。データストアは、自身が含まれているオブジェクトの参照整合性を保持します。ore.load関数を使用して、データストア内のオブジェクトを別のRセッションにリストアできます。

データストアを使用して、次のことを実行できます。

  • Oracle R Enterpriseおよび作成したその他のRオブジェクトを1つのRセッションに保存し、別のRセッションにリストアします。

  • 埋込みRの実行で使用するためにR関数に引数を渡します。

  • 埋込みRの実行で使用するためにオブジェクトを渡します。たとえば、OREdmパッケージ内の関数を使用してOracle Data Miningモデルを構築し、それをデータストアに保存することもできます。その後、埋込みRの実行を介してそのモデルを使用してデータベースにデータをスコアリングできます。埋込みRの実行関数でのデータストアの使用例は、例6-10を参照してください。

次の表にデータストアを操作する関数をリストし、その簡単な説明を示します。

表2-1 データストアを操作する関数

関数 説明

ore.datastore

現行のOracle Databaseスキーマのデータストアに関する情報をリストします。

ore.datastoreSummary

現行のOracle Databaseスキーマの指定したデータストアに関する詳細な情報を提供します。

ore.delete

現行のOracle Databaseスキーマからデータストアを削除します。

ore.grant データストアへ読取りアクセス権を付与します。

ore.lazyLoad

データストアからオブジェクトをR環境に遅延リストアします。

ore.load

データストアからオブジェクトをR環境にリストアします。

ore.revoke データストアへの読取りアクセス権を取り消します。

ore.save

Rオブジェクトを新規または既存のデータストアに保存します。

関連項目:

埋込みRの実行に対するRインタフェースおよびSQLインタフェースの使用の詳細は、「Oracle R Enterpriseの埋込みRの実行の使用方法」を参照してください。

2.2.5.2 Oracle R Enterpriseのデータストアについて

各データベース・スキーマには、指定したOracle R Enterpriseデータストアを格納する表があります。データストアにはOracle R Enterpriseオブジェクトおよび標準のRオブジェクトを含めることができます。

ore.save関数を使用してデータストアを作成できます。データストアの作成時にそのデータストアの名前を指定します。オブジェクトを1つ以上のデータストアに保存できます。

データストアにデータベース・オブジェクトのOracle R Enterpriseプロキシ・オブジェクトが含まれているかぎり、データベース・オブジェクトはRセッション間で保持されます。たとえば、OREdmパッケージでore.odmNB関数を使用してOracle Data MiningのNaive Bayesモデルを構築することもできます。構築されたore.odmNBオブジェクトをデータストアに保存してRセッションを終了した場合、Oracle DatabaseはそのOracle Data Miningモデルを削除しません。ore.odmNBオブジェクトが含まれいるデータストアがない場合は、Rセッションを終了すると、データベースは自動的にそのモデルを削除します。

2.2.5.3 データストアへのオブジェクトの保存

ore.save関数は、1つ以上のRオブジェクトを指定されたデータストアに保存します。

デフォルトでは、Oracle R Enterpriseは、現行のユーザー・スキーマにデータストアを作成します。ore.saveに対して引数を指定する場合、特定のオブジェクトの名前、またはオブジェクトのリストを指定できます。データストアへの読取りアクセス権を他のユーザーに付与できるかどうかを指定できます。特定のR環境を指定して保存するオブジェクトを検索できます。overwriteおよびappendの引数は相互に排他的です。overwrite引数をTRUEに設定した場合、既存のデータストアを同じ名前の別のデータストアで置換できます。append引数をTRUEに設定した場合、既存のデータストアにオブジェクトを追加できます。description引数を使用すると、データストアの情報取得時に表示される説明テキストを指定できます。description引数は、append引数とともに使用すると機能しません。

例2-18 オブジェクトの保存およびデータストアの作成

この例では、引数の様々な組合せを使用してデータストアを作成する方法を示します。

# Create some R objects.
df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
iris_of <- ore.push(iris)

# Create a database table and an Oracle R Enterprise proxy object for the table.
ore.drop("AIRQUALITY")
ore.create(airquality, table = "AIRQUALITY")

# List the R objects.
ls()

# List the Oracle R Enterprise proxy objects.
ore.ls()

# Save the proxy object and all objects in the current workspace environment
# to the datastore named ds1 and supply a description.
ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore")

# Create some more objects.
x <- stats::runif(20)  # x is an object of type numeric.
y <- list(a = 1, b = TRUE, c = "hoopsa")
z <- ore.push(x)  # z is an object of type ore.numeric.

# Create another datastore.
ore.save(x, y, name = "ds2", description = "x and y")

# Overwrite the contents of datastore ds2.
ore.save(x, name = "ds2", overwrite = TRUE, description = "only x")

# Append object z to datastore ds2.
ore.save(z, name = "ds2", append = TRUE)

この例のリスト

R> # Create some R objects.
R> df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
R> df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
R> iris_of <- ore.push(iris)
R> 
R> # Create a database table and an Oracle R Enterprise proxy object for the table.
R> ore.drop("AIRQUALITY")
R> ore.create(airquality, table = "AIRQUALITY")
R> 
R> # List the R objects.
R> ls()
[1] "df1"     "df2"     "iris_of"
R> 
R> # List the Oracle R Enterprise proxy objects.
R> ore.ls()
[1] "AIRQUALITY"
R> 
R> # Save the proxy object and all objects in the current workspace environment
R> # to the datastore named ds1 and supply a description.
R> ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My datastore")
R> 
R> # Create some more objects.
R> x <- stats::runif(20)  # x is an object of type numeric.
R> y <- list(a = 1, b = TRUE, c = "hoopsa")
R> z <- ore.push(x)  # z is an object of type ore.numeric.
R> 
R> # Create another datastore.
R> ore.save(x, y, name = "ds2", description = "x and y")
R> 
R> # Overwrite the contents of datastore ds2.
R> ore.save(x, name = "ds2", overwrite = TRUE, description = "only x")
R> 
R> # Append object z to datastore ds2.
R> ore.save(z, name = "ds2", append = TRUE)

2.2.5.4 データストアへのアクセスの制御

ore.grantおよびore.revoke関数を使用すると、Oracle R Enterpriseのデータストアへのアクセス権を付与したり、取り消すことができます。

ore.grantおよびore.revoke関数では、データストアへのアクセスを制御できます。自分が所有するデータストアへの読取りアクセス権を、指定したユーザーに付与したり、アクセス権限を取り消すことができます。関数ore.saveore.loadore.datastoreおよびore.datastoreSummaryには、データストアのアクセス権に関連する引数があります。

注意:

ore.createを使用して永続データベース表およびそのプロキシore.frameオブジェクトを作成し、付与可能なデータストアにプロキシore.frameオブジェクトを保存した後、ore.grantを使用してデータストアへの読取りアクセス権を付与した場合、アクセス権はore.frameオブジェクトにのみ適用されます。読取りアクセス権は、永続データベース表には拡張されません。表自体への読取り権限を付与するには、適切なSQLコマンドを実行する必要があります。

例2-19 データストアへのアクセス権の付与および取消し

この例では、ローカルのRセッションからOracleデータベースにairqualityデータセットをプッシュしますが、これは、ore.frameオブジェクトAIRQUALITYとして、また、同じ名前の一時データベース表として存在します。この例では次に、AIRQUALITYオブジェクトをデータストアds3に保存し、そのデータストアへのアクセス権を他のユーザーに付与できるように指定します。type = grantableを指定して関数ore.datastoreを呼び出し、読取りアクセス権が付与されているすべてのデータストアを表示します。ds3データストアの読取り権限をSCOTTに付与します。その後、type = grantを指定してore.datastoreを呼び出し、読取りアクセス権が付与されているデータストアを表示します。SCOTTの読取り権限を取り消し、アクセス権が付与されているデータストアを再度表示します。

AIRQUALITY <- ore.push(airquality)
ore.save(AIRQUALITY, name = "ds3", 
         description = "My datastore 3", grantable  = TRUE)
ore.datastore(type = "grantable")
ore.datastore(type = "grant")
ore.grant("ds3", type = "datastore", user = "SCOTT")
ore.datastore(type = "grant")
ore.revoke("ds3", type = "datastore", user = "SCOTT")
ore.datastore(type = "grant")

この例のリスト

R> AIRQUALITY <- ore.push(airquality)
R> ore.save(AIRQUALITY, name = "ds3", 
+           description = "My datastore 3", grantable  = TRUE)
R> ore.datastore(type = "grantable")
  datastore.name object.count  size       creation.date    description
1            ds3            1  1451 2015-11-30 18:48:25 My datastore 3
R> ore.datastore(type = "grant")
[1] datastore.name grantee       
<0 rows> (or 0-length row.names)
R> ore.grant("ds3", type = "datastore", user = "SCOTT")
R> ore.datastore(type = "grant")
  datastore.name grantee
1            ds3   SCOTT
R> ore.revoke("ds3", type = "datastore", user = "SCOTT")
R> ore.datastore(type = "grant")
[1] datastore.name grantee       
<0 rows> (or 0-length row.names)

2.2.5.5 データストアのコンテンツに関する情報の取得

ore.datastore関数およびore.datastoreSummary関数を使用することで、現行のユーザー・スキーマのデータストアに関する情報を取得できます。

ore.datastore関数を使用して、データストアに関する基本的な情報をリストできます。特定のタイプのデータストアに関する情報を取得するには、オプションの文字列引数typeを使用できます。typeの有効な値は次のとおりです。

  • 現行のセッション・ユーザーが作成したデータストアをリストするuser。これはデフォルト値です。

  • 現行のセッション・ユーザーが他のユーザーに読取りアクセス権を付与できないデータストアをリストするprivate

  • 現行のセッション・ユーザーが読取りアクセス権を持っているすべてのデータストアをリストするall

  • 現行のセッション・ユーザーが他のユーザーに読取り権限を付与できるデータストアをリストするgrantable

  • 現行のセッション・ユーザーが他のユーザーに読取り権限を付与したデータストアをリストするgrant

  • 他のユーザーによって現行のセッション・ユーザーに読取り権限が付与されたデータストアをリストするgranted

タイプを指定しなかった場合、関数ore.datastoreは、データストア名、データストア内のオブジェクト数、データストアのサイズ、作成日および説明に対応する列を含むdata.frameオブジェクトを返します。行は、列datastore.nameによってアルファベット順にソートされます。タイプを指定した場合、この関数は、指定したタイプの列を含むdata.frameを返します。

名前または正規表現パターンを使用してデータストアを検索できます。

ore.datastoreSummary関数は、接続されているデータベースのユーザー・スキーマにあるデータストアに保存されたRオブジェクトに関する情報を返します。この関数は、data.frameを返します。オブジェクト名、オブジェクト・クラス、オブジェクトのサイズおよびオブジェクトの長さ(vectorの場合)または行および列の数(data.frameオブジェクトの場合)に対応する列を含みます。これは、1つの必須の引数としてデータストアの名前を取りますが、オプションの引数としてデータストアの所有者を取ることもできます。

例2-20 ore.datastore関数の使用方法

この例では、ore.datastore関数の使用方法を示します。

# Create some R objects.df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
iris_of <- ore.push(iris)

# Create a database table and an Oracle R Enterprise proxy object for the table.
ore.drop("AIRQUALITY")
ore.create(airquality, table = "AIRQUALITY")
                                                                            
# Save the objects to a datastore named ds1 and supply a description.
ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore")

# Create some more objects.
x <- stats::runif(20) # x is an object of type numeric.
y <- list(a = 1, b = TRUE, c = "hoopsa")
z <- ore.push(x) # z is an object of type ore.numeric.

# Create other datastores.
ore.save(x, y, name = "ds2", description = "x and y")
ore.save(df1, df2, name = "dfobj", description = "df objects")
ore.save(x, y, z, name = "another_ds", description = "For pattern matching")

# List all of the datastore objects.
ore.datastore()

# List the specified datastore.
ore.datastore("ds1")

# List the datastore objects with names that include "ds".
ore.datastore(pattern = "ds")

この例のリスト

R> # Create some R objects.
R> df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
R> df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
R> iris_of <- ore.push(iris)
R> 
R> # Create a database table and an Oracle R Enterprise proxy object for the table.
R> ore.drop("AIRQUALITY")
R> ore.create(airquality, table = "AIRQUALITY")
R>                                                                             
R> # Save the objects to a datastore named ds1 and supply a description.
R> ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore")
R>
R> # Create some more objects.
R> x <- stats::runif(20) # x is an object of type numeric.
R> y <- list(a = 1, b = TRUE, c = "hoopsa")
R> z <- ore.push(x) # z is an object of type ore.numeric.
R>
R> # Create other datastores.
R> ore.save(x, y, name = "ds2", description = "x and y")
R> ore.save(df1, df2, name = "dfobj", description = "df objects")
R> ore.save(x, y, z, name = "another_ds", description = "For pattern matching")
R> 
R> # List all of the datastore objects.
R> ore.datastore()
  datastore.name object.count size       creation.date          description
1     another_ds            3 1284 2017-04-21 16:08:57 For pattern matching
2          dfobj            2  656 2017-04-21 16:08:38           df objects
3            ds1            4 3439 2017-04-21 16:03:55 My private datastore
4            ds2            2  314 2017-04-21 16:04:32              x and y

R> # List the specified datastore.
R> ore.datastore("ds1")
  datastore.name object.count size       creation.date          description
1            ds1            4 3439 2017-04-21 16:03:55 My private datastore

R> # List the datastore objects with names that include "ds".
R> ore.datastore(pattern = "ds")
  datastore.name object.count size       creation.date          description
1     another_ds            3 1284 2017-04-21 16:08:57 For pattern matching
2            ds1            4 3439 2017-04-21 16:03:55 My private datastore
3            ds2            2  314 2017-04-21 16:04:32              x and y

例2-21 ore.datastoreSummary関数の使用方法

この例では、ore.datastoreSummary関数の使用方法を示します。この例では、前の例で作成したデータストアを使用します。

ore.datastoreSummary("ds1")
ore.datastoreSummary("ds2")

この例のリスト

R> ore.datastoreSummary("ds1")
  object.name      class size length row.count col.count
1  AIRQUALITY  ore.frame 1213      6        NA         6
2         df1 data.frame  328      2         5         2
3         df2 data.frame  328      2         5         2
4     iris_of  ore.frame 1570      5        NA         5
R> ore.datastoreSummary("ds2")
  object.name   class size length row.count col.count
1           x numeric  182     20        NA        NA
2           y    list  132      3        NA        NA

2.2.5.6 データストアからのオブジェクトのリストア

ore.load関数は、データストアに保存したRオブジェクトをRグローバル環境(.GlobalEnv)にリストアします。この関数は、リストアされるオブジェクトの名前を含む文字列ベクターを返します。

保存されたすべてのオブジェクトをロードするか、list引数を使用してロードするオブジェクトを指定できます。envir引数を使用すると、オブジェクトをロードする環境を指定できます。

例2-22 データストアからオブジェクトをリストアするためのore.load関数の使用方法

この例では、ore.load関数を使用して、例2-20で作成されたデータストアからオブジェクトをリストアする方法を示します。この例は、その例と同じRセッションで実行されます。

# List the R objects.
ls()

# List the datastores.
ore.datastore()

# Delete the x and z objects.
rm(x, z)
ls()

# Restore all of the objects in datastore ds2.
ore.load("ds2")

ls()

# After ending the R session and starting another session.
ls()
# The datastore objects persist between sessions.
ore.datastore()

# Restore some of the objects from datastore ds1.
ore.load("ds1", list = c("df1", "df2", "iris_of"))
ls()
例2-22のリスト
R> # List the R objects.
R> ls()
[1] "df1"     "df2"     "iris_of" "x"       "y"       "z"
R> 
R> # List the datastores.
R> ore.datastore()
  datastore.name object.count size       creation.date  description
1     another_ds            3 1243 2014-07-24 13:31:56 For pattern mattching
2          dfobj            2  656 2014-07-24 13:31:46            df objects
3            ds1            4 3162 2014-07-24 13:25:17          My datastore
4            ds2            2 1111 2014-07-24 13:27:26                only x
R> 
R> # Delete the x and z objects.
R> rm(x, z)
R> ls()
[1] "df1"     "df2"     "iris_of" "y"
R> 
R> # Restore all of the objects in datastore ds2.
R> ore.load("ds2")
[1] "x" "z"
R> 
R> ls()
[1] "df1"     "df2"     "iris_of" "x"       "y"       "z"
R> 
R> # After ending the R session and starting another session.
R> ls()
character(0)
R> # The datastore objects persist between sessions.
R> ore.datastore()
  datastore.name object.count size       creation.date           description
1     another_ds            3 1243 2014-07-24 13:31:56 For pattern mattching
2          dfobj            2  656 2014-07-24 13:31:46            df objects
3            ds1            4 3162 2014-07-24 13:25:17          My datastore
4            ds2            2 1111 2014-07-24 13:27:26                only x

R> # Restore some of the objects from datastore ds1.
R> ore.load("ds1", list = c("df1", "df2", "iris_of"))
[1] "df1"     "df2"     "iris_of"
R> ls()
[1] "df1"     "df2"     "iris_of"

2.2.5.7 データストアの削除

ore.delete関数を使用すると、Oracle R Enterpriseデータストアのオブジェクトを削除、またはデータストア自身を削除できます。データストアを削除するには、名前を指定します。データストアから1つ以上のオブジェクトを削除するには、list引数を指定します。ore.delete関数は、削除したオブジェクトまたはデータストアの名前を返します。

データストアを削除すると、Oracle R Enterpriseは、削除したデータストアでRオブジェクトによって参照されていたすべての一時データベース・オブジェクトを破棄します。Rオブジェクトを1つ以上のデータストアに保存していた場合、Oracle R Enterpriseは、一時データベース・オブジェクトを参照しているオブジェクトがないときにのみ、一時データベース・オブジェクトを破棄します。

例2-23 ore.delete関数の使用方法

この例では、ore.deleteを使用して、データストアからオブジェクトを削除した後、データストア全体を削除する方法を示します。この例では、例2-18で作成したオブジェクトを使用します。

# Delete the df2 object from the ds1 datastore.
ore.delete("ds1", list = "df2")
# Delete the datastore named ds1.
ore.delete("ds1")
例2-23のリスト
R> # Delete the df2 object from the ds1 datastore.
R> ore.delete("ds1", list = "df2")[1] "df2"
R> # Delete the datastore named ds1.
R> ore.delete("ds1")
[1] "ds1"

2.2.5.8 埋込みRの実行でのデータストアの使用について

データストアにオブジェクトを保存することによって、埋込みRの実行関数への引数の引渡し、およびRオブジェクトの参照が非常に容易になります。1つのRセッションで作成したオブジェクトをデータベース内の単一のデータストアに保存できます。このデータストアの名前を埋込みR関数内でロードする引数として渡すことができます。データストアを使用して、1つのオブジェクトまたは複数のオブジェクトを簡単に渡すことができます。