R/string_search.R

Defines functions string_search

Documented in string_search

#' Scan all folders and files to find a string: string_search
#'
#' @param path If NULL, the current directory is used
#' @param pattern A regular expression pattern such as '\\.R$'
#' @param string A string such as 'metric'
#'
#' @return A character vector with the names of the files that contain the string
#' @export
#'
#' @examples
#' string_search(path=".","\\.R$","metric")
#'
#' # function string_search
string_search <- function(path = ".", pattern, string) {
  # List all files matching the pattern in the specified path
  fileNames <- list.files(path = path,
                          pattern = pattern,
                          recursive = TRUE,
                          full.names = TRUE)

  # Function to check if a file contains the specific string
  contains_string <- function(file) {
    lines <- readLines(file, warn = FALSE)
    any(grepl(string, lines, ignore.case = TRUE))
  }

  # Check for errors in sapply call
  result <- tryCatch({
    files_with_string <- fileNames[sapply(fileNames, contains_string)]
    return(files_with_string)
  }, error = function(e) {
    stop("An error occurred: Please provide corrected path.")
  })

  return(result)
}

Try the hmsidwR package in your browser

Any scripts or data that you put into this service are public.

hmsidwR documentation built on April 12, 2025, 2:28 a.m.