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

标签 java r object raster

我见过Rserve用于在java中连接到R。由此我想在java中访问R的光栅对象。 任何人都可以建议一些示例,例如从 java 访问 R 对象

最佳答案

对于使用 Rserve:

首先,您需要使用linux root权限启动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并检查网络接口(interface)

~ 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 代码。您需要在 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轻松猜测它们的用法。

最诚挚的问候。

关于java - 如何在java中访问R(光栅对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032444/

相关文章:

java - 如何使用java中的登录凭据从本地计算机更新Windows文件共享中的文件?

r - 绘制来自 tapply 输出的数据 - R 脚本

ruby - 如何获得 JavaScript 样式的哈希访问?

java - 在用Java为该应用程序编码应用程序时,如何存储主机地址,数据库的用户名和密码以及其他安全信息?

java - 为 API 在 CharSequence 和 String 之间进行选择

r - ggplot2:合并两个图例

r - 如何更改 slider 输入值的大小shinydashboard

html - Angular 6/Firestore 对象数据不会以 HTML 显示

javascript - d3 无法读取未定义的属性 'classed'

java - 我的一个类中的一个变量 "cannot be resolved or is not a field"