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

## -----------------------------------------------------------------------------
iris_source <- set_source(
  tblist(iris = iris),
  description = list(
    iris = list(
      dataset_ = describe("Edgar Anderson's measurements of iris flowers."),
      Species = describe("Iris species.", domain = c("setosa", "versicolor", "virginica"))
    )
  )
)

## -----------------------------------------------------------------------------
labelled_source <- set_source(
  tblist(iris = iris),
  description = list(
    iris = list(
      Species = describe("the species of iris", label = "Iris species")
    )
  )
) |>
  autofilter(attach_as = "meta")

species_filter <- purrr::detect(
  labelled_source$available_filters, ~ .x@id == "iris-Species"
)
species_filter@name

## -----------------------------------------------------------------------------
iris_cohort <- set_source(tblist(iris = iris)) |>
  autofilter(attach_as = "step") |>
  cohort()

sum_up(iris_cohort)

## -----------------------------------------------------------------------------
meta_source <- iris_source |>
  autofilter(attach_as = "meta")

length(meta_source$available_filters)

## -----------------------------------------------------------------------------
species_filter <- purrr::detect(
  meta_source$available_filters, ~ .x@id == "iris-Species"
)
species_filter@domain

## -----------------------------------------------------------------------------
result <- shape(meta_source)

# Dataset descriptions
result$datasets

# One filter entry
str(result$filters$`iris-Species`)

## -----------------------------------------------------------------------------
coh <- cohort(meta_source)
tool <- cb_tool_filters_meta(coh)
print(tool)

## ----eval = FALSE-------------------------------------------------------------
# library(ellmer)
# 
# source <- set_source(tblist(iris = iris)) |>
#   autofilter(attach_as = "meta")
# coh <- cohort(source)
# 
# chat <- chat_openai()
# chat |> cb_register_tools(coh)
# 
# chat$chat("Filter the data to setosa flowers with sepal length over 5")

