如何在java中访问R(光栅对象)

如何在java中访问R(光栅对象),java,r,object,raster,Java,R,Object,Raster,我见过在java中,Rserve用于连接R。因此,我想访问java中R的光栅对象。 有谁能推荐一些例子,比如从java访问R对象以使用Rserve: 首先,您需要使用linux root权限tp startup R并安装Rserve包 ~ sudo R > install.packages("Rserve") installing via 'install.libs.R' to /usr/local/lib/R/site-library/Rserve ** R ** inst ** pre

我见过在java中,Rserve用于连接R。因此,我想访问java中R的光栅对象。 有谁能推荐一些例子,比如从java访问R对象以使用Rserve:

首先,您需要使用linux root权限tp startup 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)
第二,启动资源服务

~ 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
对于在Windows上工作的Rserve,您只需要在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代码。您需要下载相关的jar libs
REngine.jar
rservengine.jar
at。您可以创建一个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来轻松猜测它们的用法


致以最诚挚的问候。

您能告诉我您的感受吗windows@user28536我在Linux上工作。但我认为它可以在Windows上运行。您可以尝试在Windows CMD中执行这些步骤。您能帮我告诉我如何使用R命令从java中读取Landsat数据的.tif文件吗从命令promptI在Windows中安装Rserve的命令是什么我恐怕无法通过Windows中的命令