neo4j-rstats / neo4r

A Modern and Flexible Neo4J Driver
https://neo4j-rstats.github.io/user-guide/
Other
106 stars 29 forks source link

Getting an error on creating with convert_to("igraph") has error on result #43

Closed dfgitn4j closed 5 years ago

dfgitn4j commented 5 years ago

I create a graph using the Neo4j movie graph database with "graph" as the output type and then use convert_to with "igraph"

> G<-"MATCH a=(p:Person {name: 'Tom Hanks'})-[r:ACTED_IN]->(m:Movie) RETURN a;" %>%
    call_neo4j(con, type = "graph") %>% 
    convert_to("igraph")

I get the following error referencing G:

> G
IGRAPH 35b7516 DN-- 13 12 -- 
+ attr: name (v/c), label (v/c), tagline (v/c), title (v/c), released
| (v/n), born (v/n), type (e/c), id (e/c), properties (e/x)
+ edges from 35b7516 (vertex names):
Error in if (is.na(no)) no <- len : argument is of length zero

Am thinking it might part of a different, but seemingly related error unnesting relationships when returned as a data.frame and not a graph:

> G<-"MATCH a=(p:Person {name: 'Tom Hanks'})-[r:ACTED_IN]->(m:Movie) RETURN a;" %>%
      call_neo4j(con, type = "graph")

> unnest_nodes(G$nodes)
# A tibble: 13 x 7
   id    label  tagline                         title      released  born name  
   <chr> <chr>  <chr>                           <chr>         <int> <int> <chr> 
  1 148   Movie  Houston, we have a problem.     Apollo 13      1995    NA NA    
  2 71    Person NA                              NA               NA  1956 Tom H…
  3 67    Movie  At odds in life... in love on-… You've Go…     1998    NA NA    
  4 170   Movie  Once in a lifetime you get a c… A League …     1992    NA NA    
  5 78    Movie  A story of love, lava and burn… Joe Versu…     1990    NA NA    
  6 85    Movie  In every life there comes a ti… That Thin…     1996    NA NA    
  7 111   Movie  Break The Codes                 The Da Vi…     2006    NA NA    
  8 105   Movie  Everything is connected         Cloud Atl…     2012    NA NA    
  9 158   Movie  At the edge of the world, his … Cast Away      2000    NA NA    
 10 130   Movie  Walk a mile you'll never forge… The Green…     1999    NA NA    
 11 73    Movie  What if someone you never met,… Sleepless…     1993    NA NA    
 12 169   Movie  This Holiday Season… Believe    The Polar…     2004    NA NA    
 13 167   Movie  A stiff drink. A little mascar… Charlie W…     2007    NA NA 

> unnest_relationships(G$relationships)
Error: Can't coerce element 1 from a list to a character
ColinFay commented 5 years ago

This bug seems to be due to the fact that the properties returned by the query don't have the same length everywhere:

library(neo4r)
con <- neo4j_api$new(url = "http://localhost:7474", 
                     user = "neo4j", password = "neo4j")

res <- "MATCH a=(p:Person {name: 'Tom Hanks'})-[r:ACTED_IN]->(m:Movie) RETURN a;" %>%
  call_neo4j(con, type = "graph") 

res$nodes %>% 
  unnest_nodes() 
#> # A tibble: 13 x 7
#>    id    label  tagline                    title     released  born name  
#>    <chr> <chr>  <chr>                      <chr>        <int> <int> <chr> 
#>  1 13313 Movie  A stiff drink. A little m… Charlie …     2007    NA <NA>  
#>  2 13225 Person <NA>                       <NA>            NA  1956 Tom H…
#>  3 13316 Movie  Once in a lifetime you ge… A League…     1992    NA <NA>  
#>  4 13315 Movie  This Holiday Season… Beli… The Pola…     2004    NA <NA>  
#>  5 13304 Movie  At the edge of the world,… Cast Away     2000    NA <NA>  
#>  6 13298 Movie  Houston, we have a proble… Apollo 13     1995    NA <NA>  
#>  7 13284 Movie  Walk a mile you'll never … The Gree…     1999    NA <NA>  
#>  8 13265 Movie  Break The Codes            The Da V…     2006    NA <NA>  
#>  9 13259 Movie  Everything is connected    Cloud At…     2012    NA <NA>  
#> 10 13239 Movie  In every life there comes… That Thi…     1996    NA <NA>  
#> 11 13232 Movie  A story of love, lava and… Joe Vers…     1990    NA <NA>  
#> 12 13227 Movie  What if someone you never… Sleeples…     1993    NA <NA>  
#> 13 13221 Movie  At odds in life... in lov… You've G…     1998    NA <NA>

# Doesn't work
res$relationships %>% 
  unnest_relationships()
#> Error: Can't coerce element 1 from a list to a character

library(tidyverse)
# Line 3 is a <list [6]>, while all other are <list [1]>
tidyr::unnest(res$relationships, properties)
#> # A tibble: 12 x 5
#>    id    type     startNode endNode properties
#>    <chr> <chr>    <chr>     <chr>   <list>    
#>  1 18917 ACTED_IN 13225     13313   <list [1]>
#>  2 18923 ACTED_IN 13225     13316   <list [1]>
#>  3 18921 ACTED_IN 13225     13315   <list [6]>
#>  4 18902 ACTED_IN 13225     13304   <list [1]>
#>  5 18891 ACTED_IN 13225     13298   <list [1]>
#>  6 18871 ACTED_IN 13225     13284   <list [1]>
#>  7 18824 ACTED_IN 13225     13265   <list [1]>
#>  8 18815 ACTED_IN 13225     13259   <list [4]>
#>  9 18777 ACTED_IN 13225     13239   <list [1]>
#> 10 18765 ACTED_IN 13225     13232   <list [1]>
#> 11 18758 ACTED_IN 13225     13227   <list [1]>
#> 12 18751 ACTED_IN 13225     13221   <list [1]>

# works
tidyr::unnest(res$relationships, properties) %>%
  tidyr::unnest() %>%
  dplyr::mutate(properties = as.character(properties))
#> # A tibble: 20 x 5
#>    id    type     startNode endNode properties         
#>    <chr> <chr>    <chr>     <chr>   <chr>              
#>  1 18917 ACTED_IN 13225     13313   Rep. Charlie Wilson
#>  2 18923 ACTED_IN 13225     13316   Jimmy Dugan        
#>  3 18921 ACTED_IN 13225     13315   Hero Boy           
#>  4 18921 ACTED_IN 13225     13315   Father             
#>  5 18921 ACTED_IN 13225     13315   Conductor          
#>  6 18921 ACTED_IN 13225     13315   Hobo               
#>  7 18921 ACTED_IN 13225     13315   Scrooge            
#>  8 18921 ACTED_IN 13225     13315   Santa Claus        
#>  9 18902 ACTED_IN 13225     13304   Chuck Noland       
#> 10 18891 ACTED_IN 13225     13298   Jim Lovell         
#> 11 18871 ACTED_IN 13225     13284   Paul Edgecomb      
#> 12 18824 ACTED_IN 13225     13265   Dr. Robert Langdon 
#> 13 18815 ACTED_IN 13225     13259   Zachry             
#> 14 18815 ACTED_IN 13225     13259   Dr. Henry Goose    
#> 15 18815 ACTED_IN 13225     13259   Isaac Sachs        
#> 16 18815 ACTED_IN 13225     13259   Dermot Hoggins     
#> 17 18777 ACTED_IN 13225     13239   Mr. White          
#> 18 18765 ACTED_IN 13225     13232   Joe Banks          
#> 19 18758 ACTED_IN 13225     13227   Sam Baldwin        
#> 20 18751 ACTED_IN 13225     13221   Joe Fox

Created on 2019-01-30 by the reprex package (v0.2.1)

I should make the convert and unnest more smart.

ColinFay commented 5 years ago

Hey @dfgitn4j

So the issue with the unnesting should be solved by #d14b45db commit.

As far as I can tell the igraph error is linked to the print method of igraph (how igraph prints its object to the console), not to the creation of the object itself. It seems to be due to the fact that the graph results contains NA at some point.

library(neo4r)
con <- neo4j_api$new(url = "http://localhost:7474", 
                     user = "neo4j", password = "neo4j")

res <- "MATCH a=(p:Person {name: 'Tom Hanks'})-[r:ACTED_IN]->(m:Movie) RETURN a;" %>%
  call_neo4j(con, type = "graph") 

res$nodes %>% 
  unnest_nodes() 
#> # A tibble: 13 x 7
#>    id    label  tagline                    title      released  born name  
#>    <chr> <chr>  <chr>                      <chr>         <int> <int> <chr> 
#>  1 13313 Movie  A stiff drink. A little m… Charlie W…     2007    NA <NA>  
#>  2 13225 Person <NA>                       <NA>             NA  1956 Tom H…
#>  3 13316 Movie  Once in a lifetime you ge… A League …     1992    NA <NA>  
#>  4 13315 Movie  This Holiday Season… Beli… The Polar…     2004    NA <NA>  
#>  5 13304 Movie  At the edge of the world,… Cast Away      2000    NA <NA>  
#>  6 13298 Movie  Houston, we have a proble… Apollo 13      1995    NA <NA>  
#>  7 13284 Movie  Walk a mile you'll never … The Green…     1999    NA <NA>  
#>  8 13265 Movie  Break The Codes            The Da Vi…     2006    NA <NA>  
#>  9 13259 Movie  Everything is connected    Cloud Atl…     2012    NA <NA>  
#> 10 13239 Movie  In every life there comes… That Thin…     1996    NA <NA>  
#> 11 13232 Movie  A story of love, lava and… Joe Versu…     1990    NA <NA>  
#> 12 13227 Movie  What if someone you never… Sleepless…     1993    NA <NA>  
#> 13 13221 Movie  At odds in life... in lov… You've Go…     1998    NA <NA>

# Now works
res$relationships %>% 
  unnest_relationships()
#> # A tibble: 20 x 5
#>    id    type     startNode endNode properties         
#>    <chr> <chr>    <chr>     <chr>   <chr>              
#>  1 18917 ACTED_IN 13225     13313   Rep. Charlie Wilson
#>  2 18923 ACTED_IN 13225     13316   Jimmy Dugan        
#>  3 18921 ACTED_IN 13225     13315   Hero Boy           
#>  4 18921 ACTED_IN 13225     13315   Father             
#>  5 18921 ACTED_IN 13225     13315   Conductor          
#>  6 18921 ACTED_IN 13225     13315   Hobo               
#>  7 18921 ACTED_IN 13225     13315   Scrooge            
#>  8 18921 ACTED_IN 13225     13315   Santa Claus        
#>  9 18902 ACTED_IN 13225     13304   Chuck Noland       
#> 10 18891 ACTED_IN 13225     13298   Jim Lovell         
#> 11 18871 ACTED_IN 13225     13284   Paul Edgecomb      
#> 12 18824 ACTED_IN 13225     13265   Dr. Robert Langdon 
#> 13 18815 ACTED_IN 13225     13259   Zachry             
#> 14 18815 ACTED_IN 13225     13259   Dr. Henry Goose    
#> 15 18815 ACTED_IN 13225     13259   Isaac Sachs        
#> 16 18815 ACTED_IN 13225     13259   Dermot Hoggins     
#> 17 18777 ACTED_IN 13225     13239   Mr. White          
#> 18 18765 ACTED_IN 13225     13232   Joe Banks          
#> 19 18758 ACTED_IN 13225     13227   Sam Baldwin        
#> 20 18751 ACTED_IN 13225     13221   Joe Fox

library(neo4r)
con <- neo4j_api$new(url = "http://localhost:7474", 
                     user = "neo4j", password = "neo4j")

x <-"MATCH a=(p:Person {name: 'Tom Hanks'})-[r:ACTED_IN]->(m:Movie) RETURN a;" %>%
  call_neo4j(con, type = "graph") %>% 
  convert_to("igraph")
plot(x)

class(x)
#> [1] "igraph"

Created on 2019-02-11 by the reprex package (v0.2.1)