如何使用rJava访问枚举?

时间:2015-11-10 16:39:27

标签: java r rjava

我正在使用名为CDK的第三方库。我正在尝试使用Bond

创建类rJava的实例
o1 <- .jnew("org.openscience.cdk.Atom","O")
o2 <- .jnew("org.openscience.cdk.Atom","O")
J("org.openscience.cdk.Bond",o1,o2,
  "org.openscience.cdk.interfaces.IBond.Order.SINGLE")

问题是在这个接口IBond中定义了一个枚举,我需要将它作为参数传递给构造函数Bond(IAtom atom1, IAtom atom2, IBond.Order order),但我不知道如何使用{{1}执行此操作}}

我试过

rJava

希望J("org.openscience.cdk.Bond",o1,o2, J("org.openscience.cdk.interfaces.IBond.Order")$SINGLE) 可以像任何其他静态字段/方法一样访问,但它不起作用。

有没有办法将Enum传递给使用rJava的方法?

2 个答案:

答案 0 :(得分:2)

我找到了这样做的方法,我使用美元符号来引用Order界面中声明的公共枚举IBond,然后使用另一个美元符号来访问枚举值:

J("org.openscience.cdk.interfaces.IBond")$Order$SINGLE

答案 1 :(得分:1)

它是一个嵌套类,因此正确的名称是org.openscience.cdk.interfaces.IBond$Order

> .jfields("org.openscience.cdk.interfaces.IBond$Order")
[1] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.SINGLE"   
[2] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.DOUBLE"   
[3] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.TRIPLE"   
[4] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.QUADRUPLE"
[5] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.QUINTUPLE"
[6] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.SEXTUPLE" 
[7] "public static final org.openscience.cdk.interfaces.IBond$Order org.openscience.cdk.interfaces.IBond$Order.UNSET"    

所以“正常”方式可能是

> .jfield("org.openscience.cdk.interfaces.IBond$Order",,"SINGLE")
[1] "Java-Object{SINGLE}"
> J("org.openscience.cdk.interfaces.IBond$Order")$SINGLE
[1] "Java-Object{SINGLE}"