您可以使用rJava
. 它相对简单。我在下面展示了一个我会使用的场景。
首先,我编写了一个 R 代码来聚类数据并仅使用 R 函数绘制它们。例如,您可以这样做:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2)) ## you replace kmeans by your call to java function
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
kmeans
然后你通过调用你的java函数替换调用:
library(rJava)
.jinit(PATH-TO_YOUR_CLASS_BIN_OR_JAR) # this starts the JVM
## I call a the Cluster constructor giving it the imput data
## Obvsiouly you should create this constructor
javaCluster <- .jnew("Cluster",.jarray(x,dispatch=TRUE))
## call th clustering function which returns a vector of integers
cl <- .jcall(javaCluster ,"[I",method="doClustering")