Skip to content

Instantly share code, notes, and snippets.

@retrography
Forked from mhermans/neo4R_example.R
Last active August 29, 2015 14:00
Show Gist options
  • Save retrography/fdbf658116ddb7fa7c51 to your computer and use it in GitHub Desktop.
Save retrography/fdbf658116ddb7fa7c51 to your computer and use it in GitHub Desktop.
Fetches graph data directly from Neo4j REST API into R. Useful for R igraph users.
# 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/cypher",
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$columns
data
}
# EXAMPLE
# =======
# Cypher Query:
q <- "match (o:Organization)-[r]-(p:Person) return o.name,o.location,p.account,p.name,p.email limit 20"
data <-query(q)
head(data,20)
# Output:
# o.name o.location p.account p.name p.email
# 1 PerfectLine Estonia kritik
# 2 Sappho OSS London UK andrewheald
# 3 The 88 NYC aface1
# 4 The 88 NYC xbilldozer
# 5 The 88 NYC chadyj
# 6 The 88 NYC benmanns Benjamin Manns benmanns@gmail.net
# 7 simplabs Munich, Germany marcoow
# 8 Everyday Hero Brisbane, Australia soloman1124
# 9 Everyday Hero Brisbane, Australia orodio
# 10 Everyday Hero Brisbane, Australia justinhennessy
# 11 Everyday Hero Brisbane, Australia coop Tim Cooper coop@latrobest.org
# 12 Everyday Hero Brisbane, Australia evilmarty Marty Zalega marty@zalega.co
# 13 Sorenson Media Salt Lake City, UT & San Diego, CA bcarlson
# 14 Sorenson Media Salt Lake City, UT & San Diego, CA elmomalmo
# 15 Sorenson Media Salt Lake City, UT & San Diego, CA enthooz
# 16 3scale Barcelona, Spain and Sunnyvale, USA solso
# 17 3scale Barcelona, Spain and Sunnyvale, USA MarkCheshire
# 18 3scale Barcelona, Spain and Sunnyvale, USA rhoml
# 19 3scale Barcelona, Spain and Sunnyvale, USA mikz Michal Cichra
# 20 3scale Barcelona, Spain and Sunnyvale, USA njyx
@retrography
Copy link
Author

_Caution:_ This script runs into trouble if the query response is in a non-tabular format or includes missing values. Neo4j does not support null-valued properties and any property with a null value is removed from the database. In other words running SET node.property = NULL elicits the same result as running REMOVE node.property, which means it deletes the property. Neo4j's REST API responds to queries in JSON, which is schema-less as well, and does not represent missing properties in any way. When converting the JSON output to a tabular format, this results in rows with variable number of columns. In case your database contains missing values, either replace them with symbolic values or convert them on the fly within your query. If none of these is an option, you need to use a tabular format like CSV for data interchange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment