Jsp+R Server

rServer據開發者說是rJava的theard safe版本

我用jsp呼叫rJava的時候 只要重新整理或是怎麼的 Tomcat就會整個傻住然後shoutdown 必需要自己手動開啟

因為r一次只能有一個執行緒存在 而java是可以多個執行緒的 所以只要r的執行緒還在跑 就不能再新增一個 而rJava的執行緒不知道怎麼回事 就是關也關不掉

換用rServer之後 當然是Tomcat是沒有再死機了 但根據開發者說的(在這裡)
Although Rserve works on Windows it is not recommended to use it on that platform. Windows lacks important features that make the separation of namespaces possible, therefore Rserve for Windows works in cooperative mode only,that is only one connection

意思就是說 雖然你用了rServe 但是在windows上面還是一次只能有一個連線哦

靠北啊 那不就等於是單機版 那我寫jsp幹嘛!! 一次只有一個使用者能連進來不是超落漆嗎

唉 算了啦 頭都洗一半了 老師說就先繼續做 之後再看要不要弄一台Linux試試看

在這裡我就先分享如何使用rServe的心得 也算是公德一件

首先要在R裡安裝rServe

install.packages("Rserve")

安裝好之了後就在R裡面啟動它

library(Rserve)
Rserve()
看到Starting Rserve就表示Rserve已經在幕後啟動

然後打開Java的IDE 或筆記本 任何你習慣寫java code的介面

package rServe;

import org.rosuda.REngine.Rserve.*;

public class test {
    public static void main (String[] args)
    {
        // new R connection
        try{
        RConnection c = new RConnection(); 
        // write R command in .eval
        System.out.println (c.eval("3*5").asDouble());
        // close R connection
        c.close();
        }catch (Exception e){
         System.out.println(e.toString());
     }
     finally{
            System.out.println("程式結束");
     }
    
}
跑出結果就成功了

留言

  1. Dear Sir,
    it seems here is another way to have the same function.
    https://cran.r-project.org/web/packages/R.rsp/vignettes/Dynamic_document_creation_using_RSP.pdf

    However, I am not sure if it were able to have multi-session or not.
    just FYI.

    回覆刪除

張貼留言

這個網誌中的熱門文章

R中Try and Catch的寫法

如何將DSM(NAS)變成Mail Server

使用VBA記錄股市每分鐘的交易記錄