---
title: "Searchable Codebooks"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Searchable Codebooks}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  markdown: 
    wrap: 80
---

```{r, mesage=FALSE, warning=FALSE}
library(rfars)
library(dplyr)
library(stringr)
```

`rfars` comes pre-loaded with searchable codebooks, compiled from SAS format
files and the User Manuals (see: [FARS Analytical User's
Manual](https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813794) and
[CRSS Analytical User's
Manual](https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813796)).

Access the codebooks in RStudio with:

```{r, eval=F, echo=T}
View(rfars::fars_codebook)
View(rfars::gescrss_codebook)
```

It can be helpful to search the codebooks for certain concepts. The examples
below illustrate the content of the codebooks and how variables change over
time.

Here we search for the word alcohol in any of the codebook fields, including the
names of tables, various versions of variable names, the definition, all value
labels, and additional information from the User Manual if present.

```{r, results='asis'}
cb <- rfars::fars_codebook %>% filter(if_any(everything(), ~ str_detect(., "alcohol"))) 

distinct(cb, source, file, name_ncsa, name_rfars, label, Definition, `Additional Information`) %>%
  knitr::kable(format = "html")
```

The `drinking` variable changed in a very minor way:

```{r, results='asis'}
filter(cb, name_rfars == "drinking") %>%
  distinct(name_rfars, value_label, `2015`, `2016`, `2017`, `2018`, `2019`, `2020`, `2021`, `2022`, `2023`, `2024`) %>%
  knitr::kable(format = "html")
```

But the variable for driver distraction changed names in 2020:

```{r, results='asis'}
filter(cb, name_rfars %in% c("mdrdstrd", "drdistract")) %>%
  distinct(name_rfars, value_label, `2015`, `2016`, `2017`, `2018`, `2019`, `2020`, `2021`, `2022`, `2023`, `2024`) %>%
  arrange(value_label, name_rfars) %>%
  knitr::kable(format = "html")
```

As another example, the `per_typ` variable has undergone more substantial
changes over time.

```{r, results='asis'}
filter(rfars::fars_codebook, name_rfars == "per_typ") %>%
  distinct(name_rfars, value_label, `2015`, `2016`, `2017`, `2018`, `2019`, `2020`, `2021`, `2022`, `2023`, `2024`) %>%
  arrange(value_label, name_rfars) %>%
  knitr::kable(format = "html")
```

Users of rfars and FARS data in general are strongly encouraged to User Manuals
for further guidance.
