Package ‘RNeo4j’ April 6, 2016 Title Neo4j Driver for R LazyData true Version 1.6.4 Date 2016-04-05 Author Nicole White License MIT + file LICENSE Imports httr (>= 1.1.0), jsonlite, rstudioapi Maintainer Nicole White <[email protected]> Description Neo4j, a graph database, allows users to store their data as a property graph. A graph consists of nodes that are connected by relationships; both nodes and relationships can have properties, or key-value pairs. RNeo4j is Neo4j's R driver. It allows users to read and write data from and to Neo4j directly from their R environment by exposing an interface for interacting with nodes, relationships, paths, and more. Most notably, it allows users to retrieve Cypher query results as R data frames, where Cypher is Neo4j's graph query language. Visit <http://www.neo4j.com> to learn more about Neo4j. URL http://github.com/nicolewhite/RNeo4j BugReports https://github.com/nicolewhite/RNeo4j/issues Suggests testthat NeedsCompilation no Repository CRAN Date/Publication 2016-04-06 09:43:05 R topics documented: addConstraint . addIndex . . . addLabel . . . allDijkstra . . . allShortestPaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 4 5 5 7 R topics documented: 2 appendCypher . . browse . . . . . . clear . . . . . . . createNode . . . createRel . . . . cypher . . . . . . cypherToList . . delete . . . . . . deleteProp . . . . dijkstra . . . . . dropConstraint . dropIndex . . . . dropLabel . . . . endNode . . . . . getConstraint . . getID . . . . . . getIndex . . . . . getLabel . . . . . getLabeledNodes getNodes . . . . getOrCreateNode getPaths . . . . . getRels . . . . . getSingleNode . . getSinglePath . . getSingleRel . . . getType . . . . . getUniqueNode . importSample . . incomingRels . . nodes . . . . . . outgoingRels . . rels . . . . . . . . RNeo4j . . . . . shortestPath . . . startGraph . . . . startNode . . . . updateProp . . . Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 10 10 11 12 13 14 15 16 17 18 19 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 35 36 37 38 39 40 41 42 43 45 addConstraint addConstraint 3 Uniqueness Constraints Description Add a uniqueness constraint to a label and property key. Usage addConstraint(graph, label, key) Arguments graph A graph object. label A character string. key A character string. Details A uniqueness constraint cannot be added to a (label, key) pair that already has an index applied. Attempting to add a uniqueness constraint where an index already exists results in an error. Use getIndex to view any pre-existing indexes. If you wish to add a uniqueness constraint, use dropIndex to drop the index. See Also getConstraint, dropConstraint Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) addConstraint(graph, "Person", "name") createNode(graph, "Person", name = "Alice") createNode(graph, "Person", name = "Bob") try(createNode(graph, "Person", name = "Alice")) ## End(Not run) 4 addIndex addIndex Indexes Description Add an index to a node label and property key. Usage addIndex(graph, label, key) Arguments graph A graph object. label A character string. key A character string. Details An index already exists for any (label, key) pair that has a uniqueness constraint applied. Attempting to add an index where a uniqueness constraint already exists results in an error. Use getConstraint to view any pre-existing uniqueness constraints. If a uniqueness constraint already exists for the (label, key) pair, then it must be true that the index exists as well; adding an index is unnecessary. See Also getIndex, dropIndex Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "Person", name="Alice", status="Employed") addIndex(graph, "Person", "status") ## End(Not run) addLabel 5 addLabel Node Labels Description Add a label or multiple labels to an existing node object. Usage addLabel(node, ...) Arguments node A node object. ... A character vector. See Also getLabel, dropLabel Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, name = "Alice") bob = createNode(graph, name = "Bob") addLabel(alice, "Person") addLabel(bob, c("Person", "Student")) alice bob ## End(Not run) allDijkstra Weighted Shortest Paths Description Deprecated. Use allShortestPaths. Retrieve all the shortest paths between two nodes weighted by a cost property. 6 allDijkstra Usage allDijkstra(fromNode, relType, toNode, cost_property = character(), direction = "out") Arguments fromNode A node object. relType A character string. The relationship type to traverse. toNode A node object. cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights. direction A character string. The relationship direction to traverse. Should be "in" or "out". Value A list of path objects. See Also dijkstra Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") elaine = createNode(graph, "Person", name = "Elaine") r1 r2 r3 r4 r5 r6 = = = = = = createRel(alice, "KNOWS", bob, weight=1.5) createRel(bob, "KNOWS", charles, weight=2) createRel(bob, "KNOWS", david, weight=4) createRel(charles, "KNOWS", david, weight=1) createRel(alice, "KNOWS", elaine, weight=2) createRel(elaine, "KNOWS", david, weight=2.5) p = allDijkstra(alice, "KNOWS", david, cost_property="weight") p[[1]]$length p[[1]]$weight nodes(p[[1]]) p[[2]]$length p[[2]]$weight nodes(p[[2]]) allShortestPaths 7 ## End(Not run) allShortestPaths Shortest Paths and Weighted Shortest Paths Description Retrieve all the shortest paths between two nodes. Usage allShortestPaths(fromNode, relType, toNode, direction = "out", max_depth = 1, cost_property = character()) Arguments fromNode A node object. relType A character string. The relationship type to traverse. toNode A node object. direction A character string. The relationship direction to traverse; this can be either "in", "out", or "all". max_depth An integer. The maximum depth of the path. cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights. Value A list of path objects. See Also shortestPath Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") elaine = createNode(graph, "Person", name = "Elaine") r1 = createRel(alice, "KNOWS", bob, weight=1.5) 8 appendCypher r2 r3 r4 r5 r6 = = = = = createRel(bob, "KNOWS", charles, weight=2) createRel(bob, "KNOWS", david, weight=4) createRel(charles, "KNOWS", david, weight=1) createRel(alice, "KNOWS", elaine, weight=2) createRel(elaine, "KNOWS", david, weight=2.5) # The default max_depth of 1 will not find any paths. # There are no length-1 paths between alice and david. p = allShortestPaths(alice, "KNOWS", david) is.null(p) # Set the max_depth to 4. p = allShortestPaths(alice, "KNOWS", david, max_depth = 4) n = lapply(p, nodes) lapply(n, function(x) sapply(x, function(y) y$name)) # Setting the direction to "in" and traversing from alice to david will not find a path. p = allShortestPaths(alice, "KNOWS", david, direction = "in", max_depth = 4) is.null(p) # Setting the direction to "in" and traversing from david to alice will find paths. p = allShortestPaths(david, "KNOWS", alice, direction = "in", max_depth = 4) n = lapply(p, nodes) lapply(n, function(x) sapply(x, function(y) y$name)) # Find all the weighted shortest paths between Alice and David. p = allShortestPaths(alice, "KNOWS", david, cost_property="weight") p[[1]]$length p[[1]]$weight nodes(p[[1]]) p[[2]]$length p[[2]]$weight nodes(p[[2]]) ## End(Not run) appendCypher Transactions Description Transactions Usage appendCypher(transaction, query, ...) commit(transaction) appendCypher 9 newTransaction(graph) Arguments transaction query ... graph A transaction object. A character string. A named list. Optional parameters to pass to the query in the form key = value, if applicable. A graph object. Value newTransaction returns a transaction object. Both appendCypher and commit return NULL. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) data = data.frame(Origin = c("SFO", "AUS", "MCI"), FlightNum = c(1, 2, 3), Destination = c("PDX", "MCI", "LGA")) query = " MERGE (origin:Airport {name:{origin_name}}) MERGE (destination:Airport {name:{dest_name}}) CREATE (origin)<-[:ORIGIN]-(:Flight {number:{flight_num}})-[:DESTINATION]->(destination) " t = newTransaction(graph) for (i in 1:nrow(data)) { origin_name = data[i, ]$Origin dest_name = data[i, ]$Dest flight_num = data[i, ]$FlightNum } appendCypher(t, query, origin_name = origin_name, dest_name = dest_name, flight_num = flight_num) commit(t) cypher(graph, "MATCH (o:Airport)<-[:ORIGIN]-(f:Flight)-[:DESTINATION]->(d:Airport) RETURN o.name, f.number, d.name") ## End(Not run) 10 clear browse Neo4j Browser Description Open the Neo4j browser either in the viewer pane or in the default browser. Usage browse(graph, viewer = TRUE) Arguments graph viewer A graph object. A logical constant. If TRUE, open the browser in the viewer pane; otherwise, open in the default browser. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") browse(graph) ## End(Not run) clear Clear the Database Description Delete all nodes, relationships, constraints, and indexes from Neo4j. Usage clear(graph, input = TRUE) Arguments graph input A graph object. A logical constant. If FALSE, do not require user input for confirmation. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) ## End(Not run) createNode createNode 11 Nodes Description Create a node in the graph with the given label and properties. Usage createNode(graph, .label = character(), ...) Arguments graph A graph object. .label A character string. ... A named list. Node properties in the form key = value. Value A node object. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) bob = createNode(graph, "Person", name = "Bob", age = 24, kids = c("Jenny", "Larry")) charles = createNode(graph, c("Person", "Student"), name = "Charles", age = 21) bob charles props = list(name="David", age = 26) david = createNode(graph, "Person", props) david ## End(Not run) 12 createRel createRel Relationships Description Create a relationship between two nodes with the given type and properties. Usage createRel(.fromNode, .relType, .toNode, ...) Arguments .fromNode A node object. .relType A character string. .toNode A node object. ... A named list. Relationship properties in the form key = value. Value A relationship object. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") createRel(alice, "WORKS_WITH", bob) createRel(bob, "KNOWS", charles, since = 2000, through = "Work") props = list(since = 2001, through = "School") createRel(alice, "KNOWS", charles, props) ## End(Not run) cypher cypher 13 Cypher Queries to Data Frames Description Retrieve Cypher query results as a data frame. Usage cypher(graph, query, ...) Arguments graph A graph object. query A character string. ... A named list. Parameters to pass to the query in the form key = value, if applicable. Details If returning data, you can only query for tabular results. That is, you can’t return node or relationship entities. See cypherToList for returning non-tabular data. Value A data.frame. See Also cypherToList Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice", age = 23) bob = createNode(graph, "Person", name = "Bob", age = 22) charles = createNode(graph, "Person", name = "Charles", age = 25) david = createNode(graph, "Person", name = "David", age = 20) createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) createRel(charles, "KNOWS", david) cypher(graph, "MATCH n RETURN n.name, n.age") 14 cypherToList query = "MATCH n WHERE n.age < {age} RETURN n.name, n.age" cypher(graph, query, age = 24) query = "MATCH n WHERE n.name IN {names} RETURN n.name, n.age" names = c("Alice", "Charles") cypher(graph, query, names = names) query = "MATCH n WHERE n.age > {age1} AND n.age < {age2} RETURN n.name" cypher(graph, query, age1=22, age2=30) params = list(age1=22, age2=30) cypher(graph, query, params) ## End(Not run) cypherToList Cypher Queries to Lists Description Retrieve Cypher query results as a list. Usage cypherToList(graph, query, ...) Arguments graph A graph object. query A character string. ... A named list. Parameters to pass to the query in the form key = value, if applicable. Value A list. See Also cypher Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice", age = 23) bob = createNode(graph, "Person", name = "Bob", age = 22) delete 15 charles = createNode(graph, "Person", name = "Charles", age = 25) david = createNode(graph, "Person", name = "David", age = 20) createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) createRel(charles, "KNOWS", david) cypherToList(graph, "MATCH n RETURN n, n.age") cypherToList(graph, "MATCH (n)-[:KNOWS]-(m) RETURN n, COLLECT(m) AS friends, COUNT(m) AS num_friends") cypherToList(graph, "MATCH p = (n)-[:KNOWS]-(m) RETURN p") cypherToList(graph, "MATCH p = (n)-[:KNOWS]-(m) WHERE n.name = {name} RETURN p", name="Alice") ## End(Not run) delete Delete Nodes and Relationships Description Delete node or relationship object(s) from the graph. Usage delete(...) Arguments ... A list of entities to delete. Details Nodes with incoming or outgoing relationships cannot be deleted. All incoming and outgoing relationships need to be deleted before the node can be deleted. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, name = "Alice") bob = createNode(graph, name = "Bob") rel = createRel(alice, "WORKS_WITH", bob) 16 deleteProp delete(rel) delete(alice, bob) ## End(Not run) deleteProp Delete Node and Relationship Properties Description For a node or relationship object, delete the named properties or delete all properties. Usage deleteProp(entity, ..., all = FALSE) Arguments entity ... all A node or relationship object. A character vector. The properties to delete. A logical constant. If TRUE, delete all properties. Value A node or relationship object. See Also updateProp Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice", age = 23, status = "Married") bob = createNode(graph, "Person", name = "Bob", age = 22, status = "Married") charles = createNode(graph, "Person", name = "Charles", age = 25, status = "Unmarried") alice = deleteProp(alice, "age") bob = deleteProp(bob, c("name", "age")) charles = deleteProp(charles, all = TRUE) alice bob charles ## End(Not run) dijkstra 17 dijkstra Weighted Shortest Paths Description Deprecated. Use shortestPath. Retrieve the shortest path between two nodes weighted by a cost property. Usage dijkstra(fromNode, relType, toNode, cost_property = character(), direction = "out") Arguments fromNode A node object. relType A character string. The relationship type to traverse. toNode A node object. cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights. direction A character string. The relationship direction to traverse. Should be "in" or "out". Value A path object. See Also allDijkstra Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") elaine = createNode(graph, "Person", name = "Elaine") r1 r2 r3 r4 r5 = = = = = createRel(alice, "KNOWS", bob, weight=1.5) createRel(bob, "KNOWS", charles, weight=2) createRel(bob, "KNOWS", david, weight=4) createRel(charles, "KNOWS", david, weight=1) createRel(alice, "KNOWS", elaine, weight=2) 18 dropConstraint r6 = createRel(elaine, "KNOWS", david, weight=2.5) p = dijkstra(alice, "KNOWS", david, cost_property="weight") p$length p$weight nodes(p) ## End(Not run) dropConstraint Uniqueness Constraints Description Drop uniqueness constraint(s) for a given label and property key or for the entire graph database. Usage dropConstraint(graph, label = character(), key = character(), all = FALSE) Arguments graph A graph object. label A character string. key A character string. all A logical constant. If TRUE, drop all constraints in the graph. See Also addConstraint, getConstraint Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) addConstraint(graph, "Person", "name") getConstraint(graph) dropConstraint(graph, "Person", "name") getConstraint(graph) ## End(Not run) dropIndex 19 dropIndex Indexes Description Drop the index(es) for a given node label and property key or for the entire graph database. Usage dropIndex(graph, label = character(), key = character(), all = FALSE) Arguments graph A graph object. label A character string. key A character string. all A logical constant. If TRUE, drop all indexes from the graph. Details Dropping a uniqueness constraint necessarily drops the index as well. It is unnecessary to use dropIndex if dropConstraint has already been applied to the same (label, key) pair. See Also addIndex, getIndex Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) addIndex(graph, "Person", "status") getIndex(graph) dropIndex(graph, "Person", "status") getIndex(graph) ## End(Not run) 20 endNode dropLabel Node Labels Description Drop the specified label(s) from a node. Usage dropLabel(node, ..., all = FALSE) Arguments node A node object. ... A character vector. all A logical constant. If TRUE, drop all labels from the node. See Also addLabel, getLabel Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, c("Person", "Student"), name = "Bob") dropLabel(alice, "Person") dropLabel(bob, all = TRUE) alice bob ## End(Not run) endNode Retrieve Nodes from Relationships or Paths Description Retrieve the end node from a relationship or path object. getConstraint 21 Usage endNode(object) Arguments object A relationship or path object. Value A node object. See Also startNode Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") rel = createRel(alice, "WORKS_WITH", bob) endNode(rel) query = " MATCH p = (a:Person)-[:WORKS_WITH]->(b:Person) WHERE a.name = 'Alice' AND b.name = 'Bob' RETURN p " path = cypherToList(graph, query)[[1]]$p endNode(path) ## End(Not run) getConstraint Uniqueness Constraints Description Get all uniqueness constraints for a given label or for the entire graph database. Usage getConstraint(graph, label = character()) 22 getID Arguments graph A graph object. label A character vector. Details Omitting the label argument returns all uniqueness constraints in the graph database. Value A data.frame. See Also addConstraint, dropConstraint Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) addConstraint(graph, "Person", "name") addConstraint(graph, "City", "name") getConstraint(graph, "Person") getConstraint(graph) ## End(Not run) getID Internal IDs Description Retrieve the internal ID of a node or relationship object. Usage getID(entity) Arguments entity Value An integer. A node or relationship object. getIndex 23 Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") getID(alice) ## End(Not run) getIndex Indexes Description Get all indexes for a given label or for the entire graph database. Usage getIndex(graph, label = character()) Arguments graph A graph object. label A character vector. Details Omitting the label argument returns all indexes in the graph database. Value A data.frame. See Also addIndex, dropIndex Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) addIndex(graph, "Person", "status") addIndex(graph, "School", "type") getIndex(graph, "Person") getIndex(graph) 24 getLabel ## End(Not run) getLabel Node Labels Description Get all node labels for a given node object or for the entire graph database. Usage getLabel(object) Arguments object A graph or node object. Value A character vector. See Also addLabel, dropLabel Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, c("Person", "Student"), name = "Bob") getLabel(alice) getLabel(graph) ## End(Not run) getLabeledNodes 25 getLabeledNodes Retrieve Nodes by Label and Property Description Retrieve nodes from the graph with the specified label and optional key = value pair. Usage getLabeledNodes(graph, .label, ...) Arguments graph A graph object. .label A character string. ... A named list. A key = value pair to search the labeled nodes. Value A list of node objects. See Also getUniqueNode Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "School", name = "University of Texas at Austin") createNode(graph, "School", name = "Louisiana State University") createNode(graph, "Person", name = "Nicole", status = "Employed") createNode(graph, "Person", name = "Drew", status = "Employed") createNode(graph, "Person", name = "Aaron", status = "Unemployed") schools = getLabeledNodes(graph, "School") sapply(schools, function(s) s$name) employed_people = getLabeledNodes(graph, "Person", status = "Employed") sapply(employed_people, function(p) p$name) ## End(Not run) 26 getNodes getNodes Retrieve Nodes with Cypher Queries Description Deprecated. Use cypherToList. Retrieve nodes from the graph with a Cypher query. Usage getNodes(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A list of node objects. See Also getSingleNode Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "Person", name = "Alice", age = 23) createNode(graph, "Person", name = "Bob", age = 22) createNode(graph, "Person", name = "Charles", age = 25) query = "MATCH (p:Person) WHERE p.age < 25 RETURN p" younger_than_25 = getNodes(graph, query) sapply(younger_than_25, function(p) p$name) query = "MATCH (p:Person) WHERE p.age > {age} RETURN p" older_than_22 = getNodes(graph, query, age = 22) getOrCreateNode 27 sapply(older_than_22, function(p) p$name) ## End(Not run) getOrCreateNode Create Unique Node or Retrieve Unique Node Description Create a unique node or retrieve it if it already exists. Usage getOrCreateNode(graph, .label, ...) Arguments graph A graph object. .label A character string. ... A named list. Node properties in the form key = value. Details A uniqueness constraint must exist for the given node label and first key = value pair listed in .... Use addConstraint to add a uniqueness constraint. Value A node object. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "Person", name = "Alice", age = 24) createNode(graph, "Person", name = "Bob", age = 21) addConstraint(graph, "Person", "name") # Alice is retrieved from the graph; a new node is not created. alice = getOrCreateNode(graph, "Person", name = "Alice") # Additional properties listed after the unique key = value # pair are ignored if the node is retrieved instead of # created. bob = getOrCreateNode(graph, "Person", name = "Bob", age = 22) 28 getPaths # Node doesn't exist, so it is created. charles = getOrCreateNode(graph, "Person", name = "Charles") # There are now three nodes in the graph. length(getLabeledNodes(graph, "Person")) ## End(Not run) getPaths Retrieve Paths with Cypher Queries Description Deprecated. Use cypherToList. Retrieve paths from the graph with a Cypher query. Usage getPaths(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A list of path objects. See Also getSinglePath Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice", age = 23) bob = createNode(graph, "Person", name = "Bob", age = 22) charles = createNode(graph, "Person", name = "Charles", age = 25) createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) query = "MATCH p = (:Person {name:'Alice'})-[:KNOWS]->(:Person) RETURN p" paths = getPaths(graph, query) getRels 29 lapply(paths, startNode) lapply(paths, endNode) ## End(Not run) getRels Retrieve Relationships with Cypher Queries Description Deprecated. Use cypherToList. Retrieve relationships from the graph with a Cypher query. Usage getRels(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A list of relationship objects. See Also getSingleRel Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) createRel(charles, "KNOWS", david) createRel(bob, "WORKS_WITH", david) createRel(alice, "WORKS_WITH", david) 30 getSingleNode getRels(graph, "MATCH (:Person)-[k:KNOWS]->(:Person) RETURN k") getRels(graph, "MATCH (:Person {name:{name}})-[r]->(:Person) RETURN r", name = "Alice") ## End(Not run) getSingleNode Retrieve Nodes with Cypher Queries Description Deprecated. Use cypherToList. Retrieve a single node from the graph with a Cypher query. Usage getSingleNode(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A node object. See Also getNodes Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "Person", name = "Alice", age = 23) createNode(graph, "Person", name = "Bob", age = 22) createNode(graph, "Person", name = "Charles", age = 25) query = "MATCH (p:Person) WITH p ORDER BY p.age DESC RETURN p LIMIT 1" getSingleNode(graph, query) query = "MATCH (p:Person {name:{name}}) getSinglePath 31 RETURN p" getSingleNode(graph, query, name = "Alice") ## End(Not run) getSinglePath Retrieve Paths with Cypher Queries Description Deprecated. Use cypherToList. Retrieve a single path from the graph with a Cypher query. Usage getSinglePath(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A path object. See Also getPaths Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice", age = 23) bob = createNode(graph, "Person", name = "Bob", age = 22) charles = createNode(graph, "Person", name = "Charles", age = 25) createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) query = "MATCH p = (:Person {name:'Alice'})-[:KNOWS]->(:Person {name:'Bob'}) RETURN p" getSinglePath(graph, query) query = "MATCH p = (:Person {name:'Alice'})-[:KNOWS]->(:Person {name:{name}}) RETURN p" 32 getSingleRel getSinglePath(graph, query, name = "Charles") ## End(Not run) getSingleRel Retrieve Relationships with Cypher Queries Description Deprecated. Use cypherToList. Retrieve a single relationship from the graph with a Cypher query. Usage getSingleRel(graph, query, ...) Arguments graph A graph object. query A character string. ... Parameters to pass to the query in the form key = value, if applicable. Value A relationship object. See Also getRels Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") createRel(alice, "WORKS_WITH", bob) createRel(bob, "KNOWS", charles, since = 2000, through = "Work") query = "MATCH (:Person {name:'Alice'})-[r:WORKS_WITH]->(:Person {name:'Bob'}) RETURN r" getSingleRel(graph, query) query = "MATCH (:Person {name:{start}})-[r:KNOWS]->(:Person {name:{end}}) getType 33 RETURN r" getSingleRel(graph, query, start = "Bob", end = "Charles") ## End(Not run) getType Relationship Types Description Get the type of a relationship object or all relationship types in the graph. Usage getType(object) Arguments object A relationship or graph object. Value A character vector. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") createRel(bob, "WORKS_WITH", charles) rel = createRel(alice, "KNOWS", bob) getType(rel) getType(graph) ## End(Not run) 34 getUniqueNode getUniqueNode Retrieve Nodes by Label and Property Description Retrieve a single node from the graph by specifying its label and unique key = value pair. Usage getUniqueNode(graph, .label, ...) Arguments graph A graph object. .label A character string. ... A named list. A key = value pair by which the node label is uniquely constrained. Value A node object. See Also getLabeledNodes Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) createNode(graph, "Person", name = "Alice") createNode(graph, "Person", name = "Bob") addConstraint(graph, "Person", "name") getUniqueNode(graph, "Person", name = "Alice") ## End(Not run) importSample importSample 35 Import Sample Datasets Description Populate the graph database with one of the sample datasets supplied with this package. Usage importSample(graph, data, input = TRUE) Arguments graph A graph object. data A character string. Datasets include "tweets", "dfw", "caltrain", and "movies". input A logical constant. If TRUE, require confirmation. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") importSample(graph, "tweets") ## End(Not run) incomingRels Retrieve Relationships from Nodes Description Retreive a list of incoming relationship objects from a node object, optionally filtering by relationship type. Usage incomingRels(node, ...) Arguments node A node object. ... A character vector. Value A list of relationship objects. 36 nodes See Also outgoingRels Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) createRel(charles, "KNOWS", david) createRel(alice, "WORKS_WITH", david) createRel(bob, "WORKS_WITH", david) createRel(bob, "WORKS_WITH", charles) incomingRels(david) incomingRels(charles, "WORKS_WITH") ## End(Not run) nodes Retrieve Nodes from Paths Description Retrieve all nodes from a path object. Usage nodes(path) Arguments path A path object. Value A list of node objects. See Also rels outgoingRels 37 Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") createRel(alice, "WORKS_WITH", bob) createRel(bob, "WORKS_WITH", charles) query = " MATCH p = (:Person {name:'Alice'})-[:WORKS_WITH*]->(:Person {name:'Charles'}) RETURN p " path = cypherToList(graph, query)[[1]]$p nodes(path) ## End(Not run) outgoingRels Retrieve Relationships from Nodes Description Retreive a list of outgoing relationship objects from a node object, optionally filtering by relationship type. Usage outgoingRels(node, ...) Arguments node A node object. ... A character vector. Value A list of relationship objects. See Also incomingRels 38 rels Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") createRel(alice, "KNOWS", bob) createRel(alice, "KNOWS", charles) createRel(charles, "KNOWS", david) createRel(alice, "WORKS_WITH", david) createRel(bob, "WORKS_WITH", david) createRel(bob, "WORKS_WITH", charles) outgoingRels(david) outgoingRels(alice, "WORKS_WITH") ## End(Not run) rels Retrieve Relationships from Paths Description Retrieve all relationships from a path object. Usage rels(path) Arguments path A path object. Value A list of relationship objects. See Also nodes RNeo4j 39 Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") createRel(alice, "WORKS_WITH", bob) createRel(bob, "WORKS_WITH", charles) query = " MATCH p = (:Person {name:'Alice'})-[:WORKS_WITH*]->(:Person {name:'Charles'}) RETURN p " path = cypherToList(graph, query)[[1]]$p rels(path) ## End(Not run) RNeo4j Neo4j Driver for R Description Neo4j, a graph database, allows users to store their data as a property graph. A graph consists of nodes that are connected by relationships; both nodes and relationships can have properties, or keyvalue pairs. RNeo4j is Neo4j’s R driver. It allows users to read and write data from and to Neo4j directly from their R environment by exposing an interface for interacting with nodes, relationships, paths, and more. Most notably, it allows users to retrieve Cypher query results as R data frames, where Cypher is Neo4j’s graph query language. Visit neo4j.com to learn more about Neo4j. For bug reports, please create a new GitHub issue. For support, please create a question on StackOverflow. Author(s) Nicole White 40 shortestPath shortestPath Shortest Paths and Weighted Shortest Paths Description Retrieve the shortest path between two nodes. Usage shortestPath(fromNode, relType, toNode, direction = "out", max_depth = 1, cost_property = character()) Arguments fromNode A node object. relType A character string. The relationship type to traverse. toNode A node object. direction A character string. The relationship direction to traverse; this can be either "in", "out", or "all". max_depth An integer. The maximum depth of the path. cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights. Value A path object. See Also allShortestPaths Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") charles = createNode(graph, "Person", name = "Charles") david = createNode(graph, "Person", name = "David") elaine = createNode(graph, "Person", name = "Elaine") r1 r2 r3 r4 = = = = createRel(alice, "KNOWS", bob, weight=1.5) createRel(bob, "KNOWS", charles, weight=2) createRel(bob, "KNOWS", david, weight=4) createRel(charles, "KNOWS", david, weight=1) startGraph 41 r5 = createRel(alice, "KNOWS", elaine, weight=2) r6 = createRel(elaine, "KNOWS", david, weight=2.5) # The default max_depth of 1 will not find a path. # There are no length-1 paths between alice and david. p = shortestPath(alice, "KNOWS", david) is.null(p) # Set the max_depth to 4. p = shortestPath(alice, "KNOWS", david, max_depth = 4) p$length n = nodes(p) sapply(n, function(x) x$name) # Setting the direction to "in" and traversing from alice to david will not find a path. p = shortestPath(alice, "KNOWS", david, direction = "in", max_depth = 4) is.null(p) # Setting the direction to "in" and traversing from david to alice will find a path. p = shortestPath(david, "KNOWS", alice, direction = "in", max_depth = 4) p$length n = nodes(p) sapply(n, function(x) x$name) # Find the weighted shortest path between Alice and David. p = shortestPath(alice, "KNOWS", david, cost_property="weight") p$length p$weight nodes(p) ## End(Not run) startGraph Connect to the Database Description Establish a connection to Neo4j. Usage startGraph(url, username = character(), password = character(), opts = list()) Arguments url username password opts A character string. A character string. If authentication is enabled, your username. A character string. If authentication is enabled, your password. A named list. Optional HTTP settings. 42 startNode Value A graph object. Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") graph = startGraph("http://localhost:7474/db/data/", username = "neo4j", password = "password") graph = startGraph("http://localhost:7474/db/data/", opts = list(timeout=3)) ## End(Not run) startNode Retrieve Nodes from Relationships or Paths Description Retrieve the start node from a relationship or path object. Usage startNode(object) Arguments object A relationship or path object. Value A node object. See Also endNode Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") updateProp 43 rel = createRel(alice, "WORKS_WITH", bob) startNode(rel) query = " MATCH p = (a:Person)-[:WORKS_WITH]->(b:Person) WHERE a.name = 'Alice' AND b.name = 'Bob' RETURN p " path = getSinglePath(graph, query) startNode(path) ## End(Not run) updateProp Update Node and Relationship Properties Description For a node or relationship object, update its properties. Existing properties can be overridden and new properties can be added. Usage updateProp(entity, ...) Arguments entity A node or relationship object. ... A named list. Property updates in the form key = value. Value A node or relationship object. See Also deleteProp Examples ## Not run: graph = startGraph("http://localhost:7474/db/data/") clear(graph) alice = createNode(graph, "Person", name = "Alice") bob = createNode(graph, "Person", name = "Bob") 44 updateProp alice = updateProp(alice, age = 24, eyes = "green") newProps = list(age = 25, eyes = "brown") bob = updateProp(bob, newProps) alice bob ## End(Not run) Index addConstraint, 3, 18, 22, 27 addIndex, 4, 19, 23 addLabel, 5, 20, 24 allDijkstra, 5, 17 allShortestPaths, 5, 7, 40 appendCypher, 8 incomingRels, 35, 37 browse, 10 rels, 36, 38 RNeo4j, 39 newTransaction (appendCypher), 8 nodes, 36, 38 outgoingRels, 36, 37 clear, 10 commit (appendCypher), 8 createNode, 11 createRel, 12 cypher, 13, 14 cypherToList, 13, 14, 26, 28–32 shortestPath, 7, 17, 40 startGraph, 41 startNode, 21, 42 transactions (appendCypher), 8 updateProp, 16, 43 delete, 15 deleteProp, 16, 43 dijkstra, 6, 17 dropConstraint, 3, 18, 22 dropIndex, 3, 4, 19, 23 dropLabel, 5, 20, 24 endNode, 20, 42 getConstraint, 3, 4, 18, 21 getID, 22 getIndex, 3, 4, 19, 23 getLabel, 5, 20, 24 getLabeledNodes, 25, 34 getNodes, 26, 30 getOrCreateNode, 27 getPaths, 28, 31 getRels, 29, 32 getSingleNode, 26, 30 getSinglePath, 28, 31 getSingleRel, 29, 32 getType, 33 getUniqueNode, 25, 34 importSample, 35 45
© Copyright 2026 Paperzz