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

## ----setup--------------------------------------------------------------------
library("stratigraphr")

## ----example-data-------------------------------------------------------------
library("purrr")
library("dplyr")

dates <- data.frame(
  lab_id = c("ABC-001", "ABC-002", "ABC-003", "ABC-004", "ABC-005"),
  age = c(5050, 5000, 4950, 4900, 4850),
  error = c(30, 30, 30, 30, 30),
  phase = c("Phase 1", "Phase 1", "Phase 1", "Phase 2", "Phase 2")
)

## ----r-date-------------------------------------------------------------------
dates |>
  mutate(r_date = cql_r_date(lab_id, age, error)) |>
  pluck("r_date") |>
  cql()

## ----phase-model--------------------------------------------------------------
dates |>
  group_by(phase) |>
  summarise(cql = cql_phase(phase, cql_r_date(lab_id, age, error))) |>
  arrange(desc(phase)) |>
  summarise(cql = cql_sequence("Example Sequence", cql, boundaries = TRUE)) |>
  pluck("cql") |>
  cql() ->
  example_cql

example_cql

## ----write-oxcal--------------------------------------------------------------
oxcal_cql <- cql(
  cql_r_date("ABC-001", 9100, 30),
  cql_r_date("ABC-002", 9200, 30),
  cql_r_date("ABC-003", 9300, 30)
)

write_oxcal(oxcal_cql, "cql.oxcal")

## ----write-oxcal-cleanup, include=FALSE---------------------------------------
file.remove("cql.oxcal")

## ----oxcaar-setup, results=FALSE, message=FALSE-------------------------------
library("oxcAAR")
quickSetupOxcal(path = tempdir())

## ----oxcaar-execute, results=FALSE, message=FALSE-----------------------------
executeOxcalScript(oxcal_cql) |>
  readOxcalOutput() ->
  oxcal_output

## ----oxcaar-plot, fig.show="hold"---------------------------------------------
oxcal_parsed <- oxcAAR::parseOxcalOutput(oxcal_output)
plot(oxcal_parsed)
calcurve_plot(oxcal_parsed)

## ----oxcaar-bayes, eval=FALSE-------------------------------------------------
# # Not run: slow
# example_oxcal <- executeOxcalScript(example_cql)
# readOxcalOutput(example_oxcal) |>
#   parseOxcalOutput() |>
#   plot()

