Skip to content

Instantly share code, notes, and snippets.

@joelnitta
Last active May 27, 2021 01:56
Show Gist options
  • Save joelnitta/b5ce3f982a9b62e5b400a872765d9c9a to your computer and use it in GitHub Desktop.
Save joelnitta/b5ce3f982a9b62e5b400a872765d9c9a to your computer and use it in GitHub Desktop.
Filter a list of references in YAML format to those cited in an RMD file
# Filter a list of references in YAML format to those cited in an RMD file
#
# The YAML list should be exported from Zotero
# like this: file -> "export library" -> "Better CSL YAML"
#
# Exporting YAML from Zotero requires the Better Bibtex for Zotero extension
# to be installed https://retorque.re/zotero-better-bibtex/
library(tidyverse)
library(yaml)
library(assertr)
# Set path to RMD file
rmd_file <- "ms/manuscript.Rmd"
# Parse RMD file and extract citation keys
citations <-
readr::read_lines(rmd_file) %>%
stringr::str_split(" |;") %>%
unlist %>%
magrittr::extract(., stringr::str_detect(., "@")) %>%
stringr::str_remove_all("\\[|\\]|\\)|\\(|\\.$|,|\\{|\\}") %>%
magrittr::extract(., stringr::str_detect(., "^@|^-@")) %>%
stringr::str_remove_all("^@|^-@") %>%
unique %>%
sort %>%
tibble(key = .)
# Set path to YAML including all references exported from Zotero
yaml_file <- "data_raw/main_library.yaml"
# Read in YAML
ref_yaml <- yaml::read_yaml(yaml_file)
# Extract citation keys from YAML, filter to only those in the RMD
cite_keys <- map_chr(ref_yaml$references, "id") %>%
tibble(
key = .,
order = 1:length(.)
) %>%
inner_join(citations, by = "key") %>%
# Stop if any keys are missing
assert(not_na, everything())
# Filter YAML to only those citation keys in the RMD
ref_yaml_filtered <- list(references = ref_yaml$references[cite_keys$order])
# Write out the filtered YAML file
yaml::write_yaml(ref_yaml_filtered, file = "ms/references.yaml")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment