Skip to content

Commit

Permalink
Update export.R
Browse files Browse the repository at this point in the history
Refactored export_ris function to enhance flexibility. The function now accepts a user-defined mapping of data frame columns to RIS fields. Replaced specific field parameters with a general 'user_mapping' parameter. Added user confirmation step before proceeding with the export.
  • Loading branch information
TNRiley authored May 24, 2024
1 parent b13adea commit da7e628
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,61 +54,61 @@ export_csv <- function(citations, filename = "citations.csv", separate = NULL, t
}


#' Export deduplicated citations to RIS file
#' Export data frame to RIS file
#'
#' This function saves deduplicated citations as a RIS file with sources, labels and strings
#' included as custom fields (if they were initially provided for any of the citations). Note that
#' This function saves a data frame as a RIS file with specified columns mapped to RIS fields. Note that
#' *existing files are overwritten without warning.*
#'
#' @param citations Dataframe with unique citations, resulting from `dedup_citations()`
#' @param citations Dataframe to be exported to RIS file
#' @param filename Name (and path) of file, should end in .ris
#' @param source_field Character. Which RIS field should cite_sources be saved to? NULL to exclude.
#' @param label_field Character. Which RIS field should cite_labels be saved to? NULL to exclude.
#' @param string_field Character. Which RIS field should cite_strings be saved to? NULL to exclude.
#' @param user_mapping List. Custom mapping of RIS fields to data frame columns. If NULL, a default mapping is used.
#' @export
#' @examples
#' if (interactive()) {
#' # Load example data from the package
#' examplecitations_path <- system.file("extdata", "examplecitations.rds", package = "CiteSource")
#' examplecitations <- readRDS(examplecitations_path)
#' dedup_results <- dedup_citations(examplecitations, merge_citations = TRUE)
#' export_ris(dedup_results$unique, "cite_sources.ris", string_field = NULL)
#' export_ris(dedup_results$unique, "cite_sources.ris", user_mapping = list("DB" = "cite_source_include", "C7" = "cite_label_include"))
#' }

export_ris <- function(citations, filename = "citations.ris", source_field = "DB", label_field = "C7", string_field = "C8") {
export_ris <- function(x, filename = "output.ris", user_mapping = NULL) {

if (tolower(tools::file_ext(filename)) != "ris") warning("Function saves a RIS file, so filename should (usually) end in .ris. For now, name is used as provided.")

if (!is.null(source_field) && source_field %in% names(citations)) {
citations <- citations %>% dplyr::rename(cite_source_include = .data[[source_field]])
}
# Default mapping of RIS fields to x columns
default_mapping <- list(
"DB" = "cite_source_include",
"C7" = "cite_label_include",
"C8" = "cite_string_include",
"C1" = "duplicate_id",
"C2" = "record_ids"
)

if (!is.null(label_field) && label_field %in% names(citations)) {
citations <- citations %>% dplyr::rename(cite_label_include = .data[[label_field]])
# If user_mapping is provided, override the default mapping
if (!is.null(user_mapping)) {
for (field in names(user_mapping)) {
default_mapping[[field]] <- user_mapping[[field]]
}
}

if (!is.null(string_field) && string_field %in% names(citations)) {
citations <- citations %>% dplyr::rename(cite_string_include = .data[[string_field]])
# Rename the x columns according to the final mapping
for (field in names(default_mapping)) {
if (default_mapping[[field]] %in% names(x)) {
x <- x %>% dplyr::rename(!!field := .data[[default_mapping[[field]]]])
}
}

select_cols <- c("source_type", "duplicate_id") %in% names(citations)
citations <- citations %>%
dplyr::select(c("source_type", "duplicate_id")[select_cols], dplyr::everything(), -tidyselect::any_of(c("cite_source", "cite_string", "cite_label", "record_id")))

synthesisr_codes <- dplyr::bind_rows(
tibble::tribble(
~code, ~field, ~ris_synthesisr,
source_field, "cite_source_include", TRUE,
string_field, "cite_string_include", TRUE,
label_field, "cite_label_include", TRUE,
"C1", "duplicate_id", TRUE,
"C2", "record_ids", TRUE
),
synthesisr_code_lookup %>% dplyr::filter(.data$ris_synthesisr)
) %>% dplyr::distinct(.data$code, .keep_all = TRUE) # Remove fields from synthesisr specification used for CiteSource metadata
# Print the final mapping and ask the user if they want to proceed
print(default_mapping)
proceed <- readline("Do you want to proceed with this mapping? (yes/no): ")

# Currently, write_refs does not accept tibbles, thus converted
write_refs(as.data.frame(citations), file = filename, tag_naming = synthesisr_codes)
if (tolower(proceed) == "yes") {
# Currently, write_refs does not accept tibbles, thus converted
write_refs(as.data.frame(x), file = filename)
} else {
message("Export cancelled. Please modify your user_mapping argument and try again.")
}
}


Expand Down

0 comments on commit da7e628

Please sign in to comment.