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にアップしたいと思う。

0 件のコメント:

コメントを投稿