## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
# library(lbugr)
# library(igraph)
# 
# # Create a connection
# db_path <- tempfile()
# con <- lb_connection(db_path)
# 
# # Create schema for nodes and relationships
# lb_execute(con, paste("CREATE NODE TABLE Person(name STRING, age INT64,",
#                         "PRIMARY KEY (name))"))
# lb_execute(con, "CREATE REL TABLE Knows(FROM Person TO Person, since INT64)")
# 
# # Prepare data frames
# persons_data <- data.frame(
#   name = c("Alice", "Bob", "Carol"),
#   age = c(35, 45, 25)
# )
# 
# knows_data <- data.frame(
#   from_person = c("Alice", "Bob"),
#   to_person = c("Bob", "Carol"),
#   since = c(2010, 2015)
# )
# 
# # Load data into Ladybug
# lb_copy_from_df(con, persons_data, "Person")
# lb_copy_from_df(con, knows_data, "Knows")

## ----eval=FALSE---------------------------------------------------------------
# # Query to get all persons and their relationships
# graph_query_result <- lb_execute(con, paste("MATCH (p1:Person)-[k:Knows]->",
#                                               "(p2:Person) RETURN p1, p2, k"))
# 
# # Convert the Ladybug result to an igraph object
# igraph_graph <- as_igraph(graph_query_result)
# 
# # Print the igraph object summary
# print(igraph_graph)
# #> IGRAPH UN-- 3 2 --
# #> + attr: name (v/c), label (v/c), since (e/n)
# #> + edges (vertex names):
# #> [1] Alice->Bob  Bob  ->Carol
# 
# V(igraph_graph)$label <- igraph::V(igraph_graph)$name
# E(igraph_graph)$label <- "knows"
# plot(igraph_graph,
#      vertex.color = "#dc2626",
#      vertex.label.color = "#f3f4f6",
#      vertex.label.font = 2,
#      edge.color = "#9ca3af",
#      edge.arrow.size = 0.8,
#      edge.arrow.width = 0.5,
#      bg = "#030712",
#      main = "igraph Graph")

## ----eval=FALSE---------------------------------------------------------------
# # Convert the Ladybug result to a tidygraph object
# tidygraph_graph <- as_tidygraph(graph_query_result)
# 
# # Print the tidygraph object summary
# print(tidygraph_graph)
# #> # A tbl_graph: 3 nodes and 2 edges
# #> #
# #> # A directed acyclic simple graph with 3 nodes and 2 edges
# #> #
# #> # Node Data: 3 x 2 (active)
# #>   name    age
# #>   <chr> <dbl>
# #> 1 Alice    35
# #> 2 Bob      45
# #> 3 Carol    25
# #> #
# #> # Edge Data: 2 x 3
# #>    from    to since
# #>   <int> <int> <dbl>
# #> 1     1     2  2010
# #> 2     2     3  2015
# 
# ggraph::ggraph(tidygraph_graph, layout = "kk") +
#   ggraph::geom_edge_link(color = "#9ca3af", arrow = grid::arrow(angle = 30, length = grid::unit(3, "mm")), arrow.fill = "#9ca3af") +
#   ggraph::geom_node_point(color = "#dc2626", size = 8) +
#   ggraph::geom_node_text(ggplot2::aes(label = name), color = "#f3f4f6", size = 4, vjust = -1) +
#   ggplot2::theme_void() +
#   ggplot2::theme(plot.background = ggplot2::element_rect(fill = "#030712", color = NA))

## ----eval=FALSE---------------------------------------------------------------
# library(g6R)
# graph_query_result <- lb_execute(con, paste("MATCH (p1:Person)-[k:Knows]->",
#                                               "(p2:Person) RETURN p1, p2, k"))
# # Convert the Ladybug result to a g6R-compatible list
# igraph_graph <- as_igraph(graph_query_result)
# 
# g6 <- g6_igraph(igraph_graph) |>
#   g6_layout(d3_force_layout()) |>
#   g6_options(
#     animation = FALSE,
#     node = list(
#       style = list(
#         labelText = JS("(d) => d.name")
#       )
#     ),
#     edge = list(
#       style = list(
#         endArrow = TRUE,
#         labelText = JS("(d) => d.data.label")
#       )
#     )
#   ) |>
#   g6_behaviors(
#     zoom_canvas(),
#     collapse_expand(),
#     drag_canvas(),
#     drag_element()
#   ) |>
#   g6_plugins("toolbar")
# 
# 
# # Display the graph
# g6

