## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options("tibble.print_min" = 5L, "tibble.print_max" = 5L)
library(cohortBuilder)

## -----------------------------------------------------------------------------
librarian_source <- set_source(
  as.tblist(librarian)
)

librarian_cohort <- librarian_source |>
  cohort(
    step(
      filter(
        "discrete",
        id = "author", dataset = "books",
        variable = "author", value = "Dan Brown"
      ),
      filter(
        "discrete",
        id = "program", dataset = "borrowers",
        variable = "program", value = "premium", keep_na = FALSE
      )
    ),
    step(
      filter(
        "range",
        id = "copies", dataset = "books",
        variable = "copies", range = c(-Inf, 5L)
      )
    ),
    run_flow = TRUE
  )

## -----------------------------------------------------------------------------
librarian_cohort |>
  update_filter(
    step_id = 1L, filter_id = "author", value = c("Dan Brown", "Khaled Hosseini")
  )

sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
librarian_cohort |>
  add_filter(
    filter(
      "date_range",
      id = "issue_date", dataset = "issues",
      variable = "date", range = c(as.Date("2010-01-01"), Inf)
    ),
    step_id = 2L
  )

sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
librarian_cohort |>
  rm_filter(step_id = 2L, filter_id = "copies")

sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
run(librarian_cohort, min_step_id = 2L)

get_data(librarian_cohort)

## -----------------------------------------------------------------------------
librarian_cohort |>
  rm_step(step_id = 1L)

sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
librarian_cohort |>
  add_step(
    step(
      filter(
        "discrete",
        id = "author", dataset = "books",
        variable = "author", value = "Dan Brown"
      ),
      filter(
        "discrete",
        id = "program", dataset = "borrowers",
        variable = "program", value = "premium", keep_na = FALSE
      )
    )
  )

sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
code(librarian_cohort, include_methods = NULL)

new_source <- set_source(
  as.tblist(librarian),
  source_code = quote({
    source <- list()
    source$dtconn <- as.tblist(librarian)
  })
)

update_source(librarian_cohort, new_source)
sum_up(librarian_cohort)
code(librarian_cohort, include_methods = NULL)

## -----------------------------------------------------------------------------
update_source(librarian_cohort, new_source, keep_steps = FALSE)
sum_up(librarian_cohort)

## -----------------------------------------------------------------------------
new_source <- set_source(
  as.tblist(librarian)
)
empty_cohort <- cohort()
update_source(empty_cohort, new_source)
code(empty_cohort, include_methods = NULL)

## -----------------------------------------------------------------------------
source_one <- set_source(
  as.tblist(librarian)
) |>
  add_step(
    step(
      filter(
        "discrete",
        id = "author", dataset = "books",
        variable = "author", value = "Dan Brown"
      ),
      filter(
        "discrete",
        id = "program", dataset = "borrowers",
        variable = "program", value = "premium", keep_na = FALSE
      )
    )
  )

source_two <- set_source(
  as.tblist(librarian)
) |>
  add_step(
    step(
      filter(
        "range",
        id = "copies", dataset = "books",
        variable = "copies", range = c(-Inf, 5L)
      )
    )
  )

my_cohort <- cohort(source_one)
sum_up(my_cohort)

update_source(my_cohort, source_two)
sum_up(my_cohort)

## -----------------------------------------------------------------------------
librarian_source <- set_source(as.tblist(librarian))

fast_cohort <- librarian_source |>
  cohort(
    filter(
      "discrete",
      id = "author", dataset = "books",
      variable = "author", value = "Dan Brown"
    ),
    compute_stats = FALSE,
    run_flow = TRUE
  )

get_data(fast_cohort)$books

## -----------------------------------------------------------------------------
all_genres <- sort(collapse::funique(librarian$books$genre))

propagated_cohort <- librarian_source |>
  cohort(
    step(
      filter(
        "discrete",
        id = "author", dataset = "books",
        variable = "author", value = "Dan Brown"
      )
    ),
    step(
      filter(
        "discrete",
        id = "genre", dataset = "books",
        variable = "genre", domain = all_genres
      )
    ),
    propagate_domains = "data",
    run_flow = TRUE
  )

# Full domain vs. domain after step 1 narrowing
all_genres
propagated_cohort$get_step("2")$filters[["genre"]]@domain

