Testing {neo4r} - neo4j-rstats/neo4r GitHub Wiki

Building software is a hard task, especially one that will be used in a variety of contexts. As much as we tried to make {neo4r} robust, there might still be some bugs and weird behaviors when you try to run the package in specific cases that we might not have foreseen. That's why we need your help testing the package to make it better in the future. This page will suggest a scenario for testing the package in your own environment.

Please try the dev version from GitHub (in order to not get into bugs that might already have been fixed :)

The data below are here as an example — please do use your own queries and data so that we can identify potential issues.

Connect

{neo4r} currently only works with the http(s) endpoint (bolt support should be added during 2019).

Create a connection object:

library(neo4r)
con <- neo4j_api$new(
  url = "****",
  user = "****", 
  password = "****"
  )

Request your API:

con$ping()
con$get_version()
con$get_constraints()
con$get_labels()
con$get_relationships()
con$get_schema()

Requesting

You can write your cypher query as a character vector, and send it with call_neo4j

library(magrittr)

'MATCH (r:record) -[:WAS_RECORDED] -> (b:Band) where b.formed = 1991 RETURN *;' %>%
  call_neo4j(con)

The output should be a list of one or more data.frame(s), with the number of elements returned being the number of element specifies in the RETURN statement. For example this should return a list of three:

library(magrittr)

'MATCH (r:record) -[w:WAS_RECORDED] -> (b:Band) where b.formed = 1991 RETURN r, w, b;' %>%
  call_neo4j(con)

Calling with type = graph should return a list of two data.frames :

res <- 'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 5;' %>%
  call_neo4j(con, type = "graph")

You can then unnest the nodes, relationship, or all result with

unnest_nodes(res$nodes)
unnest_relationships(res$relationships)
unnest_graph(res)

You can also extract only the nodes or only the relationships with the extract_* functions

'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 5;' %>%
  call_neo4j(con, type = "graph") %>% 
  extract_nodes()

Sending data

read_cypher() reads a cypher file in R:

read_cypher("data-raw/create.cypher")

This file can be send to the server with send_cypher()

send_cypher("data-raw/constraints.cypher", con)

You can also send a csv from an online source with load_csv()

on_load_query <- 'MERGE (a:artist { name: csvLine.artist})
MERGE (al:album {name: csvLine.album_name})
MERGE (a) -[:has_recorded] -> (al)  
RETURN a AS artists, al AS albums;'
# Send the csv 
load_csv(url = "https://raw.githubusercontent.com/ThinkR-open/datasets/master/tracks.csv", 
         con = con, header = TRUE, periodic_commit = 50, 
         as = "csvLine", on_load = on_load_query)

Converting and plotting

graph results can be turned into igraph objects and plotted:

'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 5;' %>%
  call_neo4j(con, type = "graph") %>%
  convert_to("igraph")
'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 5;' %>%
  call_neo4j(con, type = "graph") %>% 
  convert_to("igraph") %>% 
  plot()
library(ggraph)
'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 20;' %>%
  call_neo4j(con, type = "graph") %>% 
  convert_to("igraph") %>%
  ggraph() + 
  geom_node_label(aes(label = name)) +
  geom_edge_link() + 
  theme_graph()
network <- 'MATCH p=()-[r:WAS_RECORDED]->() RETURN p LIMIT 20;' %>%
  call_neo4j(con, type = "graph") %>% 
  convert_to("visNetwork")
visNetwork::visNetwork(network$nodes, network$relationships)

Reporting issues

Please don't send an email to the package maintainer, but open an issue on the GitHub repo: https://github.com/neo4j-rstats/neo4r/issues. It's the only way we can keep track of what's happening and that we can collaborate on solving the bugs you might have encounter.

What's a perfect issue report?

The more we know, the better: please do provide us your sessionInfo(), the version of Neo4J you're using, and if possible a reproducible example — in other words, an example we can copy and paste in our session and reproduce the bug. Using the {reprex} package (https://github.com/tidyverse/reprex) will definitely help.

Including the error message and / or the output of your call is important. If you're reporting a strange behavor, feel free to give us more information about what you would have expected here.

And thanks again for helping us making {neo4r} a better piece of software ;)