Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the reporting functions #171

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions R/reporting-fun-files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' 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) {
# 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
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 { #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")
message("Here are the file(s) that could be optimized along with the required optimizers:")
cat("\n")
print(final_result)
}
}
31 changes: 31 additions & 0 deletions R/reporting-fun-folder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' 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) {
#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)
}
16 changes: 16 additions & 0 deletions R/reporting-fun-text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' 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) {
#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)
}