Rapidminer 中的 R 脚本

R-Script in Rapidminer

也许有人可以帮我解决我的问题。

我正在使用 RapidMiner 和 R-Script Operator 并使用以下 R-Script 处理 369 x 258 出现矩阵:

# rm_main is a mandatory function, 
# the number of arguments has to be the number of input ports (can be none)
rm_main = function(data)
{
total_occurrence <- colSums(data)
data_matrix <- as.matrix(data)
co_occurrence <- t(data_matrix) %*% data_matrix
library(igraph)
graph <- graph.adjacency(co_occurrence,
                          weighted = TRUE,
                          mode="undirected",
                          diag = FALSE)
tkplot(graph,
        vertex.label=names(data),
        vertex.size=total_occurrence*1,
        edge.width=E(graph)$weight*1,)

dev.copy (tk_postscript, file= '/home/knecht/r-graph.pdf')
dev.off()
}

创建图形后,进程终止并显示错误消息 "cannot copy from the null device"。

所以我的问题是,如何在 postscript 或 png 等文件中打印图表?

此致

托比亚斯

我无法让 R 代码在 RapidMiner 之外工作,所以我做了一些更改来打印而不是使用 tkplot(它是交互式的,所以无论如何都可能会遇到困难)。

我还在代码中添加了一些简单的数据以使示例可重现。

将创建的 png 文件的位置更改为要存储结果的位置(RapidMiner 使用临时本地文件夹,因此您必须明确说明位置)。

rm_main = function(data)
{
    data2 = matrix(c(1,2,3,1,2,1), nrow = 2, ncol = 3)
    total_occurrence <- colSums(data2)
    data_matrix <- as.matrix(data2)
    co_occurrence <- t(data_matrix) %*% data_matrix
    library(igraph)
    graph <- graph.adjacency(co_occurrence,
                      weighted = TRUE,
                      mode="undirected",
                      diag = FALSE)
    png('c:/temp/r-graph.png')                     
    plot(graph,
       vertex.label=names(data2),
        vertex.size=total_occurrence*1,
        edge.width=E(graph)$weight*1,)
    dev.off()
return(list(data))
}