---
title: "Workflow"
author: "Michael Rustler"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Workflow}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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


## Read Endnote XML

```{r echo=TRUE}
library(kwb.endnote)

## Path to ENDNOTE XML 
endnote_xml <-  kwb.endnote::default_xml()
endnote_list <- kwb.endnote::create_endnote_list(endnote_xml)
```

The imported Endnote XML library is: `r basename(endnote_xml)`

## Check Import 

### Use Case 1: Unclean/Clean Versions

```{r echo=TRUE}
## Original Endnote data
refs_org <- kwb.endnote::create_references_df(endnote_list)

## "Cleaned" Endnote data
refs_clean <- kwb.endnote::clean_references_df(endnote_list)

## Check both for differences
diffs_df <- kwb.endnote::check_for_differences(refs_org, refs_clean)

diffs_df %>%
  dplyr::filter(.data$key != "abstract") %>% 
  head() %>% 
  knitr::kable()
  
```


### Use Case 2: Two Different Endnote Files 

```{r echo=TRUE}

en_old_xml <- kwb.endnote::extdata_file("2020-05-25_KWB-documents.xml")
en_new_xml <- kwb.endnote::extdata_file("2020-06-17_KWB-documents.xml")

en_old_list <- kwb.endnote::create_endnote_list(en_old_xml)
en_new_list <- kwb.endnote::create_endnote_list(en_new_xml)

en_old_df <- kwb.endnote::create_references_df(en_old_list)
en_new_df <- kwb.endnote::create_references_df(en_new_list)

en_diffs_df <- kwb.endnote::check_for_differences(en_old_df, en_new_df)

en_diffs_df %>% 
  dplyr::filter(.data$key != "abstract") %>% 
  dplyr::arrange(dplyr::desc(.data$rec_number), .data$key) %>% 
  knitr::kable()

n_new_entries <- length(unique(en_diffs_df$rec_number))
  
```

In total `r n_new_entries` new entries where created in `r basename(en_new_xml)`
compared to `r basename(en_old_xml)` which contain `r nrow(en_diffs_df)` new 
values.




### Use Case 3: Get Hints For 'Author Names'

```{r echo=TRUE}

multi_cols_list <- kwb.endnote::tidy_multi_cols_list(refs_clean)

names(multi_cols_list)


hints_author_names <- tibble::tibble(author = unique(multi_cols_list$author$value)) %>%
  dplyr::mutate(hints = kwb.endnote::give_hints_author_names(.data$author)) %>% 
  dplyr::filter(stringr::str_detect(.data$hints, pattern = "^fix_")) %>% 
  dplyr::arrange(.data$author) 


hints_author_names 

```
