## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
# library(lbugr)
# con <- lb_connection(":memory:")

## ----eval=FALSE---------------------------------------------------------------
# # Create a node table for users with various data types
# lb_execute(con, paste("CREATE NODE TABLE User(userID UUID, name STRING,",
#                         "age INT64, is_active BOOL, created_at TIMESTAMP,",
#                         "last_login DATE, notes STRING[],",
#                         "PRIMARY KEY (userID))"))
# 
# # Create a node table for products
# lb_execute(con, "CREATE NODE TABLE Product(productID INT64, name STRING, PRIMARY KEY (productID))")
# 
# # Create a relationship table for user purchases
# lb_execute(con, "CREATE REL TABLE Buys(FROM User TO Product, purchase_date DATE)")

## ----eval=FALSE---------------------------------------------------------------
# library(jsonlite)
# # Create data frames for nodes and relationships
# users <- data.frame(
#   userID = c("a1b2c3d4-e5f6-7890-1234-567890abcdef", "b2c3d4e5-f6a7-8901-2345-67890abcdef0"),
#   name = c("Alice", "Bob"),
#   age = c(35, 45),
#   is_active = c(TRUE, FALSE),
#   created_at = as.POSIXct(c("2023-01-15 10:30:00", "2022-11-20 14:00:00")),
#   last_login = as.Date(c("2023-10-25", "2023-09-30")),
#   stringsAsFactors = FALSE
# )
# # LIST types should be formatted as JSON strings
# users$notes <- c(toJSON(c("note1", "note2")), toJSON("note3"))
# 
# products <- data.frame(
#   productID = c(101, 102),
#   name = c("Laptop", "Mouse")
# )
# 
# buys <- data.frame(
#   from_user = c("a1b2c3d4-e5f6-7890-1234-567890abcdef", "b2c3d4e5-f6a7-8901-2345-67890abcdef0"),
#   to_product = c(101, 102),
#   purchase_date = as.Date(c("2023-02-20", "2023-03-15"))
# )
# 
# # Load data into Ladybug
# lb_copy_from_df(con, users, "User")
# lb_copy_from_df(con, products, "Product")
# lb_copy_from_df(con, buys, "Buys")

## ----eval=FALSE---------------------------------------------------------------
# # Create a CSV file in the project's root directory
# csv_filename <- "products.csv"
# write.csv(data.frame(productID = c(103, 104), name = c("Keyboard", "Monitor")),
#           csv_filename, row.names = FALSE)
# 
# # Load data from the CSV file using just the filename
# lb_copy_from_csv(con, csv_filename, "Product")
# 
# # Clean up the created file
# unlink(csv_filename)

## ----eval=FALSE---------------------------------------------------------------
# # Execute a query to get users and their purchases
# query_result <- lb_execute(con, "MATCH (u:User)-[b:Buys]->(p:Product) RETURN u.name, p.name, b.purchase_date")

## ----eval=FALSE---------------------------------------------------------------
# # Convert to a data frame
# df_result <- as.data.frame(query_result)
# print(df_result)
# #>      u.name p.name purchase_date
# #> 1     Alice Laptop    2023-02-20
# #> 2       Bob  Mouse    2023-03-15
# 
# # Convert to a tibble
# library(tibble)
# query_result <- lb_execute(con, "MATCH (u:User)-[b:Buys]->(p:Product) RETURN u.name, p.name, b.purchase_date")
# tibble_result <- as_tibble(query_result)
# print(tibble_result)
# #> # A tibble: 2 x 3
# #>   u.name p.name purchase_date
# #>   <chr>  <chr>  <date>
# #> 1 Alice  Laptop 2023-02-20
# #> 2 Bob    Mouse  2023-03-15

## ----eval=FALSE---------------------------------------------------------------
# query_result <- lb_execute(con, "MATCH (u:User)-[b:Buys]->(p:Product) RETURN u.name, p.name, b.purchase_date")
# 
# result <- lb_get_all(query_result)
# print(result)
# #> [[1]]
# #> [[1]]$u.name
# #> [1] "Alice"
# #> ...
# 
# # only fetch 1. result
# query_result <- lb_execute(con, "MATCH (u:User)-[b:Buys]->(p:Product) RETURN u.name, p.name, b.purchase_date")
# 
# result <- lb_get_n(query_result, 1)
# print(result)
# #> [[1]]
# #> [[1]]$u.name
# #> [1] "Alice"
# #> ...
# 
# #Fetch next result
# result <- lb_get_next(query_result)
# print(result)
# #> $u.name
# #> [1] "Bob"
# #> ...

## ----eval=FALSE---------------------------------------------------------------
# # Execute a query that returns a graph structure
# graph_query_result <- lb_execute(con, "MATCH (u:User)-[b:Buys]->(p:Product) RETURN u, p, b")
# igraph_obj <- as_igraph(graph_query_result)
# print(igraph_obj)
# #> IGRAPH UN-- 3 2 --
# #> + attr: name (v/c)
# #> + edges (vertex names):
# #> [1] Alice->Laptop Bob  ->Mouse
# 
# plot(igraph_obj,
#      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: User Purchases")
# 
# # Convert to a tidygraph object
# tidygraph_obj <- as_tidygraph(graph_query_result)
# print(tidygraph_obj)
# #> # A tbl_graph: 3 nodes and 2 edges
# #> #
# #> # A directed acyclic simple graph with 3 nodes and 2 edges
# #> #
# #> # Node Data: 3 x 1 (active)
# #>   name
# #>   <chr>
# #> 1 Alice
# #> 2 Bob
# #> 3 Laptop
# #> #
# #> # Edge Data: 2 x 3
# #>    from    to purchase_date
# #>   <int> <int> <date>
# #> 1     1     3 2023-02-20
# #> 2     2     4 2023-03-15
# 
# ggraph::ggraph(tidygraph_obj, layout = "kk") +
#   ggraph::geom_edge_link(color = "#9ca3af", arrow = grid::arrow(angle = 30, length = grid::unit(3, "mm"))) +
#   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))

