Lecture4: Shiny(その1)

前回の課題

getFreqDir関数

tfidf=0: 何もしない
tfidf=1: 上記のTF-IDF 1の計算式を適用
tfidf=2: 上記のTF-IDF 2の計算式を適用。

getFreqDirを読み込む

source("getFreqDir.R")

実行結果

getFreqDir("univ",tfidf=1)

前回のやり残し

データ準備

dirname="univ"
filenames<-list.files(dirname)
filenames
## [1] "hiroshima.txt" "kufs.txt"      "kyoto.txt"     "osaka1.txt"   
## [5] "osaka2.txt"    "osaka3.txt"    "tokyo.txt"     "waseda.txt"
fname<-paste(dirname, filenames[1], sep = "/")
freqMtx<-getFreqMtx(fname)[,1:2]
freqOrder<-order(freqMtx[,2], decreasing=TRUE)
freqMtx <- freqMtx[freqOrder,]
head(freqMtx)
##           term hiroshima
## 106         to        11
## 7          and         7
## 103        the         7
## 107 university         6
## 52   hiroshima         5
## 76          of         5

Zipf’sの法則

\[Frequency=\frac{K}{Rank^A} \] K,A: 定数

K=freqMtx[1,2]
A=0.75

rank <- seq(1:dim(freqMtx)[1])
zipf <- K/rank^A

グラフ図:Zipf’sの理論式

title="Zipf's Law"
xlabel="Rank"
ylabel="Frequency"
plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqMtx)),ylim=c(1,20),main=title, xlab=xlabel, ylab=ylabel)

頻度散布図&Zipf’sの理論式の重ね書き

par(new=T)
plot(rank,freqMtx[,2], xlim=c(1,nrow(freqMtx)), ylim=c(1,20),log="xy",pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)

凡例をつける: legend

配置:“bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center” ラベル lty: 線の種類 pch: プロットの種類

legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c("darkgreen","red"))

グラフ図

Manipuate実習

library("manipulate")

練習

Kの値を変化させる

title="Zipf's Law"
xlabel="Rank"
ylabel="Frequency"

A=0.75
rank <- seq(1:dim(freqMtx)[1])

manipulate(
  {
    zipf <- constK/rank^A
    plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqMtx)),ylim=c(1,50),main=title, xlab=xlabel, ylab=ylabel)
    par(new=T)
    plot(rank,freqMtx[,2], xlim=c(1,nrow(freqMtx)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)
    legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c(col="darkgreen",col="red"))
    text(20, 50, "Frequency=K/Rank^A")
    text(20, 40, paste("K=", constK))
    text(20, 30, paste("A=", A))
  }
, constK=slider(10,50, initial=freqMtx[1,2],step=2)
)

実習:練習1に追加して、定数Aの値を変化させる

スライダーのステップは0.05に設定

 “univ”フォルダ内のテキストファイルを使用して素頻度表を作成

結果を変数tfに格納

結果表示

head(tf)
##     hiroshima kufs kyoto osaka1 osaka2 osaka3 tokyo waseda
## 000         0    0     0      0      0      0     0      2
## 1           0    0     0      1      0      0     0      1
## 10          0    0     1      0      0      0     0      1
## 100         0    0     0      0      1      0     0      0
## 11          2    0     0      0      0      0     0      0
## 12          1    0     0      0      0      0     0      0

★wordcloudパッケージ

library(wordcloud)
## Loading required package: RColorBrewer

 wordcloud()の実行例1

 wordcloud(words,freq, …)

wordcloud(rownames(tf),tf$hiroshima)

 練習:tfの“waseda”列の情報でwordcloudを作る

実行結果

 wordcloud()の実行例2

wordcloud(rownames(tf),tf$osaka1,min.freq=2,colors=rainbow(10))

 複数文書比較:comparison.cloud()の実行例

hiroshima, kufs

comparison.cloud(tf[,1:2])

osaka1-3

comparison.cloud(tf[,4:6],max.words=60)

 comparison.cloud()の実行例

colnames(tf)
comparison.cloud(tf[,c(F,F,T,F,F,T,T,F)],max.words=60)

shinyのロード

library(shiny)

★shinyアプリケーション用フォルダ

ui.R

shinyUI(bootstrapPage(
  
  # Application title
  headerPanel("Test Hoge"),
  
  # Sidebar
  sidebarPanel(     
      textInput("msg", "Please input your message:")
    ),
  
  # Show a message
  mainPanel(
    textOutput("showMessage")
  )

))

server.R

shinyServer(function(input, output) {
  
  output$showMessage <- renderText({
    input$msg
  })
})

global.R (なくてもOK)

アプリケーションの起動

runApp("app_hoge")

例1: “app_freqBar”

アプリケーションの起動

runApp("app_freqBar")
#runApp("shiny_apps/app_freqBar_col")

実習1: 棒グラフに色をつけてください。(例えば赤色)

alt text

alt text

実習2: UIに棒グラフの色を何種類か用意して、色が選択できるようにしてください。

新しいフォルダ(“app_freqBar_col”)で作業

alt text

alt text

時間が余ったら…WordCloudをShiny上で表示