2

我希望能够从列表中引用一个函数。使用此代码,我想自动导出具有不同布局的图形。我正在使用 igraph 库。

问题是我得到了函数的文本而不是对函数的引用。

您能否提供一些代码更改以使其正常工作?

library(igraph)
g2 <- barabasi.game(100)
layouts = list(layout.auto, 
        layout.random, 
        layout.circle, 
        layout.sphere, 
        layout.fruchterman.reingold, 
        layout.kamada.kawai, 
        layout.spring, 
        layout.reingold.tilford, 
        layout.fruchterman.reingold.grid, 
        layout.lgl, 
        layout.graphopt, 
        layout.svd, 
        layout.norm)
for(i in layouts) {
    plot.igraph(g, layout=i, vertex.label=NA, vertex.size=3, edge.color="darkblue", edge.lty=3)
    dev.copy(png,paste("figures/network_v0.1_",substitute(i),".png",sep=""))
    dev.off()
}
4

2 回答 2

8

如果将实际的函数对象放入列表中,则无法再找到它们的名称。函数对象不存储它们的名称,AFAIK。您需要使列表成为命名列表,或者简单地收集函数名称:

library(igraph)
g <- barabasi.game(100)
layouts = c("layout.random",
  "layout.circle",
  "layout.sphere",
  "layout.fruchterman.reingold",
  "layout.kamada.kawai",
  "layout.spring",
  "layout.reingold.tilford",
  "layout.fruchterman.reingold.grid",
  "layout.lgl",
  "layout.graphopt",
  "layout.svd")

for (alg in layouts) {
  print(alg)
  coords <- do.call(alg, list(g))
  png(paste("figures/network_v0.1_", alg, ".png", sep=""))
  plot.igraph(g, layout=coords, vertex.label=NA,
              vertex.size=3, edge.color="darkblue", edge.lty=3)
  dev.off()
}

一些评论。layout.norm不是布局功能。layout.auto只需根据图形的大小和其他属性调用其他布局函数之一。你错过了一些布局功能,例如layout.drl和其他。以下是如何列出所有这些,然后您可以过滤掉不需要的那些:

grep("^layout\\.", ls("package:igraph"), value=TRUE)
#  [1] "layout.auto"                      "layout.bipartite"                
#  [3] "layout.circle"                    "layout.drl"                      
#  [5] "layout.fruchterman.reingold"      "layout.fruchterman.reingold.grid"
#  [7] "layout.graphopt"                  "layout.grid"                     
#  [9] "layout.grid.3d"                   "layout.kamada.kawai"             
# [11] "layout.lgl"                       "layout.mds"                      
# [13] "layout.merge"                     "layout.norm"                     
# [15] "layout.random"                    "layout.reingold.tilford"         
# [17] "layout.sphere"                    "layout.spring"                   
# [19] "layout.star"                      "layout.sugiyama"                 
# [21] "layout.svd"                      
于 2013-03-12T15:32:20.793 回答
3

我会做do.call(layouts[i], list(g))wherelayouts是一个向量(如下所示)并且g是你的图表。

library(igraph)
g <- barabasi.game(100) #example graph
layouts = c("layout.auto", 
        "layout.random", 
        "layout.circle", 
        "layout.sphere", 
        "layout.fruchterman.reingold", 
        "layout.kamada.kawai", 
        "layout.spring", 
        "layout.reingold.tilford", 
        "layout.fruchterman.reingold.grid", 
        "layout.lgl", 
        "layout.graphopt", 
        "layout.svd", 
        "layout.norm")

for(i in layouts) {
    plot.igraph(g, layout=get(i), vertex.label=NA, vertex.size=3, 
              edge.color="darkblue", edge.lty=3)
    dev.copy(png,paste("figures/network_v0.1_",i,".png",sep=""))
    dev.off();
}

layout.spring, layout.graphopt, layout.svd layout.norm`and给出错误(可能需要额外的参数)。我会把它留给你(因为你的问题是关于从列表/向量中获取函数内的布局矩阵)。

于 2013-03-12T15:18:47.090 回答