public
Last active

Neo4j-Cypher-R

  • Download Gist
neo4R_example.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
# Requirements
#sudo apt-get install libcurl4-gnutls-dev # for RCurl on linux
#install.packages('RCurl')
#install.packages('RJSONIO')
 
library('RCurl')
library('RJSONIO')
 
query <- function(querystring) {
h = basicTextGatherer()
curlPerform(url="http://localhost:7474/db/data/ext/CypherPlugin/graphdb/execute_query",
postfields=paste('query',curlEscape(querystring), sep='='),
writefunction = h$update,
verbose = FALSE
)
result <- fromJSON(h$value())
 
data <- data.frame(t(sapply(result$data, unlist)))
names(data) <- result.json$columns
data
}
 
# EXAMPLE
# =======
 
# Cypher query
q <- "
START root = (0)
MATCH (root)-[:CommentType]->(comments)<-[:HasType]-(comment)-[:PartOf]->(submission)-[:PartOf]->(subreddit)
RETURN comment.ups, comment.downs, submission.ups, submission.downs, subreddit.label, submission.created, comment.created
"
 
data <- query(q)
head(data)
top_subreddits <- data.frame(table(data$subreddit.label))
top_subreddits[order(top_subreddits$Freq, decreasing=T),]
 
# OUTPUT:
 
submission.ups submission.created comment.downs subreddit.label comment.ups submission.downs
1 33 1307643497 0 TheoryOfReddit 5 9
2 6375 1307723661 0 worldnews 6 4599
3 7 1307832288 1 IAmA 3 4
4 7 1307749193 0 semanticweb 1 3
5 84 1307979471 0 worldnews 1 29
6 19 1308051068 0 belgium 5 4
comment.created
1 1307691304
2 1307744384
3 1307908788
4 1307910667
5 1308049417
6 1308058498
Var1 Freq
25 TrueReddit 9
4 AskSocialScience 7
11 IAmA 7
5 belgium 6
9 Foodforthought 6
15 politics 6
26 videos 5
2 AskReddit 4
17 Python 4
20 semanticweb 4
16 programming 3
22 statistics 3
27 worldnews 3
14 pics 2
18 reddit.com 2
19 Scholar 2
23 Terraria 2
24 TheoryOfReddit 2
1 academicpublishing 1
3 askscience 1
6 bestof 1
7 buildapc 1
8 Drugs 1
10 greed 1
12 MensRights 1
13 PhilosophyofScience 1
21 sociology 1
28 WTF 1

Tried this with just a MATCH n RETURN n query

Getting:
Error in query(q) : object 'result.json' not found? Did you encounter this issue?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.