简体   繁体   中英

R-Script in Rapidminer

maybe someone can help me with my problem.

I'm working with RapidMiner and the R-Script Operator and process a 369 x 258 occurrence matix with the following R-Script:

# 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()
}

After creating the graph the process terminates with the error message "cannot copy from the null device".

So my question is, how can I print the graph in a file like postscript or png?

Kind regard

Tobias

I couldn't get the R code to work outside RapidMiner so I made a couple of changes to print rather than use tkplot (which is interactive so might struggle anyway).

I also added some simple data in the code to make the example reproducible.

Change the location of the png file that is created to somewhere where you want to store results (RapidMiner uses temporary local folders so you have to be explicit about the location).

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))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM