我从哪里开始使用 rJava?
我不是 Java 程序员。我编程 R 和 C++。我有一些 java 代码想要包含在 R 包中。该程序的基础知识是:
- 从标准输入读取数据。
- 运行 MCMC 链。
- 输出到文件。
我想将其转换为 R,这样我就可以从 R 运行程序。我熟悉 Rcpp 包并习惯了它的一些便利。我从哪里开始使用 rJava 包来学习使用此代码。
具体来说我有以下问题。
- 如何将数据从 R 传输到 java,例如数值向量、因子等。
- 如何运行类的方法?
- 如何在包中包含 java 代码?
rJava 文档不是很有帮助。有人有这方面的教程吗?
I am not a Java programmer. I program R and C++. I have some java code that I want to include in an R package. The basics of the program are this:
- Read data from standard input.
- Run a MCMC chain.
- output to a file.
I want to convert this to R where I can run the program from R. I am familiar with Rcpp package and am used to some of the conveniences of it. Where do I start with the rJava package to learn to use this code.
Specifically i have the following questions.
- How to I transfer data to java from R, e.g. numeric vector, factor, etc.
- How do I run methods of a class?
- How do I include java code in a package?
The rJava documentation is not very helpful. Does anyone have a tutorial on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种“简单”的方法可以做到这一点,也有一种稍微困难的方法可以做到这一点。我是一个简单的人,所以我倾向于简单的解决方案:
然后使用任何有意义的 R 函数读取输出文件。
稍微困难的解决方案是编辑 Java 代码,使其不从 stdin 读取,而是传递向量或其他 Java 对象。我无法真正概括如何更改 Java 代码,但如果 Java 函数最终需要输入向量,您会这样做:
我总是发现处理 rJava 中的类型是相当痛苦的,正如你在上面看到的。
一个可以为您节省大量时间的技巧是:当您在 rJava 中创建了一个 Java 对象时,您可以通过键入名称、美元符号然后按 Tab 键来找出它的方法。因此,使用上面创建的 v 对象,输入“v$”,您应该得到以下结果:
祝您好运,如果您遇到特定问题,请务必大喊大叫。
There's a "simple" way to do this and a somewhat harder way to do this. I'm a simple man so I lean toward the simple solution:
Then read in the output file using whatever R function makes sense.
The somewhat harder solution is to edit your Java code so it doesn't read from stdin, but gets passed a vector, or other Java object. I can't really generalize about how to alter your Java code, but if the Java function ultimately needs to be fed a vector, you'd do it something like this:
I always find dealing with types in rJava to be a considerable pain, as you can see above.
One tip that will save you considerable time is this: When you have a Java object created in rJava you can figure out its methods by typing the name, a dollar sign, and then hit tab. So using the v object created above type "v$" and you should get this:
Good luck, and be sure and yell if you have specific snags.
Deducer 插件开发文档中对此进行了介绍。虽然它面向扩展 Deducer 的包,但某些部分是通用的。
在 R 中运行 java 方法:
http://www.deducer.org/pmwiki/pmwiki.php ?n=Main.Development#wwjoir
将 R 对象引入 java 并使用 java 代码创建包:
http://www.deducer.org/pmwiki/pmwiki.php ?n=Main.Development#suaptijc
全面披露:Deducer 是我的项目。
This is covered in the Deducer plug-in development document. While it is geared towards packages extending Deducer, some sections are general.
Running java methods in R:
http://www.deducer.org/pmwiki/pmwiki.php?n=Main.Development#wwjoir
Bringing R objects into java and creating a package with java code:
http://www.deducer.org/pmwiki/pmwiki.php?n=Main.Development#suaptijc
Full disclosure: Deducer is my project.