## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----load-starwars, eval = FALSE----------------------------------------------
# library(dplyr)
# library(mongolite)
# library(mdbplyr)
# 
# starwars_collection <- mongolite::mongo(
#   collection = "starwars",
#   db = "mdbplyr"
# )
# 
# starwars_collection$drop()
# starwars_collection$insert(dplyr::starwars)
# 
# starwars_tbl <- tbl_mongo(
#   starwars_collection,
#   schema = names(dplyr::starwars)
# )

## ----create-lazy-table, eval = FALSE------------------------------------------
# library(dplyr)
# library(mdbplyr)
# 
# starwars_collection <- mongolite::mongo(
#   collection = "starwars",
#   db = "mdbplyr"
# )
# 
# starwars_tbl <- tbl_mongo(starwars_collection) %>%
#   infer_schema()

## ----schema-explicit, eval = FALSE--------------------------------------------
# starwars_tbl <- tbl_mongo(
#   starwars_collection,
#   schema = c("name", "species", "height", "mass", "homeworld")
# )

## ----schema-infer, eval = FALSE-----------------------------------------------
# starwars_tbl <- tbl_mongo(starwars_collection) |>
#   infer_schema()

## ----schema-fields, eval = FALSE----------------------------------------------
# schema_fields(starwars_tbl)

## ----inspect, eval = FALSE----------------------------------------------------
# schema_fields(starwars_tbl)
# 
# starwars_tbl |>
#   filter(species == "Human", height > 180) |>
#   select(name, height, mass) |>
#   show_query()

## ----inspect-cursor, eval = FALSE---------------------------------------------
# iter <- starwars_tbl |>
#   filter(species == "Human", height > 180) |>
#   select(name, height, mass) |>
#   cursor()
# 
# iter$page(10)

## ----verb-filter, eval = FALSE------------------------------------------------
# starwars_tbl |>
#   filter(species == "Droid", height > 100) |>
#   collect()

## ----verb-select, eval = FALSE------------------------------------------------
# starwars_tbl |>
#   select(name, species, homeworld) |>
#   collect()

## ----verb-select-nested, eval = FALSE-----------------------------------------
# sensor_tbl |>
#   select(`message.timestamp`, `message.measurements`) |>
#   collect()

## ----verb-rename, eval = FALSE------------------------------------------------
# starwars_tbl |>
#   rename(character_name = name, planet = homeworld) |>
#   collect()

## ----verb-mutate, eval = FALSE------------------------------------------------
# starwars_tbl |>
#   mutate(height_m = height / 100, bmi_like = mass / (height_m * height_m)) |>
#   select(name, height, mass, height_m, bmi_like) |>
#   collect()

## ----verb-transmute, eval = FALSE---------------------------------------------
# starwars_tbl |>
#   transmute(name = name, height_m = height / 100) |>
#   collect()

## ----verb-arrange, eval = FALSE-----------------------------------------------
# starwars_tbl |>
#   arrange(desc(height), name) |>
#   select(name, height) |>
#   slice_head(n = 10) |>
#   collect()

## ----verb-group-by, eval = FALSE----------------------------------------------
# starwars_tbl |>
#   group_by(species)

## ----verb-summarise, eval = FALSE---------------------------------------------
# starwars_tbl |>
#   group_by(species) |>
#   summarise(
#     n = n(),
#     avg_height = mean(height),
#     max_mass = max(mass)
#   ) |>
#   arrange(desc(n)) |>
#   collect()

## ----verb-slice-head, eval = FALSE--------------------------------------------
# starwars_tbl |>
#   select(name, species) |>
#   slice_head(n = 5) |>
#   collect()

## ----verb-slice-tail, eval = FALSE--------------------------------------------
# starwars_tbl |>
#   select(name, species) |>
#   slice_tail(n = 5) |>
#   collect()

## ----verb-head, eval = FALSE--------------------------------------------------
# head(starwars_tbl, 5) |>
#   collect()

## ----verb-flatten-fields, eval = FALSE----------------------------------------
# sensor_tbl |>
#   select(`message.timestamp`, `message.measurements`) |>
#   flatten_fields() |>
#   collect()

## ----verb-flatten-fields-names, eval = FALSE----------------------------------
# sensor_tbl |>
#   flatten_fields(
#     `message.measurements`,
#     names_fn = function(x) gsub(".", "_", x, fixed = TRUE)
#   ) |>
#   collect()

## ----verb-unwind-array, eval = FALSE------------------------------------------
# orders_tbl |>
#   unwind_array(items) |>
#   collect()

## ----verb-unwind-array-flatten, eval = FALSE----------------------------------
# orders_tbl |>
#   unwind_array(items) |>
#   flatten_fields(items) |>
#   collect()

## ----new-server-version, eval = FALSE-----------------------------------------
# # Probed automatically from a live connection, or set it explicitly:
# starwars_tbl <- tbl_mongo(
#   starwars_collection,
#   schema = names(dplyr::starwars),
#   server_version = "7.0"
# )
# 
# mongo_server_version(starwars_tbl)

## ----new-group-computed, eval = FALSE-----------------------------------------
# starwars_tbl |>
#   group_by(height_band = floor(height / 50) * 50) |>
#   summarise(n = n(), avg_mass = mean(mass)) |>
#   arrange(height_band) |>
#   collect()

## ----new-summaries, eval = FALSE----------------------------------------------
# starwars_tbl |>
#   group_by(species) |>
#   summarise(
#     n = n(),
#     sd_height = sd(height),
#     var_mass = var(mass),
#     first_name = first(name),
#     distinct_worlds = n_distinct(homeworld)
#   ) |>
#   collect()

## ----new-median, eval = FALSE-------------------------------------------------
# starwars_tbl |>
#   group_by(species) |>
#   summarise(
#     median_height = median(height),
#     p90_mass = quantile(mass, probs = 0.9)
#   ) |>
#   collect()

## ----new-select-tidyselect, eval = FALSE--------------------------------------
# starwars_tbl |>
#   select(name, starts_with("home"), ends_with("_color")) |>
#   collect()
# 
# # Drop columns with negation, or select from a character vector:
# starwars_tbl |>
#   select(-ends_with("_color")) |>
#   collect()
# 
# cols <- c("name", "height", "mass")
# starwars_tbl |>
#   select(all_of(cols)) |>
#   collect()

## ----new-coalesce, eval = FALSE-----------------------------------------------
# starwars_tbl |>
#   mutate(world = coalesce(homeworld, "unknown")) |>
#   select(name, world) |>
#   collect()

## ----new-across-summarise, eval = FALSE---------------------------------------
# # One aggregate over several columns
# starwars_tbl |>
#   group_by(species) |>
#   summarise(across(c(height, mass), mean, na.rm = TRUE)) |>
#   collect()
# 
# # A named list of aggregates -> {.col}_{.fn} output names
# starwars_tbl |>
#   group_by(species) |>
#   summarise(across(c(height, mass), list(avg = mean, max = max))) |>
#   collect()

## ----new-across-mutate, eval = FALSE------------------------------------------
# # A lambda applied to each selected column, with custom output names
# starwars_tbl |>
#   mutate(across(c(height, mass), ~ .x / 100, .names = "{.col}_scaled")) |>
#   select(name, height_scaled, mass_scaled) |>
#   collect()

## ----new-window-rank, eval = FALSE--------------------------------------------
# starwars_tbl |>
#   group_by(species) |>
#   mutate(height_rank = min_rank(desc(height))) |>
#   select(name, species, height, height_rank) |>
#   collect()

## ----new-window-cumulative, eval = FALSE--------------------------------------
# starwars_tbl |>
#   arrange(height) |>
#   mutate(cumulative_mass = cumsum(mass)) |>
#   select(name, height, mass, cumulative_mass) |>
#   collect()

## ----new-window-offset, eval = FALSE------------------------------------------
# starwars_tbl |>
#   arrange(height) |>
#   mutate(
#     prev_height = lag(height),
#     next_height = lead(height, default = 0)
#   ) |>
#   select(name, height, prev_height, next_height) |>
#   collect()

## ----new-join-setup, eval = FALSE---------------------------------------------
# films_tbl <- tbl_mongo(
#   mongolite::mongo(collection = "films", db = "mdbplyr"),
#   schema = c("film_id", "title", "director")
# )

## ----new-join-inner, eval = FALSE---------------------------------------------
# appearances_tbl |>
#   inner_join(films_tbl, by = "film_id") |>
#   collect()

## ----new-join-semi, eval = FALSE----------------------------------------------
# appearances_tbl |>
#   semi_join(films_tbl, by = c("movie_id" = "film_id")) |>
#   collect()

## ----new-join-nested, eval = FALSE--------------------------------------------
# appearances_tbl |>
#   left_join(films_tbl, by = "film_id", unnest = FALSE) |>
#   collect()

