#' find_pattern
#' @description return how many times a specific pattern occurs in any file of
#' the current directory and sub directories.
#' @param fun string or name with the pattern to search
#' @param which_files a character vector with file extensions to check.
#' @param dir starting directory from which an iterative search
#' is performed
#' @import cli
#' @export
#'
find_pattern <- function(pattern, which_files = c("R", "Rmd"), dir = "."){
which_files <- paste0(".", which_files, "$", collapse = "|")
# Getting files
files <- list.files(dir, which_files, full.names = TRUE, recursive = TRUE)
files_content <- lapply(files, function(x) readLines(x, warn = FALSE))
names(files_content) <- files
# Counting times
pattern_times <- sapply(files_content, function(x) sum(grepl(pattern, x)))
files_content <- files_content[pattern_times > 0]
pattern_times <- pattern_times[pattern_times > 0]
# Formatting
if(length(pattern_times) > 0){
out <- paste(names(files_content), "---", pattern_times, "times")
out_cli <- ifelse(grepl("R/", out), cli::col_blue(out), out) # Different for R/ files
msg <- lapply(out_cli, cli::cli_alert_success)
}else{
cli::cli_alert_info(paste("No files with", cli::col_green(pattern)))
}
}
#' success
#' @description fancy success message
#' @param msg string with the message
#' @import cli
success <- function(msg){
cli::cli_alert_success(msg)
}
#' pb
#' @description Display a progress bar to use within a \code{for} loop
#' @param niter Integer that indicate the total number of iterations
#' @param index Integer that indicate the current iteration
#'
#' @export
#' @import cli
#'
pb <- function(niter, index){
step <- niter/10
if(i %% step == 0){
pr <- paste0(rep("----", index/step), collapse = "")
cat("\r", paste0(index/step * 10, "% "), pr, sep = "")
utils::flush.console()
if(index/step == 10){
cat(" ")
cli::cli_alert_success("")
}
}
}
#' conditional
#' @description Function to create a wrapper version of the input function
#' that run according to a logical condition. Useful for conditional pipelines.
#' see https://community.rstudio.com/t/conditional-pipelines/6076/2
#' @param fun function
#'
#' @export
#'
conditional <- function(fun){
function(..., execute) {
if (execute) fun(...) else ..1
}
}
#' session_info
#' @description Return a simplified version of the base R \code{utils::sessionInfo()} function.
#' @return a dataframe with a subset of information
#' @export
#'
session_info <- function(){
session <- sessionInfo()
data.frame(
Info = c("R version", "Platform", "OS"),
Value = c(
session$R.version$version.string,
session$platform,
session$running
)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.