2013年10月18日金曜日

Rによる統計解析Webサービスの作成(3) 回帰分析プログラムの作成

csvを読み込んで回帰分析を行ってみた。
ソースは以下の通り。

index.html
<html>

<head>
  <script src="shared/jquery.js" type="text/javascript"></script>
  <script src="shared/shiny.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
</head>

<body>
  <div id="wrapper">
  <div id="header">
     <h2>回帰分析</h2>
    <a href="#">ホームに戻る</a>
  </div>
  <div id="container">
      <p>CSVファイルは1行目を列名とし、説明変数の列名はyとしてください。</p>
      <h4>CSV取り込み</h4>
      <p><input type="file" name="datafile"></p>  
      <p>結果</p>
      <div style="margin:-20 0 0 0 ;float:left;width:300px">
          <pre id="summary" class="shiny-text-output"></pre>
      </div>
      <div style="float:right">
          <div id="plot" class="shiny-plot-output"
           style="width: 100%; height: 350px; width:350px"></div>
      </div>
  </div>
  </div>
</body>

</html>

server.R
library(shiny)
shinyServer(function(input, output) {
    output$summary <- renderPrint({
        file <- input$datafile
        if(is.null(file)) {
            "CSVデータを入力してください"
        } else {
            filepath <- file$datapath
            data <- read.csv(filepath, header=T)
            data.lm <- lm(y~.,data=data)
            summary(data.lm)
        }
    })
    output$plot <- renderPlot(function() {
        file <- input$datafile
        if(is.null(file)) {
        } else {
            filepath <- file$datapath
            data <- read.csv(filepath, header=T)
            plot(data)
        }
  })
})

実行した画面が以下の通り


















csvを入力する前にもplot部分が表示されてしまうが、そのあたりはどうすればよいかわからなかった。


















次回はこれをwebにアップしたいと思う。

2013年10月16日水曜日

Rによる統計解析Webサービスの作成(2) サンプルの作成

まずはRのコンソールからShinyをインストール

options(repos=c(RStudio='http://rstudio.org/_packages', getOption('repos')))
install.packages('shiny') 
 









ui.Rで表示部分を実装
今回はhtmlで記述

index.html
<html>

<head>
  <script src="shared/jquery.js" type="text/javascript"></script>
  <script src="shared/shiny.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="shared/shiny.css"/> 
</head>
 
<body>
  <h1>TEST</h1>
 
  <p>
    <label>Distribution type:</label><br />
    <select name="dist">
      <option value="norm">Normal</option>
      <option value="unif">Uniform</option>
      <option value="lnorm">Log-normal</option>
      <option value="exp">Exponential</option>
    </select> 
  </p>
 
  <p>
    <label>Number of observations:</label><br /> 
    <input type="number" name="n" value="500" min="1" max="1000" />
  </p>
 
  <pre id="summary" class="shiny-text-output"></pre> 
  
  <div id="plot" class="shiny-plot-output" 
       style="width: 100%; height: 400px"></div> 
  
  <div id="table" class="shiny-html-output"></div>
</body>

</html> 
 
続いて、server.Rを実装
library(shiny)

# Define server logic for random distribution application
shinyServer(function(input, output) {

  # Reactive expression to generate the requested distribution. This is 
  # called whenever the inputs change. The output renderers defined 
  # below then all used the value computed from this expression
  data <- reactive({  
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)

    dist(input$n)
  })

  # Generate a plot of the data. Also uses the inputs to build the 
  # plot label. Note that the dependencies on both the inputs and
  # the data reactive expression are both tracked, and all expressions 
  # are called in the sequence implied by the dependency graph
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n

    hist(data(), 
         main=paste('r', dist, '(', n, ')', sep=''))
  })

  # Generate a summary of the data
  output$summary <- renderPrint({
    summary(data())
  })

  # Generate an HTML table view of the data
  output$table <- renderTable({
    data.frame(x=data())
  })
}) 
 
Rのコンソールから実行 
runApp("dir") 


 
 
 
 
 
 
 
 
 
 
 
 
 
ブラウザ上で表示することができた。
次回は試しに回帰分析が出来るものを作ってみる。 

Rによる統計解析Webサービスの作成(1) 概要

学生時代Rを少し使ったことがあるので、これで簡単な解析サービスを作れないか考えてみた。
いろいろ調べた結果、Shinyを使うのが簡単そう。

とりあえずローカルで試すには
1.Rをインストール
2.RのコンソールからShinyをインストール
3.ui.R(ブラウザ側の処理)、server.R(サーバー側の処理)の実装
4.Rのコンソールで実行し、ブラウザが立ち上がる
5.さらにShinyホスティングサービスを利用することでWeb上にアップロードできる

アプリの最小構成は以下のようになる
<application-dir>
|-- ui.R (ブラウザ側の処理)
|-- server.R (サーバ側の処理)

ちなみにブラウザ側の処理はHTMLでも記述できる

<application-dir>
|-- www
    |-- index.html
|-- server.R


[参考]
・はじめてのShiny
http://www.slideshare.net/wdkz/shiny-16178934

・github
https://github.com/rstudio/shiny

・Shinyを使ってRだけでWebアプリケーション
http://ixixi.hateblo.jp/entry/2012/11/10/150759