如何使用 gml 格式文件将 igraph 边缘属性设置为字符串?
How to set igraph edge attributes to strings using a gml format file?
我使用 GML 图形文件格式将图形读入 igraph(R 版本)。有没有办法将边缘属性设置为字符串?似乎允许某些属性标签具有字符串值,而其他属性标签则不允许。示例输入文件:
graph [
node [
id 1
control 1
label "CiscoSW-1"
]
node [
id 2
control 1
label "CiscoSW-z"
]
edge [
source 1
target 2
difficulty 'A,B,C'
label "CiscoSW-1"
]
]
似乎 read_graph
不喜欢单引号 ' '
,因此您需要将它们换成双引号 " "
。
一种方法是读入文件,gsub
找出有问题的引用,然后用 read_graph
再次读入。所以如果你的图形文件保存为so.gml
,那么
# Read in file, `gsub` quotes and write to tempfile()
r <- gsub("[']", "\"", readLines("so.gml"))
cat(r, file=temp<-tempfile())
# Read amended gml file
g <- read_graph(temp, format="gml")
检查边缘属性是否符合预期
edge.attributes(g)
我使用 GML 图形文件格式将图形读入 igraph(R 版本)。有没有办法将边缘属性设置为字符串?似乎允许某些属性标签具有字符串值,而其他属性标签则不允许。示例输入文件:
graph [
node [
id 1
control 1
label "CiscoSW-1"
]
node [
id 2
control 1
label "CiscoSW-z"
]
edge [
source 1
target 2
difficulty 'A,B,C'
label "CiscoSW-1"
]
]
似乎 read_graph
不喜欢单引号 ' '
,因此您需要将它们换成双引号 " "
。
一种方法是读入文件,gsub
找出有问题的引用,然后用 read_graph
再次读入。所以如果你的图形文件保存为so.gml
,那么
# Read in file, `gsub` quotes and write to tempfile()
r <- gsub("[']", "\"", readLines("so.gml"))
cat(r, file=temp<-tempfile())
# Read amended gml file
g <- read_graph(temp, format="gml")
检查边缘属性是否符合预期
edge.attributes(g)