-
Notifications
You must be signed in to change notification settings - Fork 2
/
global.R
58 lines (48 loc) · 1.54 KB
/
global.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
conf <- config::get()
con <- neo4j_api$new(
url = conf$url,
user = conf$username,
password = conf$password
)
get_graph_components <- function(x) {
query <-
glue("MATCH (c:Company)-[r:invests_in]-(i:Investor)
WHERE c.name = '{x}' RETURN c, r, i;") %>%
call_neo4j(con, type = "graph")
nodes <-
unnest_nodes(query$nodes) %>%
mutate(
label = name,
color.background = if_else(value == "Company", "salmon", "lightblue")
) %>%
select(id, label, color.background)
rels <-
unnest_relationships(query$relationships) %>%
select(from = startNode, to = endNode) %>%
mutate(label = "")
## return
list(nodes = nodes, rels = rels)
}
remove_graph_components <- function(x, nn) {
others <- glue_collapse(nn, "','")
suppressMessages(
res <-
glue(
"WITH ['{others}'] as others
MATCH (c:Company {{name:'{x}'}})<-[r:invests_in]-(i:Investor)
OPTIONAL MATCH (i)-[:invests_in]->(o:Company)
WHERE o.name in others
WITH i, c, count(DISTINCT c) + count(DISTINCT o) as nods
WHERE nods = 1
RETURN i, c, nods;"
) %>%
call_neo4j(con, type = "graph")
)
if(is_empty(res)) {
res <-
glue("MATCH (c:Company) WHERE c.name = '{x}' RETURN c") %>%
call_neo4j(con, type = "graph")
}
# return
res
}