30日間の無料評価版をお試しいただけます。

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

高度な関数のカスタマイズの例として、レポートでRを使用する方法を示します。

Rserve Rのインストール

Expand
titleクリックして展開する

Yellowfinは、高度な関数を使用することで、Rスクリプトをレポートビルダーに統合することができます。

  1. Yellowfin-R jar」をダウンロードし、Yellowfin/appserver/webapps/ROOT/WEB-INF/libへコピーします。
  2. R」をダウンロードして、インストールします。
  3. Rserve」をインストールします。Rserveは、TCP/IPサーバーであり、Rを初期化したり、Rライブラリーへのリンクを必要とすることなく、他のプログラムが、様々な言語からRの機能を使用することを許可します。以下のコマンドを使用します。   install.packages("Rserve")
    install.packages("magrittr")
    install.packages("rattle") (linux requires special installation)
  4. Rserve」を起動します。YellowfinがRに接続するためには、Rserveが起動していなくてはいけません。Rコンソールで、以下のコマンドを入力します。
    library(Rserve)
    Rserve()
  5. Rスクリプトのために、「RSCRIPT_PATH」を定義します。すべてのRスクリプトは、ユーザーのために、閲覧、編集権限のあるディレクトリに配置しなくてはいけません。RSCRIPT_PATHディレクトリを指定する、環境変数を設定します。例:
    RSCRIPT_PATH=/home/foo/Rscripts

Rスクリプトを記述する

Expand
titleクリックして展開する

YellowfinにRスクリプトを理解させ、実行させるためには、以下に記載されているように、若干異なるスクリプト構造を使用します。

サンプルスクリプト、<R_file_name>.Rを見てみましょう。Yellowfinから渡された入力パラメーターは、<R_file_name>.R.input.csvで利用可能になります。処理が進むと、Rスクリプトは、結果(ひとつのカラム(列)のみ)を<R_file_name>.R.result.csv に書き込みます。

以下は、Neural NetworksのためのRスクリプトのサンプルです。こちらのスクリプトをコピーすれば、エラーを発生することなくRが動作します。

Code Block
titleSample R-Script : neural-net-script.R
setwd("C:/R/R-3.2.3/bin/x64")
library(rattle)   #
To access the weather dataset and utility commands.
library(magrittr) # For the
%>% and %<>% operators.
building <- TRUE
scoring  <- ! building
# A pre-defined value is used
to reset the random seed so that results are repeatable.
crv$seed <- 42 
# Load the data.
rPATH  <-
Sys.getenv("RSCRIPT_PATH")
rINPUT <- paste0(rPATH ,"/neural-net-script.r.input.csv")
rOUTPUT <- paste0(rPATH
,"/neural-net-script.r.result.csv")
dataset <-
read.csv(file=rINPUT, header=FALSE, sep=",")
# Note the user
selections. 
# Build the
training/validate/test datasets.
set.seed(crv$seed) 
crs$nobs <- nrow(dataset) #
366 observations 
crs$sample <- crs$train
<- sample(nrow(dataset), 0.7*crs$nobs) # 256 observations
crs$validate <-
sample(setdiff(seq_len(nrow(dataset)), crs$train), 0.15*crs$nobs) # 54
observations
crs$test <-
setdiff(setdiff(seq_len(nrow(dataset)), crs$train), crs$validate) # 56
observations
# The following variable
selections have been noted.
crs$input <-
c("V1", "V2", "V3", "V4","V5")
crs$target 
<- "V6"
#============================================================
# Neural Network 
#============================================================
# Build a neural network model
using the nnet package.
library(nnet, quietly=TRUE)
# Build the NNet model.

set.seed(199)
crs$nnet <-
nnet(as.factor(V6) ~ .,data=dataset[crs$sample,c(crs$input, crs$target)],size=10,
skip=TRUE, MaxNWts=10000, trace=FALSE, maxit=100)
#============================================================
# Score a dataset. 
#============================================================
# Obtain probability scores for
the Neural Net model on weather.csv [validate].
#crs$pr <- predict(crs$nnet,
newdata=dataset[crs$validate, c(crs$input)], type="class")
#crs$pr <- predict(crs$nnet,
newdata=dataset[crs$validate, c(crs$input)], type="class")
crs$pr <- predict(crs$nnet,
newdata=dataset, type="class")
write.table(crs$pr,
file=rOUTPUT, row.names=FALSE, col.names = FALSE)

...