簡體   English   中英

如何在Java中訪問R(柵格對象)

[英]How to access R (raster object ) in java

我已經看到Rserve用於在Java中連接到R。 從那我想在Java中訪問R的柵格對象。 任何人都可以提出示例,例如從Java訪問R對象

使用Rserve:

首先,您需要使用linux root權限tp啟動R並安裝Rserve軟件包。

~ sudo R
> install.packages("Rserve")
installing via 'install.libs.R' to /usr/local/lib/R/site-library/Rserve
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (Rserve)

其次,啟動Rserve。

~ R CMD Rserve

R version 3.0.1 (2013-05-16) -- "Good Sport"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Rserv started in daemon mode.

然后檢查Rserve進程並檢查網絡接口

~ ps -aux|grep Rserve
panda     7142  0.0  1.2 116296 25240 ?        Ss   09:13   0:00 /usr/lib/R/bin/Rserve

~ netstat -nltp|grep Rserve
tcp        0      0 127.0.0.1:6311          0.0.0.0:*               LISTEN      7142/Rserve

為了使Rserve在Windows上運行,您只需要在RGui中運行這些命令。

> install.packages('Rserve')
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.2/Rserve_1.7-3.zip'
Content type 'application/zip' length 711910 bytes (695 KB)
downloaded 695 KB

package ‘Rserve’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\panda\AppData\Local\Temp\2\Rtmpw7mlQY\downloaded_packages
> library(Rserve)
> Rserve()
Starting Rserve...
 "D:\Program Files\R\R-3.2.2\library\Rserve\libs\x64\Rserve.exe"  
> 

現在,Rserve已啟動,其端口為6311。您可以繼續使用Java連接它。 如果要將Rserve用作遠程服務器,則需要運行命令R CMD Rserve --RS-enable-remote 遠程服務器端口為7173,該端口通過命令netstat -nltp|grep Rserve再次檢查端口。

這是Rserve的簡單Java代碼。 您需要從http://www.rforge.net/Rserve/files/下載相關的jar庫REngine.jarRserveEngine.jar 您可以創建一個Java項目來開發Rserve的代碼。

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class Demo1 {

    public static void main(String[] args) throws RserveException, REXPMismatchException {
        Demo1 demo = new Demo1();
        demo.callRserve();
    }

    public void callRserve() throws RserveException, REXPMismatchException {
        RConnection c = new RConnection("192.168.1.201");
        REXP x = c.eval("R.version.string");
        System.out.println(x.asString());//打印變量x

        double[] arr = c.eval("rnorm(10)").asDoubles();
        for (double a : arr) {//循環打印變量arr
            System.out.print(a + ",");
        }
    }
}

上面的代碼結果是:

R version 3.0.1 (2013-05-16)
1.7695224124757984,-0.29753038160770323,0.26596993631142246,1.4027325257239547,-0.30663565983302676,-0.17594309812158912,0.10071253841443684,0.9365455161259986,0.11272119436439701,0.5766373030674361,

我認為您可以遵循Eclipse中Rserve的API來輕松猜測其用法。

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM