shinyでシャイニイイイイイイイイイイイイイイイイイイイイイイイイイ

webアプリを簡単に作れるライブラリshinyがみんな大好きRStudioから公表された。
下記にわかりやすいチュートリアルがあるので読めばわかる。
http://rstudio.github.com/shiny/tutorial
以下メモ。

shinyライブラリをインストールする

shinyライブラリをrstudioのリポジトリからインストールする。
RStudioは必要ない。
UIおよびサーバー側の処理を書いたコードを保存するためのフォルダも作っておく。

options(repos=c(RStudio="http://rstudio.org/_packages", getOption("repos")))
install.packages("shiny")
dir.create("shinyapp")

UIを書く

shinyUI関数を使って中に色々パーツを埋め込む感じ。
今回は下記アドレスのui.Rをコピペして、同じ名前で上記のshinyappフォルダに保存する。
http://rstudio.github.com/shiny/tutorial/#hello-shiny

library(shiny)
 
shinyUI(pageWithSidebar(
    
    # Application title
    headerPanel("Hello Shiny!"),
    
    # Sidebar with a slider input for number of observations
    sidebarPanel(
        sliderInput("obs", 
                    "Number of observations:", 
                    min = 0, 
                    max = 1000, 
                    value = 500)
    ),
    mainPanel(plotOutput("distPlot"))
    ))

サーバー側の処理を書く

shinyServer関数を使って処理をつらつらと書く。
こちらも下記のserver.Rをコピペして、同じ名前で(略)
http://rstudio.github.com/shiny/tutorial/#hello-shiny

library(shiny)
shinyServer(function(input, output) {
    
    # Function that generates a plot of the distribution. The function
    # is wrapped in a call to reactivePlot to indicate that:
    #
    #  1) It is "reactive" and therefore should be automatically 
    #     re-executed when inputs change
    #  2) Its output type is a plot 
    #
    output$distPlot <- reactivePlot(function() {
        
        # generate an rnorm distribution and plot it
        dist <- rnorm(input$obs)
        hist(dist)
    })
})

アプリを起動する

runApp関数で、上記ui.Rとserver.Rが保存されたフォルダを指定してやると、webブラウザが開いてヒストグラムが出現するはず

library(shiny)
runApp("./shinyapp")

enjoy!!!!