pacotes <- c("dplyr","openxlsx","readxl","topsis") if(sum(as.numeric(!pacotes %in% installed.packages())) != 0){ instalador <- pacotes[!pacotes %in% installed.packages()] for(i in 1:length(instalador)) { install.packages(instalador, dependencies = T) break()} sapply(pacotes, require, character = T) } else { sapply(pacotes, require, character = T) } ' @title pmtcR #' #' @description Receives a standardized dataset and returns the ranking of the #' alternatives according to the user-defined criteria weight calculation method #' and the user-defined ranking method. #' #' @param df A dataset object #' @param weight_method Method for calculating weights or user-defined weights #' @param ranking_method Method for ranking the alternatives #' @param alpha Parameter alpha for the CoCoSo method (optional, used only for #' CoCoSo method) #' #' @return A dataframe containing the ranked alternatives and the ranking #' metrics according to the chosen method. #' #' @examples #' # Example usage: #' # df <- df4 #' # result <- pmtcR(df,ranking_method = "cocoso") #' #' @export #' @import dplyr #' @importFrom openxlsx read.xlsx #' @importFrom readxl read_excel #' @importFrom topsis topsis pmtcR <- function(df, weight_method = "decisor", ranking_method, alpha = 0.5) { alternatives <- df[3:nrow(df), 1] alternatives <- as.data.frame(alternatives) colnames(alternatives) <- "Alternatives" weights_decisor <- as.numeric(df[2, 2:ncol(df)]) criteria_direction <- df[1, 2:ncol(df)] criterias <- colnames(df)[2:ncol(df)] matrix_decis <- as.data.frame(df[3:nrow(df), 2:ncol(df)]) colnames(matrix_decis) <- criterias matrix_decis <- matrix_decis %>% mutate_all(as.numeric) if (weight_method == "decisor" & abs(sum(weights_decisor) - 1) > 1E-10) { stop("The sum of the weights must be equal to 1!") } if (any(!sapply(matrix_decis, is.numeric))) { stop("All values in the Decision Matrix must be numeric!") } if (any(is.na(alternatives))) { stop("All alternatives must be named!") } if (any(is.na(criterias))) { stop("All criteria must be named!") } if (any(!criteria_direction %in% c("MAX", "MIN"))) { stop("All criteria must be either MAXIMIZATION or MINIMIZATION!") } if (!weight_method %in% c("psi", "mpsi", "topsis", "decisor")) { stop("Choose a valid elicitation method for the weights!") } if (!ranking_method %in% c("psi", "topsis", "cocoso")) { stop("Choose a valid ranking method!") } # IF ELSE for the weights **************************************************** if(weight_method == "psi") { vector_weights <- weights_psi(df) } else if(weight_method == "mpsi") { vector_weights <- weights_mpsi(df) } else { vector_weights <- as.numeric(df[2, 2:ncol(df)]) } # IF ELSE for the ranking method ********************************************* if(ranking_method == "psi") { ranked_alternatives <- rank_psi(df, vector_weights) } else if(ranking_method == "topsis") { ranked_alternatives <- rank_topsis(df, vector_weights) } else { ranked_alternatives <- rank_cocoso(df, vector_weights, alpha) } return(ranked_alternatives) } # ranking <- pmtcR(df4,ranking_method = "cocoso") # View(ranking)