Skip to content

Commit

Permalink
Allow to pass BibTex as character to drop_name()
Browse files Browse the repository at this point in the history
This allows users to copy-paste BibTex from (e.g.) Google Scholar 

```r
drop_name("@Article{rungta2022geographic,
  title={Geographic Citation Gaps in NLP Research},
  author={Rungta, Mukund and Singh, Janvijay and Mohammad, Saif M and Yang, Diyi},
  journal={arXiv preprint arXiv:2210.14424},
  year={2022}
}")
```

In that context, it also deals with nucleic-acid#49 - while we should prob not edit bib-files here, it seems fair to add a workaround for this interactive use until bib2df addresses the issue?
  • Loading branch information
LukasWallrich committed Dec 11, 2022
1 parent 97d744f commit 2783015
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions R/drop_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param bib Accepts one of the following:
#' 1) A data.frame or tibble containing the columns YEAR, JOURNAL, AUTHOR, TITLE, BIBTEXKEY (all mandatory) and DOI, URL (optional).
#' 2) A file path to a bibliography file in BibTeX/BibLaTeX format (usually *.bib file).
#' 3) A string containing one or more BibTeX citations (must start with `@`)
#' @param cite_key If given, either a character string or a vector of strings are accepted.
#' Specifies the reference items within the bibliography for which visual citations should be created.
#' If no key is specified, a visual citation is created for ALL reference items within the bibliography.
Expand Down Expand Up @@ -142,6 +143,14 @@ drop_name <- function(bib, cite_key,
if (is.data.frame(bib)) {
bib_data <- bib
} else if (is.character(bib)) {
bib_file <- NULL #Track whether new file was created
if(substr(bib, 1, 1) == "@") {
bib <- gsub("(?<=\\w{1})\\=\\{", " \\= \\{", bib, perl = TRUE) # To deal with Google Scholar parsing issue - https://github.com/nucleic-acid/namedropR/issues/49
bib_file <- tempfile(fileext = ".bib")
writeLines(bib, bib_file)
writeLines(bib, "test.bib")
bib <- bib_file
}
if (file.exists(bib)) {
bib_data <- suppressMessages(suppressWarnings(bib2df::bib2df(file = bib)))
if ("YEAR" %in% colnames(bib_data)) {
Expand All @@ -150,6 +159,9 @@ drop_name <- function(bib, cite_key,
message("Years coerced to string format.")
}
}
if(!is.null(bib_file)) {
file.remove(bib_file)
}
message("Bibliography file successfully read.")
} else {
stop("BibTeX file not found. Check file path or pass a suitable data.frame/tibble to the function.")
Expand Down

0 comments on commit 2783015

Please sign in to comment.