From 9b9ae580d427d31bbfd4e262bffb051f5193556c Mon Sep 17 00:00:00 2001 From: Chris Pritchard Date: Sun, 5 May 2024 22:10:37 +0100 Subject: [PATCH] Fix bug where single reason causes the app to crash. Closes #52 --- R/PRISMA_flowdiagram.R | 243 ++++------------------------------ R/utils.R | 73 ++++++++++ man/PRISMA_format_reasons_.Rd | 19 +++ man/PRISMA_parse_reasons_.Rd | 19 +++ 4 files changed, 139 insertions(+), 215 deletions(-) create mode 100644 man/PRISMA_format_reasons_.Rd create mode 100644 man/PRISMA_parse_reasons_.Rd diff --git a/R/PRISMA_flowdiagram.R b/R/PRISMA_flowdiagram.R index 2bd1e75..4dc095f 100755 --- a/R/PRISMA_flowdiagram.R +++ b/R/PRISMA_flowdiagram.R @@ -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:", @@ -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 <- "" @@ -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( @@ -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 @@ -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 @@ -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 diff --git a/R/utils.R b/R/utils.R index e549aa6..e138773 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 diff --git a/man/PRISMA_format_reasons_.Rd b/man/PRISMA_format_reasons_.Rd new file mode 100644 index 0000000..a57df0a --- /dev/null +++ b/man/PRISMA_format_reasons_.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{PRISMA_format_reasons_} +\alias{PRISMA_format_reasons_} +\title{Formats multiple exclusion reasons properly for printing} +\usage{ +PRISMA_format_reasons_(df) +} +\arguments{ +\item{df}{the dataframe to parse} +} +\value{ +a string ready for printing +} +\description{ +Parse an exclusion reason dataframe from +\code{\link[=PRISMA_parse_reasons_]{PRISMA_parse_reasons_()}} and returns a properly formatted string +} +\keyword{internal} diff --git a/man/PRISMA_parse_reasons_.Rd b/man/PRISMA_parse_reasons_.Rd new file mode 100644 index 0000000..fe60539 --- /dev/null +++ b/man/PRISMA_parse_reasons_.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{PRISMA_parse_reasons_} +\alias{PRISMA_parse_reasons_} +\title{Parse an exclusion reason into a data frame} +\usage{ +PRISMA_parse_reasons_(reasons) +} +\arguments{ +\item{reasons}{the string to parse} +} +\value{ +a dataframe containing reasons and number applicable +} +\description{ +Parse an exclusion reason string and returns a dataframe +containing reasons and number +} +\keyword{internal}