How to rename igraph node id when writing to file - r

I'm trying to write a graph to a file but I'm getting a different node labels than I would like.
Example Code:
g <- graph.star(2)
V(g)$name <- c('homer','marge')
write.graph(g,file = 'g.graphml',format = 'graphml')
Output:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<!-- Created by igraph -->
<key id="g_name" for="graph" attr.name="name" attr.type="string"/>
<key id="g_mode" for="graph" attr.name="mode" attr.type="string"/>
<key id="g_center" for="graph" attr.name="center" attr.type="double"/>
<key id="v_name" for="node" attr.name="name" attr.type="string"/>
<graph id="G" edgedefault="directed">
<data key="g_name">In-star</data>
<data key="g_mode">in</data>
<data key="g_center">1</data>
<node id="n0"> ###### This should <node id = "homer">
<data key="v_name">homer</data>
</node>
<node id="n1"> ###### This should <node id = "marge">
<data key="v_name">marge</data>
</node>
<edge source="n1" target="n0">
</edge>
</graph>
</graphml>
I would like the "node id" attribute to be the names of the nodes (as shown in comments). Anybody have any ideas? Thanks!

You can't do this currently AFAIK. I am not sure why it is not implemented that way. Can you please open an issue about it at https://github.com/igraph/igraph/issues?state=open ? Thanks.

Related

Count number of nodes R, xml2

I have an xml file and want to know the count of a specific node using R. My xml looks something like below. The node count should be 4. I'm using the xml2 package. Notice there's another element, <tag>, at the same level that I don't want to count.
I appreciate the help. Thanks!
<root>
<node>
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
</node>
<node>
<string>5</string>
<string>6</string>
<string>7</string>
<string>8</string>
</node>
<node>
<string>9</string>
<string>10</string>
<string>11</string>
<string>12</string>
</node>
<node>
<string>13</string>
<string>14</string>
<string>15</string>
<string>16</string>
</node>
<tag>
<string>17</string>
</tag>
</root>
library(xml2)
length(xml2::xml_find_all(doc, ".//node"))
# [1] 4
doc <- read_xml("<root>
<node>
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
</node>
<node>
<string>5</string>
<string>6</string>
<string>7</string>
<string>8</string>
</node>
<node>
<string>9</string>
<string>10</string>
<string>11</string>
<string>12</string>
</node>
<node>
<string>13</string>
<string>14</string>
<string>15</string>
<string>16</string>
</node>
<tag>
<string>17</string>
</tag>
</root>
")

Export Tinkerpop graph using writeGraph() to different OutputStream

The following statement will create a new file on the graph server:
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(new FileOutputStream("export.graphml"), graph)
I want to use a different OutputStream to see the output directly in my gremlin client. I have tried DataOutputStream() but got a NullPointerException. How would I get the response from writeGraph()?
I originally read this question as wanting to know how to create a remote OutputStream to write from the server back to some local file on the client, which likely has some solution but I'm not sure what the answer is to that. As I happen to look at this question again though the bounty notes that you are interested in "Export of full tinkerpop graph to console" in which case perhaps a different approach will satisfy.
I would simply construct a GraphMLWriter directly using its Builder and then write to a byte[] and return that as a String:
baos = new ByteArrayOutputStream()
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph)
baos.toByteArray()
Here is the full console session though I've elided some of the graphml to help with readability:
gremlin> :remote connect tinkerpop.server conf/remote-objects.yaml session
==>Configured localhost/127.0.0.1:8182-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d] - type ':remote console' to return to local mode
gremlin> baos = new ByteArrayOutputStream();[]
gremlin> graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph);[]
gremlin> new String(baos.toByteArray(), java.nio.charset.StandardCharsets.UTF_8)
==><?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml>
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> result
==>result{object=<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml> class=java.lang.String}
gremlin> result.get(0).getString()
==><?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graph>
</graphml>
gremlin>
My Gremlin Console example sends scripts to the server for execution in a session (thus this approach will also work as a script sent from drivers assuming it is sent as one script or if using session each line can be sent individually). Note that the result of each line returned from the server is stored in a result variable in the console. For this to work properly it is important that you connect with a configuration like the one presented in the default remote-objects.yaml where objects are returned rather than just string representations, though in this case since the ultimate object is a String it might not matter so much. I suppose whether or not it depends on what you intend to do with the result itself once you have it.
Some caveats to consider:
For a large graph this operation will be fairly expensive because not only is it a full graph scan but you're also writing the entire graph to memory before streaming it back.
The use of the Graph API (i.e. graph.io()) and/or direct use of GraphWriter implementations isn't recommended, so I suppose these APIs could disappear in future versions but give that there aren't better solutions at this time there isn't much choice I'm afraid.

No value returned when using identity() in the within step, but values are returned when hardcoded

I'm new to gremlin.
Here's a sample graph
<?xml version='1.0' ?>
<graphml xmlns='http://graphml.graphdrawing.org/xmlns'>
<key id='labelV' for='node' attr.name='labelV' attr.type='string'></key>
<key id='type' for='node' attr.name='type' attr.type='string'></key>
<key id='pick' for='node' attr.name='pick' attr.type='string'></key>
<key id='match' for='node' attr.name='match' attr.type='string'></key>
<key id='pot' for='node' attr.name='pot' attr.type='double'></key>
<key id='stake' for='node' attr.name='stake' attr.type='double'></key>
<key id='labelE' for='edge' attr.name='labelE' attr.type='string'></key>
<graph id='routes' edgedefault='directed'>
<!-- vertices -->
<node id='1'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M1</data>
<data key='pos'>H1</data>
<data key='pick'>M1H1</data>
<data key='pot'>100.0</data>
<data key='stake'>10.0</data>
</node>
<node id='2'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M1</data>
<data key='pos'>D1</data>
<data key='pick'>M1D1</data>
<data key='pot'>50.0</data>
<data key='stake'>5.0</data>
</node>
<node id='3'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M1</data>
<data key='pos'>A1</data>
<data key='pick'>M1A1</data>
<data key='pot'>150.0</data>
<data key='stake'>15.0</data>
</node>
<node id='4'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M2</data>
<data key='pos'>A2</data>
<data key='pick'>M2A2</data>
<data key='pot'>75.0</data>
<data key='stake'>10.0</data>
</node>
<node id='5'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M3</data>
<data key='pos'>A3</data>
<data key='pick'>M3A3</data>
<data key='pot'>70.0</data>
<data key='stake'>7.0</data>
</node>
<node id='6'>
<data key='labelV'>transaction</data>
<data key='type'>transaction</data>
<data key='match'>M1</data>
<data key='pos'>H1</data>
<data key='pick'>M1H1:M2D2</data>
<data key='pot'>60.0</data>
<data key='stake'>6.0</data>
</node>
<!-- edges -->
<edge id='20' source='1' target='4'>
<data key='labelE'>compatible</data>
</edge>
<edge id='21' source='1' target='5'>
<data key='labelE'>compatible</data>
</edge>
<edge id='22' source='1' target='6'>
<data key='labelE'>compatible</data>
</edge>
<edge id='23' source='2' target='4'>
<data key='labelE'>compatible</data>
</edge>
<edge id='24' source='2' target='5'>
<data key='labelE'>compatible</data>
</edge>
<edge id='25' source='3' target='4'>
<data key='labelE'>compatible</data>
</edge>
<edge id='26' source='3' target='5'>
<data key='labelE'>compatible</data>
</edge>
<edge id='27' source='4' target='1'>
<data key='labelE'>compatible</data>
</edge>
<edge id='28' source='4' target='2'>
<data key='labelE'>compatible</data>
</edge>
<edge id='29' source='4' target='3'>
<data key='labelE'>compatible</data>
</edge>
<edge id='30' source='4' target='5'>
<data key='labelE'>compatible</data>
</edge>
<edge id='31' source='5' target='1'>
<data key='labelE'>compatible</data>
</edge>
<edge id='32' source='5' target='2'>
<data key='labelE'>compatible</data>
</edge>
<edge id='33' source='5' target='3'>
<data key='labelE'>compatible</data>
</edge>
<edge id='34' source='5' target='4'>
<data key='labelE'>compatible</data>
</edge>
<edge id='35' source='5' target='6'>
<data key='labelE'>compatible</data>
</edge>
<edge id='36' source='6' target='1'>
<data key='labelE'>compatible</data>
</edge>
<edge id='37' source='6' target='5'>
<data key='labelE'>compatible</data>
</edge>
</graph>
</graphml>
I can't figure out how to add a property with with multiple values with graphML, so here's the code to add an additional value to V(6)
g.V(6).property(set, 'match', "M2")
The goal is to calculate the maximum pot in the graph by getting each node and its connected vertices, grouping the vertices of that node, by their match values ( grouping two nodes means they have at least one intersecting match value), taking the maximum of each group, and doing the sum for that node. At the end, with take the maximum value of all the nodes. I don't know if this is quite how it should be done but this is what I've tried:
g.E().hasLabel('compatible').outV().dedup().as('s'). //1. get all edges with comptible label
local( //2. for each node
union( //3. get the node and its first level connected vertices
identity(),
out()
)
.as('compat')
.values('match') //4. get the values of the match property
.as('match')
.local( //5. for each key in the list
select('compat') //6. get all vertices in the sub graph that have this value in their match list
.has('match',within(identity())) //7
.order().by("pot", decr).limit(1) //8. get the one with the highest pot value
)
.dedup() //9. remove duplicates
.values('pot')
.sum() //10. sum the values for this node
).max() //11. get the max for all nodes
EXPECTED RESULT:
For each vertex, we should have its BulkSet as follows:
V(5)==>[M1:[v[2],v[3],v[6],v[1]],M2:[v[4],v[6]],M3:[v[5]]]
V(6)==>[M1:[v[6],v[1]],M3:[v[5]]]
V(1)==>[M1:[v[1],v[6]],M2:[v[4], v[6]],M3:[v[5]]]
V(2)==>[M1:[v[2]],M2:[v[4]],M3:[v[5]]]
V(3)==>[M1:[v[3]],M2:[v[4]],M3:[v[5]]]
V(4)==>[M1:[v[1],v[2],v[3]],M2:[v[4]],M3:[v[5]]]
and the map of the maximum pots of each vertex:
V(5) ==> [M1:v[3],M2:v[4],M3:v[5]], Sum(pot) ==> 150 + 75 + 70 = 295
V(6) ==> [M1:v[1],M3:v[5]], Sum(pot) ==> 100 + 70 = 170
V(1) ==> [M1:v[1],M2:[v[4]],M3:v[5]], Sum(pot) ==> 100 + 75 + 70 = 295
V(2) ==> [M1:v[2],M2:v[4],M3:v[5]], Sum(pot) ==> 50 + 75 + 70 = 195
V(3) ==> [M1:v[3],M2:v[4],M3:v[5]], Sum(pot) ==> 150 + 75 + 70 = 295
V(4) ==> [M1:v[3],M2:v[4],M3:v[5]], Sum(pot) ==> 150 + 75 + 70 = 295
The final output should be 295.
The first problem is at 7. If I pass static values such as ['M1', 'M2'] I do get data but get an empty list with identity().
The second problem is at 8. I get multiple nodes instead of just the node with the maximum value.
I assume that your expected result is not quite right (M2 should be part of v[6]'s bulkset). That said, here's what I think the solution looks like:
gremlin> g.V().hasLabel("transaction").
map(union(identity(), out("compatible")).as("v").
values("match").
group().
by().
by(select("v").order().by("pot", decr)))
==>[M1:v[1],M2:v[4],M3:v[5]]
==>[M1:v[2],M2:v[4],M3:v[5]]
==>[M1:v[3],M2:v[4],M3:v[5]]
==>[M1:v[3],M2:v[4],M3:v[5]]
==>[M1:v[3],M2:v[4],M3:v[5]]
==>[M1:v[1],M2:v[6],M3:v[5]]
gremlin> g.V().hasLabel("transaction").
map(union(identity(), out("compatible")).as("v").
values("match").
group().
by().
by(select("v").order().by("pot", decr))).
map(select(values).unfold().values("pot").fold())
==>[100.0,75.0,70.0]
==>[50.0,75.0,70.0]
==>[150.0,75.0,70.0]
==>[150.0,75.0,70.0]
==>[150.0,75.0,70.0]
==>[100.0,60.0,70.0]
gremlin> g.V().hasLabel("transaction").
map(union(identity(), out("compatible")).as("v").
values("match").
group().
by().
by(select("v").order().by("pot", decr))).
map(select(values).unfold().values("pot").sum()).
max()
==>295.0
Worked for me on the example above:
g.V().hasLabel('transaction').local(
union(identity(), out('compatible')).as('v')
.values('match')
.group()
.by()
.by(select('v').values('pot').max()).select(values).unfold().sum()
).max()

Read a graphML file created by yEd into R

I have this graphML file, which was created with yworks yEd.
Now I want to read this file into R to plot it via visNetwork. But reading the file into R using the igraph::read_graph function fails:
library(igraph)
example <- igraph::read_graph("data/graphml_example.graphml", format = "graphml")
Warning messages:
1: In read.graph.graphml(file, ...) : At foreign-graphml.c:651
:Ignoring because of a missing or unknown 'attr.type'
attribute
...
This is the graphml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--Created by yFiles for HTML 2.0.1.4-->
<graphml xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml.html/2.0/ygraphml.xsd " xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:demostyle="http://www.yworks.com/yFilesHTML/demos/FlatDemoStyle/1.0" xmlns:bpmn="http://www.yworks.com/xml/yfiles-for-html/bpmn/2.0" xmlns:demotablestyle="http://www.yworks.com/yFilesHTML/demos/FlatDemoTableStyle/1.0" xmlns:compat="http://www.yworks.com/xml/yfiles-compat-arrows/1.0" xmlns:VuejsNodeStyle="http://www.yworks.com/demos/yfiles-vuejs-node-style/1.0" xmlns:y="http://www.yworks.com/xml/yfiles-common/3.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/3.0" xmlns:yjs="http://www.yworks.com/xml/yfiles-for-html/2.0/xaml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<key id="d0" for="node" attr.type="boolean" attr.name="Expanded" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/folding/Expanded">
<default>true</default>
</key>
<key id="d1" for="node" attr.name="NodeLabels" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/NodeLabels"/>
<key id="d2" for="node" attr.name="NodeGeometry" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/NodeGeometry"/>
<key id="d3" for="all" attr.name="UserTags" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/UserTags"/>
<key id="d4" for="node" attr.name="NodeStyle" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/NodeStyle"/>
<key id="d5" for="node" attr.name="NodeViewState" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/folding/1.1/NodeViewState"/>
<key id="d6" for="edge" attr.name="EdgeLabels" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/EdgeLabels"/>
<key id="d7" for="edge" attr.name="EdgeGeometry" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/EdgeGeometry"/>
<key id="d8" for="edge" attr.name="EdgeStyle" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/EdgeStyle"/>
<key id="d9" for="edge" attr.name="EdgeViewState" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/folding/1.1/EdgeViewState"/>
<key id="d10" for="port" attr.name="PortLocationParameter" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/PortLocationParameter">
<default>
<x:Static Member="y:FreeNodePortLocationModel.NodeCenterAnchored"/>
</default>
</key>
<key id="d11" for="port" attr.name="PortStyle" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/PortStyle">
<default>
<x:Static Member="y:VoidPortStyle.Instance"/>
</default>
</key>
<key id="d12" for="port" attr.name="PortViewState" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/folding/1.1/PortViewState"/>
<key id="d13" attr.name="SharedData" y:attr.uri="http://www.yworks.com/xml/yfiles-common/2.0/SharedData"/>
<data key="d13">
<y:SharedData>
<yjs:Color x:Key="1" value="#FF316C94"/>
<yjs:Color x:Key="2" value="#FF006A70"/>
<yjs:Color x:Key="3" value="#FFC9E2E2"/>
<yjs:Arrow x:Key="4" type="NONE"/>
</y:SharedData>
</data>
<graph id="G" edgedefault="directed">
<node id="n0">
<data key="d1">
<x:List>
<y:Label>
<y:Label.Text>A</y:Label.Text>
<y:Label.LayoutParameter>
<y:RatioAnchoredLabelModelParameter LayoutOffset="45.2981354349738,20.5"/>
</y:Label.LayoutParameter>
<y:Label.Style>
<yjs:DefaultLabelStyle horizontalTextAlignment="CENTER" autoFlip="false" textFill="#FFFFFFFF" textSize="16">
<yjs:DefaultLabelStyle.font>
<yjs:Font fontSize="16" fontFamily="Calibri" fontWeight="BOLD"/>
</yjs:DefaultLabelStyle.font>
</yjs:DefaultLabelStyle>
</y:Label.Style>
</y:Label>
</x:List>
</data>
<data key="d2">
<y:RectD X="1917.9576310558555" Y="2912.1062122893554" Width="214.8931458699476" Height="57"/>
</data>
<data key="d4">
<demostyle:FlowchartNodeStyle type="terminator">
<demostyle:FlowchartNodeStyle.stroke>
<yjs:Stroke fill="#FF000000" miterLimit="1.45" thickness="2"/>
</demostyle:FlowchartNodeStyle.stroke>
<demostyle:FlowchartNodeStyle.fill>
<yjs:LinearGradient spreadMethod="PAD" startPoint="0,0" endPoint="1,1">
<yjs:GradientStop color="{y:GraphMLReference 1}" offset="0"/>
<yjs:GradientStop color="{y:GraphMLReference 1}" offset="1"/>
</yjs:LinearGradient>
</demostyle:FlowchartNodeStyle.fill>
</demostyle:FlowchartNodeStyle>
</data>
<port name="p0"/>
</node>
<node id="n1">
<data key="d1">
<x:List>
<y:Label>
<y:Label.Text>B</y:Label.Text>
<y:Label.LayoutParameter>
<y:RatioAnchoredLabelModelParameter LayoutOffset="68.91532293497403,12.5"/>
</y:Label.LayoutParameter>
<y:Label.Style>
<yjs:DefaultLabelStyle horizontalTextAlignment="CENTER" autoFlip="false" textFill="#FFFFFFFF" textSize="16">
<yjs:DefaultLabelStyle.font>
<yjs:Font fontSize="16" fontFamily="Calibri" fontWeight="BOLD"/>
</yjs:DefaultLabelStyle.font>
</yjs:DefaultLabelStyle>
</y:Label.Style>
</y:Label>
</x:List>
</data>
<data key="d2">
<y:RectD X="1917.9576310558555" Y="2809.9061759957385" Width="214.89314586994806" Height="57"/>
</data>
<data key="d4">
<demostyle:FlowchartNodeStyle type="document">
<demostyle:FlowchartNodeStyle.stroke>
<yjs:Stroke fill="#FF000000" miterLimit="1.45" thickness="2"/>
</demostyle:FlowchartNodeStyle.stroke>
<demostyle:FlowchartNodeStyle.fill>
<yjs:LinearGradient spreadMethod="PAD" startPoint="0,0" endPoint="1,1">
<yjs:GradientStop color="{y:GraphMLReference 2}" offset="0"/>
<yjs:GradientStop color="{y:GraphMLReference 2}" offset="1"/>
</yjs:LinearGradient>
</demostyle:FlowchartNodeStyle.fill>
</demostyle:FlowchartNodeStyle>
</data>
<port name="p0"/>
<port name="p1"/>
<port name="p2"/>
</node>
<node id="n2">
<data key="d1">
<x:List>
<y:Label>
<y:Label.Text>X</y:Label.Text>
<y:Label.LayoutParameter>
<y:RatioAnchoredLabelModelParameter LayoutOffset="62.41922918497403,20.5"/>
</y:Label.LayoutParameter>
<y:Label.Style>
<yjs:DefaultLabelStyle horizontalTextAlignment="CENTER" autoFlip="false" textFill="#FF000000" textSize="16">
<yjs:DefaultLabelStyle.font>
<yjs:Font fontSize="16" fontFamily="Calibri" fontWeight="BOLD"/>
</yjs:DefaultLabelStyle.font>
</yjs:DefaultLabelStyle>
</y:Label.Style>
</y:Label>
</x:List>
</data>
<data key="d2">
<y:RectD X="1917.9576310558555" Y="2702.7061397021216" Width="214.89314586994806" Height="57"/>
</data>
<data key="d4">
<demostyle:FlowchartNodeStyle type="start1">
<demostyle:FlowchartNodeStyle.stroke>
<yjs:Stroke fill="#FF000000" miterLimit="1.45" thickness="2"/>
</demostyle:FlowchartNodeStyle.stroke>
<demostyle:FlowchartNodeStyle.fill>
<yjs:LinearGradient spreadMethod="PAD" startPoint="0,0" endPoint="1,1">
<yjs:GradientStop color="{y:GraphMLReference 3}" offset="0"/>
<yjs:GradientStop color="{y:GraphMLReference 3}" offset="1"/>
</yjs:LinearGradient>
</demostyle:FlowchartNodeStyle.fill>
</demostyle:FlowchartNodeStyle>
</data>
<port name="p0"/>
</node>
<edge id="e0" source="n1" target="n0" sourceport="p0" targetport="p0">
<data key="d7">
<x:List>
<y:Bend Location="2025.4042039908295,2898.850638702741"/>
</x:List>
</data>
<data key="d8">
<yjs:PolylineEdgeStyle sourceArrow="{y:GraphMLReference 4}">
<yjs:PolylineEdgeStyle.stroke>
<yjs:Stroke fill="#FF000000" miterLimit="1.45"/>
</yjs:PolylineEdgeStyle.stroke>
<yjs:PolylineEdgeStyle.targetArrow>
<yjs:Arrow stroke="#FF000000" fill="#FF000000"/>
</yjs:PolylineEdgeStyle.targetArrow>
</yjs:PolylineEdgeStyle>
</data>
</edge>
<edge id="e1" source="n2" target="n1" sourceport="p0" targetport="p2">
<data key="d8">
<yjs:PolylineEdgeStyle sourceArrow="{y:GraphMLReference 4}">
<yjs:PolylineEdgeStyle.stroke>
<yjs:Stroke fill="#FF000000" miterLimit="1.45"/>
</yjs:PolylineEdgeStyle.stroke>
<yjs:PolylineEdgeStyle.targetArrow>
<yjs:Arrow stroke="#FF000000" fill="#FF000000"/>
</yjs:PolylineEdgeStyle.targetArrow>
</yjs:PolylineEdgeStyle>
</data>
</edge>
</graph>
</graphml>
How can I read this into R so that properties like shape and color are preserved?

improving code to find nodes with using attributename and node

I have following xml:
<TextWithNodes><Node id="0" />astralis<Node id="8" /> <Node id="9" />ltd<Node id="12" />
<Node id="14" />{<Node id="15" />DOCUMENT<Node id="23" />}<Node id="24" /> <Node id="25" />{<Node id="26" />TYPE<Node id="30" />}<Node id="31" />EX-<Node id="34" />10<Node id="36" />.<Node id="37" />12<Node id="39" /> <Node id="40" />{<Node id="41" />SEQUENCE<Node id="49" />}<Node id="50" />3<Node id="51" /> <Node id="52" />{<Node id="53" />FILENAME<Node id="61" />}<Node id="62" />e<Node id="63" />300201<Node id="69" />_<Node id="70" />ex<Node id="72" />10<Node id="74" />-<Node id="75" />12<Node id="77" />.<Node id="78" />txt<Node id="81" /> </TextWithNodes>
and I need to pick node from Id 25 to id 75. It is a portion of XML. Original XML is very long.
I am using following code:
Dim reader As XmlTextReader = New XmlTextReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/gate_xml_output.xml"))
reader.WhitespaceHandling = WhitespaceHandling.None
Dim xmlDoc As XmlDocument = New XmlDocument()
'Load the file into the XmlDocument
xmlDoc.Load(reader)
'Close off the connection to the file.
reader.Close()
Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("//TextWithNodes/node()[preceding-sibling::Node[#id=" & startNode & "] and following-sibling::Node[#id=" & endNode & "]]")
Dim sb As StringBuilder = New StringBuilder
For Each childNode As XmlNode In nodeList
If childNode.Value IsNot Nothing Then
sb.Append(childNode.Value & " ")
End If
Next
' read the text between these nodes
ExtractText = sb.ToString
It is working but It is very slow. Any alternative of getting this data from XML ?
Please suggest.
Thanks
Explore Linq to XML; it should be quicker: http://msdn.microsoft.com/en-us/library/bb387098.aspx
Also good info here: http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
There's ton of info out there.
Here's an article about performance comparison with xmldoc and xmlreader: http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html
and another: http://msdn.microsoft.com/en-us/library/bb387048.aspx
+1
You could do it with XPath. Like this:
Dim sb As New StringBuilder()
Dim document As New XPathDocument("C:\testfile2.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim iterator As XPathNodeIterator = navigator.Select("yourPath")
While iterator.MoveNext()
sb.Append(iterator.Current.Value)
End While
You can find more information here
The evaluation of this XPath expression has only O(N) complexity (linear) vs. O(N^2) for your original XPath expression:
/*/Node[#id >= 15 and not(#id > 75)]
This selects any Node element that is a child of the top element of the XML document and the value of whose id attribute is between the two specified lower and upper limits: respectively 15 and 75.
Here we assume that the values of the id attributes of Node elements are monotonously increasing -- exactly as in the provided XML document.
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/Node[#id >= 15 and not(#id > 75)]"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is performed on the provided XML document:
<TextWithNodes>
<Node id="0" />astralis
<Node id="8" />
<Node id="9" />ltd
<Node id="12" />
<Node id="14" />{
<Node id="15" />DOCUMENT
<Node id="23" />}
<Node id="24" />
<Node id="25" />{
<Node id="26" />TYPE
<Node id="30" />}
<Node id="31" />EX-
<Node id="34" />10
<Node id="36" />.
<Node id="37" />12
<Node id="39" />
<Node id="40" />{
<Node id="41" />SEQUENCE
<Node id="49" />}
<Node id="50" />3
<Node id="51" />
<Node id="52" />{
<Node id="53" />FILENAME
<Node id="61" />}
<Node id="62" />e
<Node id="63" />300201
<Node id="69" />_
<Node id="70" />ex
<Node id="72" />10
<Node id="74" />-
<Node id="75" />12
<Node id="77" />.
<Node id="78" />txt
<Node id="81" />
</TextWithNodes>
the XPath expression is evaluated and the selected nodes are output:
<Node id="15"/>
<Node id="23"/>
<Node id="24"/>
<Node id="25"/>
<Node id="26"/>
<Node id="30"/>
<Node id="31"/>
<Node id="34"/>
<Node id="36"/>
<Node id="37"/>
<Node id="39"/>
<Node id="40"/>
<Node id="41"/>
<Node id="49"/>
<Node id="50"/>
<Node id="51"/>
<Node id="52"/>
<Node id="53"/>
<Node id="61"/>
<Node id="62"/>
<Node id="63"/>
<Node id="69"/>
<Node id="70"/>
<Node id="72"/>
<Node id="74"/>
<Node id="75"/>

Resources