By Nicole White, Data Scientist | December 27, 2014 RNeo4j is Neo4j’s R driver – it allows you to quickly and easily interact with a Neo4j database from your R environment. Some recent updates to RNeo4j include:
- My contributions
- Functionality for retrieiving and handling paths
- Additional sample datasets
- Community contributions
- Open the Neo4j browser in RStudio
- Set custom HTTP options
Several functions have been added for retrieving and manipulating paths. These include:
library(RNeo4j) neo4j = startGraph("http://localhost:7474/db/data/") alice = createNode(neo4j, "User", name = "Alice") bob = createNode(neo4j, "User", name = "Bob") charles = createNode(neo4j, "User", name = "Charles") david = createNode(neo4j, "User", name = "David") elaine = createNode(neo4j, "User", name = "Elaine") r1 = createRel(alice, "LIKES", bob, weight = 1) r2 = createRel(bob, "LIKES", charles, weight = 2) r3 = createRel(bob, "LIKES", david, weight = 3) r4 = createRel(charles, "LIKES", david, weight = 4) r5 = createRel(alice, "LIKES", elaine, weight = 5) r6 = createRel(elaine, "LIKES", david, weight = 6)
getPaths allows you to retrieve a list of path objects with a Cypher query. The following query will find all paths (to a maximum depth of four) that traverse the relationship type
LIKESbetween Alice and David:
query = " MATCH p = (u1:User)-[:LIKES*..4]->(u2:User) WHERE u1.name = 'Alice' AND u2.name = 'David' RETURN p " p = getPaths(neo4j, query)
##  3
nodesnodes extracts the node objects from a path object. Because
pathsis a list of path objects, we need to
lapplythrough it and apply the function
nodesto each path in the list:
n = lapply(p, nodes)
nis a list of lists. If we wanted to view the names of all the people on each path, for example, we can again use the apply family of functions. As we saw earlier when using
length, three paths were found. The following displays the names of the nodes on each of the paths:
lapply(n, function(x) sapply(x, `[[`, 'name'))
## [] ##  "Alice" "Bob" "Charles" "David" ## ## [] ##  "Alice" "Bob" "David" ## ## [] ##  "Alice" "Elaine" "David"
relsrels, similarly to
nodes, extracts the relationship objects from a path object. Recall that each relationship has a
r = lapply(p, rels) lapply(r, function(x) sapply(x, `[[`, 'weight'))
## [] ##  1 2 4 ## ## [] ##  1 3 ## ## [] ##  5 6
shortestPath, allShortestPathsshortestPath and allShortestPaths find a single shortest path or all shortest paths between two node objects, respectively.
p = shortestPath(alice, "LIKES", david, max_depth = 4) sapply(nodes(p), `[[`, 'name')
##  "Alice" "Bob" "David"
p = allShortestPaths(alice, "LIKES", david, max_depth = 4) n = lapply(p, nodes) lapply(n, function(x) sapply(x, `[[`, 'name'))
## [] ##  "Alice" "Bob" "David" ## ## [] ##  "Alice" "Elaine" "David"
allShortestPathsfound both of the these paths because they tie for the shortest path (they’re both length-two paths).
shortestPath, on the other hand, returned just one of these paths arbitrarily.
Additional Sample DatasetsIf you want to quickly begin exploring the capabilities of RNeo4j but don’t have any datasets, you can import one of the sample datasets shipped with this package with importSample. This will import the selected dataset into Neo4j. There are four datasets, ranging from travel to entertainment to social. These include:
importSample, you will have to answer a prompt confirming it is okay to wipe your Neo4j database and import the selected dataset. You can get a decent overview of what’s been imported into Neo4j with
summary, which will show you what is related and how:
## This To That ## 1 Tweet USING Source ## 2 Tweet MENTIONS User ## 3 Tweet RETWEETS Tweet ## 4 User POSTS Tweet ## 5 Tweet CONTAINS Link ## 6 Tweet REPLY_TO Tweet ## 7 Hashtag TAGS Tweet
query = " MATCH (tweet:Tweet)-[:MENTIONS]->(user:User) RETURN user.screen_name AS user, COUNT(tweet) AS mentions ORDER BY mentions DESC LIMIT 5 " cypher(neo4j, query)
## user mentions ## 1 neo4j 28 ## 2 ikwattro 8 ## 3 _nicolemargaret 7 ## 4 rvanbruggen 7 ## 5 Linkurious 6
Open the Neo4j Browser in RStudioA pull request by Kenneth Darrell makes it so you can open the Neo4j browser in RStudio‘s viewer pane with browse. I use this functionality all the time now, as I usually want to do a quick check to see if my data was imported correctly. Recall that the Twitter dataset is currently in Neo4j, as we imported it earlier. Often, I’ll open the Neo4j browser and run the query…
MATCH n RETURN n LIMIT 50
Set Custom HTTP OptionsA pull request by Mark Needham makes it so you can set custom HTTP options. In particular, this is useful for setting the HTTP timeout. These options are set instartGraph:
neo4j = startGraph("http://localhost:7474/db/data/", opts = list(timeout=2))
query = " MATCH p = ()-[*..5]-() RETURN LENGTH(p) " test = try(cypher(neo4j, query)) cat(test)
## Error in function (type, msg, asError = TRUE) : ## Operation timed out after 2000 milliseconds with 327680 bytes received
Feel free to make your own contributions by submitting pull requests to the RNeo4j GitHub repository.
Want to learn more about graph databases? Click below to get your free copy of O’Reilly’s Graph Databases ebook and discover how to use graph technologies for your application today.
Download My Ebook
About the Author
Nicole White, Data Scientist
Nicole White grew up in Kansas City, Missouri and then spent four years at LSU in Baton Rouge, Louisiana where she got a degree in economics with a minor in mathematics. She then went to the University of Texas at Austin where she got her masters degree in analytics, and it was during this time that she found Neo4j and began playing around with it. When she’s not graphing all the things, she spends her time playing card games and board games.
From the CEO
Have a Graph Question?
Reach out and connect with the Neo4j staff.Stackoverflow
Share your Graph Story?
Email us: firstname.lastname@example.org