From 69aac6aaef35cb18c3c14d65fe53eac0a2f0262d Mon Sep 17 00:00:00 2001 From: Rahul Saxena Date: Mon, 31 Aug 2020 03:53:25 +0530 Subject: [PATCH 1/2] Added the reporting functions --- R/reporting-fun-files.R | 47 ++++++++++++++++++++++++++++++++++++++++ R/reporting-fun-folder.R | 27 +++++++++++++++++++++++ R/reporting-fun-text.R | 14 ++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 R/reporting-fun-files.R create mode 100644 R/reporting-fun-folder.R create mode 100644 R/reporting-fun-text.R diff --git a/R/reporting-fun-files.R b/R/reporting-fun-files.R new file mode 100644 index 0000000..3de804d --- /dev/null +++ b/R/reporting-fun-files.R @@ -0,0 +1,47 @@ +#' Report possible optimizations in `.R` files. +#' +#' @param files A character vector with paths to files to optimize. +#' @param optimizers A named list of optimizer functions. +#' +#' @export +#' +generate_files_opt_report <- function(files, optimizers = rco:::all_optimizers) { + message("Sit back, this may take some time...") + #Initializations + final_result <- codes <- list() + # Read the contents of a file. 3 files will form a list/vector/df of size 3 + codes <- lapply(files, rco:::read_code_file) + # Naming each entry in the list + names(codes) <- files + pb_outer <- utils::txtProgressBar(1, length(codes)*length(optimizers), style=3) + pb_itr <- 0 + # Probing the contents of each file: + for (i in seq_len(length(codes))) { + og_code <- codes[[i]] + og_code_no_ws <- gsub(" ", "", og_code) + opt_used_in_i <- vector(mode = "character") + for(j in seq_along(optimizers)) { + pb_itr <- pb_itr + 1 + utils::setTxtProgressBar(pb_outer, pb_itr) + act_optim <- optimizers[[j]] + act_optim_name <- names(optimizers)[[j]] + opt_j_code <- act_optim(og_code) + opt_j_code_no_ws <- gsub(" ", "", opt_j_code[[1]]) + if(!isTRUE(all.equal(og_code_no_ws, opt_j_code_no_ws))) + opt_used_in_i <- append(opt_used_in_i, act_optim_name) + } + code_file_name <- names(codes)[i] + final_result[[code_file_name]] <- opt_used_in_i + } + final_result <- final_result[lapply(final_result, length) > 0] + if(length(final_result) == 0) { + message("No more optimizations are required in your script(s)") + } else { + cat("\n") + message(sprintf("`rco` found %d files for optimization", as.integer(length(final_result)))) + message("Use the `rco::rco_gui()` function for detailed comparsion") + message("Here are the file(s) that could be optimized along with the required optimizers:") + cat("\n") + print(final_result) + } +} diff --git a/R/reporting-fun-folder.R b/R/reporting-fun-folder.R new file mode 100644 index 0000000..0481a6f --- /dev/null +++ b/R/reporting-fun-folder.R @@ -0,0 +1,27 @@ +#' Report possible optimizations in folder containing `.R` files. +#' +#' @param folder Path to a directory with files to optimize. +#' @param optimizers A named list of optimizer functions. +#' @param pattern An optional regular expression. Only file names which match +#' the regular expression will be optimized. +#' @param recursive A logical value indicating whether or not files +#' in subdirectories of `folder` should be optimized as well. +#' +#' @export +#' +generate_folder_opt_report <- function(folder, optimizers = all_optimizers, + pattern = "\\.R$", recursive = TRUE) { + if (!dir.exists(folder)) { + stop("The specified folder does not exist.") + } + to_opt_files <- list.files( + path = folder, pattern = pattern, full.names = TRUE, recursive = recursive + ) + if (length(to_opt_files) == 0) { + return() + } + message("Files to check for optimization:") + message(paste(to_opt_files, collapse = "\n")) + generate_files_opt_report(files = to_opt_files, + optimizers = optimizers) +} \ No newline at end of file diff --git a/R/reporting-fun-text.R b/R/reporting-fun-text.R new file mode 100644 index 0000000..d78a091 --- /dev/null +++ b/R/reporting-fun-text.R @@ -0,0 +1,14 @@ +#' Report possible optimizations in `.R` code snippet. +#' +#' @param text A character vector with the code to optimize. +#' @param optimizers A named list of optimizer functions. +#' +#' @export +#' +generate_text_opt_report <- function(text, optimizers = all_optimizers) { + input_code_snippet <- tempfile() + rco:::write_code_file(text, input_code_snippet) + message("Ignore the name that will be assigned to your text/code snippet") + cat("\n") + generate_files_opt_report(input_code_snippet, optimizers) +} From 55c61a90a9561a8a666e3799aac02019a3a24cd8 Mon Sep 17 00:00:00 2001 From: Rahul Saxena Date: Mon, 31 Aug 2020 04:04:52 +0530 Subject: [PATCH 2/2] Added comments --- R/reporting-fun-files.R | 19 +++++++++++++++++-- R/reporting-fun-folder.R | 4 ++++ R/reporting-fun-text.R | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/R/reporting-fun-files.R b/R/reporting-fun-files.R index 3de804d..9302680 100644 --- a/R/reporting-fun-files.R +++ b/R/reporting-fun-files.R @@ -6,37 +6,52 @@ #' @export #' generate_files_opt_report <- function(files, optimizers = rco:::all_optimizers) { + # An introductory message, as the function could take quite some time depending on the number and size of files message("Sit back, this may take some time...") - #Initializations + # Initializations final_result <- codes <- list() # Read the contents of a file. 3 files will form a list/vector/df of size 3 codes <- lapply(files, rco:::read_code_file) # Naming each entry in the list names(codes) <- files + # Setting up the progress bar pb_outer <- utils::txtProgressBar(1, length(codes)*length(optimizers), style=3) pb_itr <- 0 # Probing the contents of each file: for (i in seq_len(length(codes))) { + # Original code from the file og_code <- codes[[i]] + # Remove whitespaces for comparsion purposes og_code_no_ws <- gsub(" ", "", og_code) + # Initialize an empty vector opt_used_in_i <- vector(mode = "character") + # Running each file through each of the optimizers specified for(j in seq_along(optimizers)) { + #Incrementing the progress bar pb_itr <- pb_itr + 1 utils::setTxtProgressBar(pb_outer, pb_itr) + # act_optim is the current optimizer act_optim <- optimizers[[j]] + # act_optim_name is the name of the current optimizer act_optim_name <- names(optimizers)[[j]] + # Optimized code opt_j_code <- act_optim(og_code) + # Remove whitespaces for comparsion purposes opt_j_code_no_ws <- gsub(" ", "", opt_j_code[[1]]) + # If code was optimized, then append the optimizer name to the file if(!isTRUE(all.equal(og_code_no_ws, opt_j_code_no_ws))) opt_used_in_i <- append(opt_used_in_i, act_optim_name) } + # Setting the name for the files in the resultant list code_file_name <- names(codes)[i] final_result[[code_file_name]] <- opt_used_in_i } + # Removing all entries with `character(0)` or no optimizations final_result <- final_result[lapply(final_result, length) > 0] + # If no optimization required if(length(final_result) == 0) { message("No more optimizations are required in your script(s)") - } else { + } else { #If optimization is required cat("\n") message(sprintf("`rco` found %d files for optimization", as.integer(length(final_result)))) message("Use the `rco::rco_gui()` function for detailed comparsion") diff --git a/R/reporting-fun-folder.R b/R/reporting-fun-folder.R index 0481a6f..1c08e9b 100644 --- a/R/reporting-fun-folder.R +++ b/R/reporting-fun-folder.R @@ -11,17 +11,21 @@ #' generate_folder_opt_report <- function(folder, optimizers = all_optimizers, pattern = "\\.R$", recursive = TRUE) { + #Check if given folder exists or not if (!dir.exists(folder)) { stop("The specified folder does not exist.") } + #List out the files from the specified folder to_opt_files <- list.files( path = folder, pattern = pattern, full.names = TRUE, recursive = recursive ) + #Ensure folder is non-empty if (length(to_opt_files) == 0) { return() } message("Files to check for optimization:") message(paste(to_opt_files, collapse = "\n")) + #Run the files in the `generate_files_opt_report` function generate_files_opt_report(files = to_opt_files, optimizers = optimizers) } \ No newline at end of file diff --git a/R/reporting-fun-text.R b/R/reporting-fun-text.R index d78a091..731209d 100644 --- a/R/reporting-fun-text.R +++ b/R/reporting-fun-text.R @@ -6,9 +6,11 @@ #' @export #' generate_text_opt_report <- function(text, optimizers = all_optimizers) { + #Convert the given text into a temporary file input_code_snippet <- tempfile() rco:::write_code_file(text, input_code_snippet) message("Ignore the name that will be assigned to your text/code snippet") cat("\n") + #Run the temporary file in the `generate_files_opt_report` function generate_files_opt_report(input_code_snippet, optimizers) }