Skip to content

Commit 7f671dd

Browse files
committed
Trouble at Mill
1 parent c7d3424 commit 7f671dd

File tree

5 files changed

+142
-7
lines changed

5 files changed

+142
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export(get_citation)
88
export(get_refs)
99
export(reconstruct_abstract)
1010
export(repair_refs)
11+
export(runBibfixApp)
1112
export(scan_file)
1213
export(search_openAlex)
1314
importFrom(MESS,cumsumbinning)

R/build_ris.R

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

R/repair_refs.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ repair_refs <- function(refs,
1919
repair_incomplete = TRUE,
2020
source = 'lens',
2121
title_search = TRUE,
22-
token = 'NFxMqRTdXCQRq3uDl8NgduSAXcEf5DAqLIBAPALydHloF0n1n2Xi'){
22+
token = 'NFxMqRTdXCQRq3uDl8NgduSAXcEf5DAqLIBAPALydHloF0n1n2Xi')
23+
{
2324

2425
#enter polite pool
2526
suppressMessages(invisible(capture.output(openalex::openalex_polite("[email protected]"))))
@@ -414,7 +415,7 @@ refs<-refs |>
414415
}
415416
)
416417

417-
}
418+
}}
418419
####End DOI section####
419420

420421
####Title section####
@@ -582,10 +583,10 @@ refs<-refs |>
582583
}}}
583584

584585
####End title section####
585-
}
586586
} else {
587587
title_refs <- NULL
588-
}
588+
}
589+
589590
#bind doi lookup and title lookup table with original values not missing information
590591
result <- dplyr::bind_rows(non_missing, doi_refs, title_refs)
591592

@@ -606,11 +607,10 @@ refs<-refs |>
606607
output <- intermediate %>%
607608
dplyr::group_by(intID) %>%
608609
dplyr::summarise_all(coalesce_by_column)
609-
610+
610611
return(output)
611-
612-
}
613612
}
613+
614614

615615

616616
coalesce_by_column <- function(df) {

R/runBibfixApp.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' Run Shiny App
2+
#'
3+
#' This function launches a Shiny application from the specified directory in an R package.
4+
#'
5+
#' @param app_name bibfix
6+
#' @return Launches the Shiny application.
7+
#' @export
8+
runBibfixApp <- function(app_name="bibfix") {
9+
# Ensure the Shiny package is installed
10+
if (!requireNamespace("shiny", quietly = TRUE)) {
11+
stop("The 'shiny' package is required but not installed.")
12+
}
13+
14+
# Define the app directory
15+
app_dir <- here::here("inst/shiny-examples/bibfix")
16+
17+
# Check if the app directory exists
18+
if (app_dir == "" || !dir.exists(app_dir)) {
19+
stop("App directory not found. Ensure the app is located in inst/shiny/", app_name)
20+
}
21+
22+
# Launch the app
23+
shiny::runApp(app_dir, display.mode = "normal")
24+
}

man/runBibfixApp.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)