---
title: "cgmguru: Examples Gallery"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Examples Gallery}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 10, fig.height = 6, warning = FALSE, message = FALSE)
library(cgmguru)
library(iglu)
library(ggplot2)
library(dplyr)

# Use package root as working directory so we can reference ../examples
pkg_root <- normalizePath("..", winslash = "/", mustWork = TRUE)
examples_dir <- file.path(pkg_root, "/inst/examples")
stopifnot(dir.exists(examples_dir))
```

# Overview

This vignette renders and runs the example scripts provided in the package's `/inst/examples/` directory, so you can see inputs, outputs, and typical workflows in one HTML document.

If you are viewing this from RStudio Help, use the "Open in Browser" button for the best experience.

## Notes

- Code is executed by sourcing the example scripts directly from `/inst/examples/`.
- Some examples may take longer on large datasets.
- Figures may be produced where applicable.

---

## GRID

```{r ex-grid}
source(file.path(examples_dir, "grid.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Detect all events

```{r ex-detect-all-events}
source(file.path(examples_dir, "detect_all_events.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Detect hyperglycemic events

```{r ex-detect-hyper}
source(file.path(examples_dir, "detect_hyperglycemic_events.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Detect hypoglycemic events

```{r ex-detect-hypo}
source(file.path(examples_dir, "detect_hypoglycemic_events.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Excursion

```{r ex-excursion}
source(file.path(examples_dir, "excursion.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Local maxima

```{r ex-local-maxima}
source(file.path(examples_dir, "find_local_maxima.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Max after hours

```{r ex-max-after}
source(file.path(examples_dir, "find_max_after_hours.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Max before hours

```{r ex-max-before}
source(file.path(examples_dir, "find_max_before_hours.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Min after hours

```{r ex-min-after}
source(file.path(examples_dir, "find_min_after_hours.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Min before hours

```{r ex-min-before}
source(file.path(examples_dir, "find_min_before_hours.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Maxima + GRID combined

```{r ex-maxima-grid}
source(file.path(examples_dir, "maxima_grid.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Ordering utility

```{r ex-orderfast}
source(file.path(examples_dir, "orderfast.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Start finder

```{r ex-start-finder}
source(file.path(examples_dir, "start_finder.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

## Transform dataframe

```{r ex-transform-df}
source(file.path(examples_dir, "transform_df.R"), echo = TRUE, print.eval = TRUE, max.deparse.length = Inf)
```

---

## Function reference and help

For detailed function documentation with additional runnable `@examples`, see the package reference:

```{r, eval=FALSE}
help(package = "cgmguru")
```

You can also open the package vignettes index:

```{r, eval=FALSE}
browseVignettes("cgmguru")
```


