|
| 1 | +#' Build RIS files from other sources |
| 2 | +#' |
| 3 | +#' @description Builds an RIS file based on a basic input of |
| 4 | +#' fields corresponding to a minimum information for |
| 5 | +#' deduplication and record identification from external |
| 6 | +#' API sources (e.g. CrossRef). |
| 7 | +#' @param input A dataframe object containing bibliographic |
| 8 | +#' data. Each item is an independent line in the |
| 9 | +#' dataframe. The dataframe must contain |
| 10 | +#' columns named as follows: 'authors', 'year', 'title', |
| 11 | +#' 'source', 'volume', 'issue', 'start_page', 'end_page', |
| 12 | +#' and 'doi'. |
| 13 | +#' @param save Logical argument to specify whether the output |
| 14 | +#' file should be saved as an .ris file. |
| 15 | +#' @param filename Optional name of the output file if |
| 16 | +#' save = TRUE. Default is 'export'. |
| 17 | +#' @param path Path to which file should be saved. |
| 18 | +#' @return An RIS formatted text file saved to the desired |
| 19 | +#' path. |
| 20 | +#' @export |
| 21 | +#' @examples |
| 22 | +#' \dontrun{ |
| 23 | +#' data <- read.csv('inst/extdata/data.csv') |
| 24 | +#' ris <- build_ris(data, save=TRUE) |
| 25 | +#' } |
| 26 | +build_ris <- function(data, |
| 27 | + save = FALSE, |
| 28 | + filename = 'export', |
| 29 | + path = NULL){ |
| 30 | + |
| 31 | + if (is.data.frame(data) == FALSE){ |
| 32 | + stop('Please ensure the input object is a data frame') |
| 33 | + } |
| 34 | + |
| 35 | + #add missing columns where necessary (ensures build below works even if fields not present) |
| 36 | + if(is.null(data$source_type) == TRUE){data$source_type <- NA} |
| 37 | + if(is.null(data$author) == TRUE){data$author <- NA} |
| 38 | + if(is.null(data$year) == TRUE){data$year <- NA} |
| 39 | + if(is.null(data$title) == TRUE){data$title <- NA} |
| 40 | + if(is.null(data$journal) == TRUE){data$journal <- NA} |
| 41 | + if(is.null(data$volume) == TRUE){data$volume <- NA} |
| 42 | + if(is.null(data$issue) == TRUE){data$issue <- NA} |
| 43 | + if(is.null(data$start_page) == TRUE){data$start_page <- NA} |
| 44 | + if(is.null(data$end_page) == TRUE){data$end_page <- NA} |
| 45 | + if(is.null(data$abstract) == TRUE){data$abstract <- NA} |
| 46 | + if(is.null(data$doi) == TRUE){data$doi <- NA} |
| 47 | + if(is.null(data$publisher) == TRUE){data$publisher <- NA} |
| 48 | + if(is.null(data$url) == TRUE){data$url <- NA} |
| 49 | + if(is.null(data$notes) == TRUE){data$notes <- NA} |
| 50 | + if(is.null(data$M1) == TRUE){data$M1 <- NA} |
| 51 | + if(is.null(data$database) == TRUE){data$database <- NA} |
| 52 | + if(is.null(data$AN) == TRUE){data$AN <- NA} |
| 53 | + |
| 54 | + #replace NAs with '' |
| 55 | + data[is.na(data)==TRUE]='' |
| 56 | + |
| 57 | + #create RIS file |
| 58 | + ris <- paste(paste0('\n', |
| 59 | + 'TY - ', data$source_type, '\n', |
| 60 | + 'AU - ', data$author, '\n', |
| 61 | + 'TI - ', data$title, '\n', |
| 62 | + 'PY - ', data$year, '\n', |
| 63 | + 'AB - ', data$abstract, '\n', |
| 64 | + 'SP - ', data$start_page, '\n', |
| 65 | + 'EP - ', data$end_page, '\n', |
| 66 | + 'JF - ', data$journal, '\n', |
| 67 | + 'VL - ', data$volume, '\n', |
| 68 | + 'IS - ', data$issue, '\n', |
| 69 | + 'DO - ', data$doi, '\n', |
| 70 | + 'UR - ', data$url, '\n', |
| 71 | + 'PB - ', data$publisher, '\n', |
| 72 | + 'N1 - ', data$notes, '\n', |
| 73 | + 'M1 - ', data$M1, '\n', |
| 74 | + 'DB - ', data$DB, '\n', |
| 75 | + 'AN - ', data$AN, '\n', |
| 76 | + 'ER - '), |
| 77 | + collapse = '\n') |
| 78 | + |
| 79 | + #generate report if file exported as .ris |
| 80 | + if (save == TRUE){ |
| 81 | + write.table(ris, file = paste0(path, filename, '.ris'), row.names = FALSE, col.names = FALSE) |
| 82 | + if (is.null(path) == TRUE){ |
| 83 | + location <- 'your working directory' |
| 84 | + } else { |
| 85 | + location <- paste0(path) |
| 86 | + } |
| 87 | + report <- paste0('The output file "', filename, '.ris" has been saved to ', location) |
| 88 | + message(report) |
| 89 | + } |
| 90 | + |
| 91 | + return(ris) |
| 92 | + |
| 93 | +} |
0 commit comments