0

I'm trying to access my Neo4j data directly from R to do some network analysis

I have already read this: Use neo4j with R

So I tried using their code:

#install.packages('RCurl')
#install.packages('RJSONIO')

library('bitops')
library('RCurl')
library('RJSONIO')

query <- function(querystring) {
  h = basicTextGatherer()
  curlPerform(url="myhost:7474/db/data/ext/CypherPlugin/graphdb/execute_query",
    postfields=paste('query',curlEscape(querystring), sep='='),
    writefunction = h$update,
    verbose = FALSE
  )           
  result <- fromJSON(h$value())
  #print(result)
  data <- data.frame(t(sapply(result$data, unlist)))
  print(data)
  names(data) <- result$columns

}

q <-"start a = node(50) match a-->b RETURN b"
 data <- query(q)
 print(data)

However all I get is:

data frame with 0 columns and 1 rows + print(data) NULL

am I doing something wrong?

Community
  • 1
  • 1
AngelloMaggio
  • 504
  • 5
  • 16

2 Answers2

2

That function is meant for returning tabular data, whereas you are returning a node. Hence the data frame error. There are also several things wrong with that function; it cannot return 1-column data and it has no NULL handling. Consider using the RNeo4j driver here:

http://nicolewhite.github.io/RNeo4j/

library(RNeo4j)
graph = startGraph("http://localhost:7474/db/data/")
query = "MATCH (n:`layer_1_SB`)-[r]-> (m) WHERE m:layer_2_SB RETURN n.userid, m.userid"
data = cypher(graph, query)
write.table(data, file = "filename.file")
Nicole White
  • 7,720
  • 29
  • 31
1

So I fixed my problem. This is how the new code looks like:

#install.packages('RCurl')
#install.packages('RJSONIO')

library('bitops')
library('RCurl')
library('RJSONIO')

query <- function(querystring) {
  h = basicTextGatherer()
  curlPerform(url="myhost:7474/db/data/cypher",
    postfields=paste('query',curlEscape(querystring), sep='='),
    writefunction = h$update,
    verbose = FALSE
  )           
  result <- fromJSON(h$value())
  #print(result)
  data <- data.frame(t(sapply(result$data, unlist)))
  print(data)
  names(data) <- result$columns
  data

}

q <-"MATCH (n:`layer_1_SB`)-[r]-> (m) WHERE m:layer_2_SB RETURN n.userid, m.userid"
 data <- query(q)
 head(data)
 dim(data)
 names(data)
 write.table(data, file = "/home/angello-maggio/data2.dat", append=FALSE,quote=FALSE,sep=" ",eol="\n", na="NA", dec=".", row.names=FALSE)

As you can see if you're using neo4j 2.x you should use the path /db/data/cypher instead of /db/data/ext/CypherPlugin/graphdb/execute_query also I had forgotten to return data, and was returning names(data) instead.

Also nulls might happen if your query is wrong. Hope this helps someone who had my problem too!

AngelloMaggio
  • 504
  • 5
  • 16