Conda環境の使用
Condaは、サード・パーティRライブラリが含まれる環境の使用を可能にするオープンソースのパッケージおよび環境管理システムです。
前提条件
conda環境を使用してEmbedded R Execution REST APIを実行するには、OML4Rスクリプト・リポジトリで実行するRスクリプトを保存するか、他のユーザーからRスクリプトを付与する必要があります。また、ADMINユーザーがconda環境を作成してオブジェクト・ストレージにアップロードする必要もあります。
例で使用される環境
環境の作成および管理にはADMINユーザーが必要です。次では、Rバージョン4.0.5に基づいてmyrenv
環境を作成し、PDBに所有されているオブジェクト・ストレージにそれをアップロードします。データ・ビジュアライゼーション・パッケージggplot2
が環境にインストールされます。ggplot2
パッケージでは、チャネルconda-forgeが使用されます。
create -n myrenv -c conda-forge r-base=4.0.5 r-ggplot2
upload myrenv -t application OML4R
トークンの取得
Oracle Machine Learning User Management Cloud Service RESTエンドポイント/oauth2/v1/token
を使用して、アクセス・トークンのデータベース・ユーザー資格証明を交換します。詳細は、認証を参照してください。
$ curl -X POST --header 'Content-Type: application/json' --header 'Accept:
application/json'
-d '{"grant_type":"password", "username":"'${username}'", "password":"'${password}'"}'
"<oml-cloud-service-location-url>/omlusers/api/oauth2/v1/token"
一部のスクリプトで使用されるデータ
表作成
次のRコードでは、一部のスクリプトで使用されるデータを含む、IRIS表を作成します。
%r
ore.create(iris, table = 'IRIS')
ore.sync(table = "IRIS")
dim(IRIS)
スクリプト作成
test_ggplot2_noinp
このスクリプトでは、ggplot2
ライブラリがインポートされ、分布図が作成されます。次では、スクリプトを定義し、スクリプト・リポジトリに格納します。
begin
sys.rqScriptCreate('test_ggplot2_noinp',
'function() {
library(ggplot2);
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"));
gf <- qplot(mpg, data=mtcars, geom="density",
fill=gear, alpha=I(.5), main="Distribution of Gas Milage",
xlab="Miles Per Gallon", ylab="Density");
plot(gf);
res <- "hello world"
res
}',FALSE,TRUE);
end;
/
次のcURLコマンドは、Embedded R ExecutionのREST APIを使用して、ユーザーのOML4Rスクリプト・リポジトリに保存されたスクリプトtest_ggplot2_noinp
を実行します。conda環境名パラメータenvName
はmyrenv
に設定され、スクリプトによってイメージが返されるため、graphicsFlag
はtrue
に設定されます。
curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json'
-d '{"envName": "r-env", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/do-eval/test_ggplot2_noinp"
出力には、PNGイメージとスクリプトによって返されるデータの両方が含まれます。
{
"result":[
{
"IMAGE":"VJ3W6Y29saMw...................AAAABJRU5ErkJggg==",
"DATA":"\"hello world\"",
"ID":1
}
]
}
test_ggplot2_inp
このスクリプトでは、ggplot2
ライブラリがインポートされ、入力データで柱状グラフが作成されます。次では、スクリプトを定義し、スクリプト・リポジトリに格納します。
begin
sys.rqScriptCreate('test_ggplot2_inp',
'function(dat) {
library(ggplot2);
histogram <- ggplot(data=dat, aes(x=Sepal.Width)) +
geom_histogram(binwidth=0.2, color="black", aes(fill=Species)) +
xlab("Sepal Width") + ylab("Frequency") + ggtitle("Histogram of
Sepal Width")
plot(histogram);
res <- "hello world"
res
}',FALSE,TRUE);
end;
/
次のcURLコマンドは、Embedded R ExecutionのREST APIを使用して、ユーザーのOML4Rスクリプト・リポジトリに保存されたスクリプトtest_ggplot2_inp
を実行します。conda環境名パラメータenvName
はmyrenv
に設定され、スクリプトによってイメージが返されるため、graphicsFlag
はtrue
に設定されます。
curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"input": "IRIS", "envName": "myrenv", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/table-apply/test_ggplot2_inp"
出力には、PNGイメージとスクリプトによって返されるデータの両方が含まれます。
{
"result":[
[
{
"IMAGE":"To+O0X2umWN27mfWA......Q91XUfJggg==",
"DATA":"\"hello world\"",
"ID":1,
"CHUNK":"1"
}
],
]
}
test_ggplot2_idx
このスクリプトでは、ggplot2
ライブラリがインポートされ、タイトル付きの柱状グラフが生成されます。次では、スクリプトを定義し、スクリプト・リポジトリに格納します。
begin
sys.rqScriptCreate('test_ggplot2_idx',
'function(idx) {
library(ggplot2);
row <- iris[idx,]
histogram <- ggplot(data=row, aes(x=Sepal.Width)) +
geom_histogram(binwidth=0.2, color="black", aes(fill=Species)) +
xlab("Sepal Width") + ylab("Frequency") + ggtitle("Histogram of
Sepal Width")
plot(histogram);
res <- "hello world"
res
}',FALSE,TRUE);
end;
/
次のcURLコマンドは、Embedded R ExecutionのREST APIを使用して、ユーザーのOML4Rスクリプト・リポジトリに保存されたスクリプトtest_ggplot2_idx
を実行します。conda環境名パラメータenvName
はmyrenv
に設定され、スクリプトによってイメージが返されるため、graphicsFlag
はtrue
に設定されます。
curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json'
-d '{"times": 2, "envName": "myrenv", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/r-scripts/v1/index-apply/test_ggplot2_idx"
出力は、次のようなものです。
{
"result":
[
[
{
"IMAGE":"iVBAElFTkS..................uQmCC",
"DATA":"\"hello world\"",
"TIME":"1",
"ID":1
}
],
[
{
"IMAGE":"iVBORw................AElFTkSuQmCC",
"DATA":"\"hello world\"",
"TIME":"2",
"ID":1
}
]
]
}