Shinyの紹介資料→ 「はじめてのShiny」(和田 計也さん作成)
Rで簡単にインタラクティブなWebページを作れてしまう。
Webページでパラメータを入力すると、
パラメータに応じたグラフがリアルタイムに表示される。
パラメータに応じたグラフがリアルタイムに表示される。
便利、これは便利。遊ばにゃ損損。
ただ、公式サイトのチュートリアルのソースではテンションが上がらなかったので、
twitteRを使って簡単なTwitterアプリを作っていた。
上がその画面。検索キーワードに関連するハッシュタグをつぶやきから抽出する。
Rをまともに触ったのは初だからアレなソースだろうけど、
Shinyの日本語文献は少ないので記載しておく。
ただ、公式サイトのチュートリアルのソースではテンションが上がらなかったので、
twitteRを使って簡単なTwitterアプリを作っていた。
上がその画面。検索キーワードに関連するハッシュタグをつぶやきから抽出する。
Rをまともに触ったのは初だからアレなソースだろうけど、
Shinyの日本語文献は少ないので記載しておく。
##### ui.R #####
if(grep("^shiny$",(installed.packages())[,"Package"]) < 1){
# shinyライブラリ未インストール時
install.packages("shiny")
}
library(shiny)
# Twitter関連タグ検索(画面)
shinyUI(pageWithSidebar(
# アプリ名
headerPanel("Twitter関連タグ検索"),
# 管理フォーム
sidebarPanel(
textInput("keyword",
"検索キーワード:",
value="#twitter"),
numericInput("nsearch",
"検索数:",
min = 1,
max = 1500,
value = 20),
numericInput("ntag",
"最大表示タグ数:",
min = 1,
max = 15,
value = 7)
),
# 表示フォーム
mainPanel(
plotOutput("barPlot")
)
))
##### server.R #####
if(grep("^shiny$",(installed.packages())[,"Package"]) < 1){
# shinyライブラリ未インストール時
install.packages("shiny")
}
library(shiny)
if(grep("^twitteR$",(installed.packages())[,"Package"]) < 1){
# twitteRライブラリ未インストール時
install.packages("twitteR")
}
library(twitteR)
# Twitter関連タグ検索(ロジック)
shinyServer(function(input, output) {
output$barPlot <- reactivePlot(function() {
keyword <- tolower(input$keyword)
tag.disp.nmaxrow <- input$nsearch
tag.disp.nmaxcol <- input$ntag
tag <- NULL
comment <- searchTwitter(keyword,n = tag.disp.nmaxrow)
for(i in 1:length(comment)){
idx <- gregexpr("(#|#){1}[[:alnum:]]+",comment[[i]]$text)
if(-1 < idx[[1]][[1]]){
# 取得タグがある時
tag <- append(list(tolower(substring(comment[[i]]$text,idx[[1]],idx[[1]] + attr(idx[[1]], 'match.length') - 1))),tag)
}
}
tag.df <- data.frame(key = unlist(tag),count = 1)
tag.sum.df <- rowsum(tag.df[,2],tag.df[,1])
barplot((tag.sum.df[order(tag.sum.df, decreasing = TRUE),1])[1:tag.disp.nmaxcol])
})
})
上の画面を作るためのソースはこれだけ。Shinyすげえ。
■追記(2013/02/04)
上記のソースを大幅にリフォームし、Gistに公開してみた。
ツールを試してみたい人はこちらから。
https://gist.github.com/4702281
■追記(2013/02/04)
上記のソースを大幅にリフォームし、Gistに公開してみた。
ツールを試してみたい人はこちらから。
https://gist.github.com/4702281