More Related Content
More from Kazuya Wada (16)
DeployR使ってみた話
- 8. 2015/12/05
比較表
サイバー系
8
サイバー系
shiny rApache DeployR
想定ユーザー 人 (人と)プログラム プログラム
メリット 簡単にBIツールっ
ぽのが作成できる
戻り値を柔軟に設
定できる
開発&デバッグが
やりやすい
デメリット 同時アクセスにめっ
ぽう弱い
開発&デバッグが
やりづらい 戻り値が冗長
ライセンス
・AGPL v3
・RStudio License
Agreement
・Apache License
2.0
・AGPL v3
・Microsoft License
Agreement
開発者 R Studio社 Jeffrey Horner Microsoft(Revolutio
nAnalytics社)
- 11. 2015/12/05
word2vecやりましょう
サイバー系
11
サイバー系
python 2.7以上がインストールされているとしてshell上でgensimをinstall
>pip install gensim
build_mdl_w2v <- function(input_data = 'text8', #入力
size = 256,
windows = 5,
sg = 0,
min_count = 5,
model_out = 'vectors.txt' #出力
){
library(rPython)
python.assign("data", input_data)
python.assign("size", size)
python.assign("windows", windows)
python.assign("sg", sg)
python.assign("min_count", min_count)
python.assign("model_out", model_out)
python.exec("from gensim.models import word2vec")
python.exec("sentence = word2vec.Text8Corpus(data)")
python.exec("model = word2vec.Word2Vec(sentence, size=size, window=windows,
sg=sg, min_count=min_count)")
python.exec("model.save_word2vec_format(model_out, binary=False)")
}
build_mdl_w2v()
- 23. 2015/12/05
Shinyで
サイバー系
23
サイバー系
u server.R部分修正後
u こんな感じ
library(shiny); library(dplyr); library(inline)
library(data.table); library(rhandsontable)
cos.simfun = cxxfunction( … ) #略
calc_distance <- function(mdl, word, topn){…} #略
mdl <- fread(txt, sep = ‘ ’, skip = 1){} #略
shinyServer(function(input, output) {
select_data <- reactive({
calc_distance(mdl, word = input$word, topn = input$topn)
})
output$table1 <- renderRHandsontable({
if(is.null(input$terms)){
return(NULL)
}else{
select_data() %>% rhandsontable
}
})
})
- 25. 2015/12/05
Shinyで
サイバー系
25
サイバー系
u ui.R部分修正後
u こんな感じ
library(shiny); library(rhandsontable)
shinyUI(fluidPage(
titlePanel("word2vec"),
sidebarLayout(
sidebarPanel(
sliderInput("topn",
"TopN:",
min = 1,
max = 50,
value = 10),
textInput("words", "入力", "king")
),
mainPanel(
rHandsontableOutput("table1", height = 1250)
)
)
))
plotをtable
描写へ
変更
input部分を微調整
title表記微調整
- 28. 2015/12/05
rApacheで
サイバー系
28
サイバー系
u まずはRookライブラリでそれっぽいの構築します
library(Rook); library(dplyr); library(inline); library(data.table);
cos.simfun = cxxfunction( … ) #略
calc_distance <- function(mdl, term, topn){…} #略
run_w2v <- function(env){
mdl <- fread(txt, sep = ‘ ’, skip = 1){} #略
req <- Rook::Request$new(env); res <- Rook::Response$new()
words <- NULL
if(!is.null(req$GET()[['word']])){
words <- req$GET()[['word']]
mdl %>% calc_distance(term=words) -> res_tbl
}
res$write('input term.n')
res$write('<form method="GET">n')
res$write('<input type="text" name="term">n')
res$write('<input type="submit" name="Submit"><br>n</form>n<br>')
if(!is.null(words)) res$write(paste(‘<h1>’,terms,’</h1>n'))
if(exists(‘res_tbl’)) res$write(hwriter::hwrite(res_tbl))
res$finish()
}
ここら辺の
outputは
HTMLベタ書き
- 30. 2015/12/05
rApacheで
サイバー系
30
サイバー系
u rApacheにデプロイなんだけど、その前にrApacheの準備
u Ubuntuの場合
sudo add-apt-repository ppa:opencpu/rapache
sudo apt-get update
sudo apt-get install libapache2-mod-r-base
sudo vi /etc/apache2/mods-available/mod_R.load
LoadModule R_module /usr/lib/apache2/modules/mod_R.so 1行追記
sudo a2enmod mod_R
sudo service apache2 restart
sudo vi /etc/apache2/apache2.conf
<Location /word2vec>
SetHandler r-handler
RFileEval /home/parallels/sandbox/w2v_rook.R:R::Server$call(run_W2v)
</Location>
Rook使って書いた
Rのファイル名
Rookのbrowseで
呼んでた関数名
URLの一部になる
- 40. 2015/12/05
deployRで
サイバー系
40
サイバー系
u APIを叩いてみよう
u 何でRから叩いてるの?とか聞かない
library(RCurl)
tt <- postForm(“http://xx.xx.xx.xx:7400/deployr/r/repository/script/execute”,
format = "json",
filename = "w2v.R",
directory = "jr",
author = "admin",
csvinputs = "word,king",
robjects = "res"
)
library(jsonlite)
tl <- fromJSON(tt)[[1]]$response$workspace$objects$value[[1]]$value
names(tl) <- fromJSON(tt)[[1]]$response$workspace$objects$value[[1]]$name
as.data.frame(tl)
ここら辺
参考に
- 41. 2015/12/05
比較表
サイバー系
41
サイバー系
shiny rApache DeployR
想定ユーザー 人 (人と)プログラム プログラム
メリット 簡単にBIツールっ
ぽのが作成できる
戻り値を柔軟に設
定できる
開発&デバッグが
やりやすい
デメリット
同時アクセスにめっ
ぽう弱い
開発&デバッグが
大変面倒い
戻り値が冗長だし
柔軟性なし
ライセンス
・AGPL v3
・RStudio License
Agreement
・Apache License
2.0
・AGPL v3
・Microsoft License
Agreement
開発者 R Studio社 Jeffrey Horner
Microsoft(Revolutio
nAnalytics社)