Lecture6: Shiny(その1)

前回の復習

getRelativeFreqMtxの関数を改造して、引数にRaw Freq./Relative Freq.のどちらかを選択するオプションを追加し、Raw/Relativeだけの結果を表示させる。

getFreqMtx2 <- function(filename, relative = FALSE){

 ....
 
 if (relative==TRUE){
    freqMtx<-freqMtx[,c(1,3)]
  }else{
    freqMtx<-freqMtx[,1:2]
  }
  
  return(freqMtx)
}

getFreqMtx2.Rを読み込む

source("getFreqMtx2.R")
freqMtx<-getFreqMtx2("osaka-u.txt")
head(freqMtx)
##           term raw
## 211        the  33
## 21         and  31
## 146         of  31
## 228 university  16
## 112         in  15
## 149      osaka  15
freqMtx<-getFreqMtx2("osaka-u.txt", relative=TRUE)
head(freqMtx)
##           term   relative
## 211        the 0.06470588
## 21         and 0.06078431
## 146         of 0.06078431
## 228 university 0.03137255
## 112         in 0.02941176
## 149      osaka 0.02941176

同一ディレクトリの複数ファイルから頻度表を作成

source("getFreqDir.R")
getFreqDir("testData")
##   test1 test2 test3
## a     3     2     2
## b     4     4     0
## c    13     2     3
## d     0     0     1
## e     7     1     1
## f     0    11     9
## g     0     7     7
## h     0     0     4
getFreqDir("testData",relative=TRUE)
##       test1      test2      test3
## a 0.1111111 0.07407407 0.07407407
## b 0.1481481 0.14814815 0.00000000
## c 0.4814815 0.07407407 0.11111111
## d 0.0000000 0.00000000 0.03703704
## e 0.2592593 0.03703704 0.03703704
## f 0.0000000 0.40740741 0.33333333
## g 0.0000000 0.25925926 0.25925926
## h 0.0000000 0.00000000 0.14814815

 練習:“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(univ[,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 type your message:")
    ),
  
  # Show a message
  mainPanel(
    textOutput("showMessage")
  )

))

server.R

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

global.R (なくてもOK)

アプリケーションの起動

runApp("shiny_apps/app_hoge")

★例1: “app_freqBar”

アプリケーションの起動

runApp("shiny_apps/app_freqBar")

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

alt text

alt text

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

“shiny_apps/app_freqBar_col”で作業

alt text

alt text