---
title: "Automating data entry"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Automating data entry}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Inserting explicit death subrecords

Sometimes individuals are defined with a date of birth but no death subrecord because there is no information about the death to warrant creating one. However, including an empty death subrecord is valuable in its own right to indicate the individual is dead. The `insert_explicit_death_subrecords()` function allows you to automatically insert these subrecords for individuals who would be too old to still be alive.

We illustrate with the following example of five individuals with various dates of birth (including a missing birth subrecord for person 5):

```{r}
library(tidyged)
library(tidyged.utils)

people <- gedcom(subm("Me")) |> 
  add_indi(qn = "Person 1") |> 
  add_indi_fact("birth", date = date_calendar(1900, 4, 4)) |> 
  add_indi(qn = "Person 2") |> 
  add_indi_fact("birth", date = date_calendar(1888)) |>
  add_indi(qn = "Person 3") |> 
  add_indi_fact("birth", date = date_calendar(1885, 6)) |>
  add_indi(qn = "Person 4") |> 
  add_indi_fact("birth", date = date_calendar(1910)) |>
  add_indi(qn = "Person 5") |> 
  add_indi_fact("census", date = date_calendar(1911), age = "42y")

dplyr::filter(people, tag == "DEAT")
```

If we were to assume a maximum age of 120 years (the default for the function) we would want a death subrecord for the first three individuals.  

```{r}
people_wds <- insert_explicit_death_subrecords(people, max_age = 120)

dplyr::filter(people_wds, tag == "DEAT")
```

If an individual has no date of birth defined, then the default action is to leave the record as is. However, setting the `guess` parameter to TRUE will cause the function to guess an age from associated facts if a date of birth does not exist.

```{r}
people_wds <- insert_explicit_death_subrecords(people, max_age = 120, guess = TRUE)

dplyr::filter(people_wds, tag == "DEAT")
```

We can now see an additional death subrecord for the fifth individual as the function has guessed the current age of the individual from the census event.

## Inserting explicit marriage types

The GEDCOM specification recommends that all relationship events have an explicit TYPE subrecord. If one is not given, marriage is assumed. The `insert_explicit_marr_types_all()` function inserts an explicit marriage TYPE if one is not given:

```{r}
dplyr::filter(sample555, record == "@F1@")

insert_explicit_marr_types_all(sample555) |> 
  dplyr::filter(record == "@F1@")
```

## Creating parent/ancestor placeholders

If you are visualising your tree and would like to automatically create ancestor records going back a certain number of generations, you can use the `add_ancestors()` function:

```{r}
people_with_anc <- add_ancestors(people, "@I1@", num_gen = 2)

tidyged::describe_records(people_with_anc, people_with_anc$record)
```

In the example above, you can see it has created parents and grandparents for Person 1, and has also created the Family Group records since they didn't already exist.

By default, the function will automatically assign the appropriate sex for new Individual records it creates:

```{r}
dplyr::filter(people_with_anc, record %in% paste0("@I", 6:11, "@"), tag == "SEX")
```

If the `inc_sex` parameter is FALSE or if the function cannot determine the appropriate sex, it will set it to be undetermined:

```{r}
add_ancestors(people, "@I1@", num_gen = 2, inc_sex = FALSE) |>
  dplyr::filter(record %in% paste0("@I", 6:11, "@"), tag == "SEX")
```

The `add_parents()` function is equivalent to running `add_ancestors()` with `num_gen = 1`.