Skip to content

Commit

Permalink
Fix bug where single reason causes the app to crash. Closes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscpritchard committed May 5, 2024
1 parent 075aec1 commit 9b9ae58
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 215 deletions.
243 changes: 28 additions & 215 deletions R/PRISMA_flowdiagram.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,7 @@ PRISMA_flowdiagram <- function( #nolint
} else {
cond_citation <- ""
}
if (is.data.frame(other_excluded) == TRUE) {
other_excluded_data <- paste0(
":",
paste(
paste(
"\n",
other_excluded[, 1],
" (n = ", other_excluded[, 2], ")",
sep = ""
),
collapse = ""
)
)
} else {
other_excluded_data <- paste0(
"\n", "(n = ", other_excluded, ")"
)
}
other_excluded_data <- PRISMA_format_reasons_(other_excluded)
# labels
other_identified_label <- paste0(
"Records identified from:",
Expand Down Expand Up @@ -319,36 +302,13 @@ PRISMA_flowdiagram <- function( #nolint
cond_newreports <- ""
}
if (detail_databases == TRUE) {
db_specific_data_nr <- paste(
paste(
"\n",
database_specific_results[, 1],
" (n = ", database_specific_results[, 2], ")",
sep = ""
),
collapse = ""
)
db_specific_data <- paste0(
":",
db_specific_data_nr
)
db_specific_data <- PRISMA_format_reasons_(database_specific_results)
} else {
db_specific_data <- ""
db_specific_data_nr <- ""
}
if (detail_registers == TRUE) {
reg_specific_data_nr <- paste(
paste(
"\n", register_specific_results[, 1],
" (n = ", register_specific_results[, 2], ")",
sep = ""
),
collapse = ""
)
reg_specific_data <- paste0(
":",
reg_specific_data_nr
)
reg_specific_data <- PRISMA_format_reasons_(register_specific_results)
} else {
reg_specific_data <- ""
reg_specific_data_nr <- ""
Expand All @@ -369,22 +329,7 @@ PRISMA_flowdiagram <- function( #nolint
} else {
cond_register <- paste0("", reg_specific_data_nr)
}
if (any(!grepl("\\D", dbr_excluded)) == FALSE) {
dbr_excluded_data <- paste0(
":",
paste(
paste(
"\n",
dbr_excluded[, 1],
" (n = ", dbr_excluded[, 2], ")",
sep = ""
),
collapse = ""
)
)
} else {
dbr_excluded_data <- paste0("\n", "(n = ", dbr_excluded, ")")
}
dbr_excluded_data <- PRISMA_format_reasons_(dbr_excluded)
if (is.na(duplicates) == FALSE) {
cond_duplicates <- paste0(
stringr::str_wrap(
Expand Down Expand Up @@ -1249,85 +1194,19 @@ PRISMA_data <- function(data) { #nolint
]$n
)
)
database_specific_results <- data.frame(
reason = gsub(
",.*$",
"",
unlist(
strsplit(
as.character(
data[
grep(
"database_specific_results",
data[, 1]
),
]$n
),
split = "; "
)
)
),
n = scales::comma(
PRISMA_format_number_( #nolint
gsub(
".*?,(.*)",
"\\1",
unlist(
strsplit(
as.character(
data[
grep(
"database_specific_results",
data[, 1]
),
]$n
),
split = "; "
)
)
)
)
)
database_specific_results <- PRISMA_parse_reasons_(data[
grep(
"database_specific_results",
data[, 1]
),
]$n
)
register_specific_results <- data.frame(
reason = gsub(
",.*$",
"",
unlist(
strsplit(
as.character(
data[
grep(
"register_specific_results",
data[, 1]
),
]$n
),
split = "; "
)
)
),
n = scales::comma(
PRISMA_format_number_( #nolint
gsub(
".*?,(.*)",
"\\1",
unlist(
strsplit(
as.character(
data[
grep(
"register_specific_results",
data[, 1]
),
]$n
),
split = "; "
)
)
)
)
)
register_specific_results <- PRISMA_parse_reasons_(data[
grep(
"register_specific_results",
data[, 1]
),
]$n
)
website_results <- scales::comma(
PRISMA_format_number_( #nolint
Expand Down Expand Up @@ -1459,45 +1338,12 @@ PRISMA_data <- function(data) { #nolint
]$n
)
)
dbr_excluded <- data.frame(
reason = gsub(
",.*$",
"",
unlist(
strsplit(
as.character(
data[
grep(
"dbr_excluded",
data[, 1]
),
]$n
),
split = "; "
)
)
),
n = scales::comma(
PRISMA_format_number_( #nolint
gsub(
".*?,(.*)",
"\\1",
unlist(
strsplit(
as.character(
data[
grep(
"dbr_excluded",
data[, 1]
),
]$n
),
split = "; "
)
)
)
)
)
dbr_excluded <- PRISMA_parse_reasons_(data[
grep(
"dbr_excluded",
data[, 1]
),
]$n
)
other_assessed <- scales::comma(
PRISMA_format_number_( #nolint
Expand All @@ -1509,45 +1355,12 @@ PRISMA_data <- function(data) { #nolint
]$n
)
)
other_excluded <- data.frame(
reason = gsub(
",.*$",
"",
unlist(
strsplit(
as.character(
data[
grep(
"other_excluded",
data[, 1]
),
]$n
),
split = "; "
)
)
),
n = scales::comma(
PRISMA_format_number_( #nolint
gsub(
".*?,(.*)",
"\\1",
unlist(
strsplit(
as.character(
data[
grep(
"other_excluded",
data[, 1]
),
]$n
),
split = "; "
)
)
)
)
)
other_excluded <- PRISMA_parse_reasons_(data[
grep(
"other_excluded",
data[, 1]
),
]$n
)
new_studies <- scales::comma(
PRISMA_format_number_( #nolint
Expand Down
73 changes: 73 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,79 @@ PRISMA_format_number_ <- function(x) { #nolint
return(as.numeric(x))
}

#' Parse an exclusion reason into a data frame
#'
#' @description Parse an exclusion reason string and returns a dataframe
#' containing reasons and number
#' @param reasons the string to parse
#' @return a dataframe containing reasons and number applicable
#' @keywords internal
#'
PRISMA_parse_reasons_ <- function(reasons) { #nolint
reasons_out <- NA
if (grepl("[^0-9,]", as.character(reasons))) {
reasons_out <- data.frame(
reason = gsub(
",.*$",
"",
unlist(
strsplit(
as.character(reasons),
split = "(;)( )?"
)
)
),
n = scales::comma(
PRISMA_format_number_( #nolint
gsub(
".*?,([ 0-9,]*)|.*()",
"\\1",
unlist(
strsplit(
as.character(reasons),
split = "(;)( )?"
)
)
)
)
)
)
} else {
reasons_out <- data.frame(
reason = "",
n = scales::comma(
PRISMA_format_number_(as.character(reasons))
)
)
}
return(reasons_out)
}

#' Formats multiple exclusion reasons properly for printing
#'
#' @description Parse an exclusion reason dataframe from
#' [PRISMA2020::PRISMA_parse_reasons_()] and returns a properly formatted string
#' @param df the dataframe to parse
#' @return a string ready for printing
#' @keywords internal
#'
PRISMA_format_reasons_ <- function(df) { #nolint
out_string <- paste0(
":",
paste(
paste(
"\n",
df[, 1],
" (n = ", df[, 2], ")",
sep = ""
),
collapse = ""
)
)
return(out_string)
}


#' Generate a temporary SVG from a plot object
#'
#' @description Generate and save a temporary SVG from a plot object
Expand Down
19 changes: 19 additions & 0 deletions man/PRISMA_format_reasons_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9b9ae58

Please sign in to comment.