R/matchPubs.R

Defines functions matchPubs

#' @title Validate CrossRef Publications
#' @description Given a \code{data.frame()} of publication information, and a set of publications, allow interactive selection of "best matches" for publications.
#' @param cross A list generated by the function \code{scrape_dois()}.
#' @param pubs A \code{data.frame()} from the users (unformatted) publication data.
#' @param results The match values returned from \code{scrape_dois()}.
#' @export
matchPubs <- function(cross, pubs, results, savefile = NA, restore = TRUE) {
  if (!is.na(savefile) & restore == TRUE) {
    bestMatch <- try(readRDS(savefile), silent = TRUE)
    if ('try-error' %in% class(bestMatch)) {
      warning("Could not find savefile to restore from.  Generating new save file.")
      bestMatch <- list()
    } else {
      bestMatch <- bestMatch@publications
    }
  } else {
    bestMatch <- list()
  }

  for (i in (length(bestMatch) + 1):length(cross)) {
    cat('\n')
    bestMatch[[i]] <- findMatch(pubs[i],
                                results[[i]],
                                cross[[i]])

    if (class(bestMatch[[i]]) == "publication") {
      if (!is.na(savefile)) {
        output <- new('publications',
                      publications = bestMatch)
        saveRDS(output, savefile)
      }
    } else {
      if (bestMatch[[i]] == "break") {
        bestMatch[[i]] <- NULL
        break
      }
    }
  }

  output <- new('publications',
                publications = bestMatch)

  return(output)
}
NeotomaDB/bulkUploader documentation built on June 9, 2025, 10:49 p.m.