sif: Detect pattern across multiple files

View source: R/sif.R

sifR Documentation

Detect pattern across multiple files

Description

sif() recursively searches all files inside a directory for a term (usually a regex pattern). If used interactively from RStudio it defaults to displaying the results in the Markers pane. This is very similar to RStudios find in files feature, but has the advantage that the search results are also returned as a data.frame that can be further processed.

Usage

sif(
  pattern,
  dir = ".",
  markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE),
  fixed = FALSE,
  case_sensitive = TRUE,
  path_pattern = getOption("sifr.path_pattern"),
  path_case_sensitive = FALSE,
  recursive = TRUE,
  encoding = "unknown"
)

sifkw(
  keywords,
  dir = ".",
  markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE),
  fixed = FALSE,
  case_sensitive = FALSE,
  path_pattern = getOption("sifr.path_pattern"),
  path_case_sensitive = FALSE,
  recursive = TRUE
)

todos(
  dir = ".",
  markers = interactive() && requireNamespace("rstudioapi", quietly = TRUE),
  fixed = FALSE,
  case_sensitive = TRUE,
  path_pattern = getOption("sifr.path_pattern"),
  path_case_sensitive = FALSE,
  recursive = TRUE,
  encoding = "unknown"
)

Arguments

pattern

character scalar. A pattern for which to search in files.

dir

character scalar. A file system path to the directory in which files should be search.

markers

logical scalar. Display results in the RStudio source markers pane.

fixed

logical scalar. If FALSE pattern is interpreted as regular expression, else the string is matched as-is.

case_sensitive

logical scalar. If TRUE pattern is matched case sensitively.

path_pattern

character scalar. A regular expression pattern to match file paths against. Defaults to common source files for R (.R, .Rmd, Rnw, Rhtml, c, cpp). Please note that .Rd files are not included by default. You can modify the default behavior by setting the option() sifr.path_pattern.

path_case_sensitive

logical. If TRUE path_pattern is matched case sensitively.

recursive

logical scalar. If TRUE files are searched recursively starting from dir.

encoding

passed on to base::readLines()

keywords

a character vector of keywords

Value

A sif_result or sifkw_results data.table with the columns:

  • path - path to the file,

  • ln - line number,

  • pos - a list column of numeric two-column matrices indicating start and end of each match, see stringi::stri_locate_all()

  • contents - the text contents of the line

The returned object may also contain the attributes "pattern" (for sif()) or "keywords" (for sifkw()) that contain the original search terms

See Also

https://stackoverflow.com/a/7284583/2622641

Examples

 tf <- tempfile(fileext = ".csv")
 write.csv(iris, tf)

 x <- sif("5.5", dir = dirname(tf), markers = FALSE, path_pattern = ".*\\.csv$")

 print(x)
 as.data.frame(x)
 attr(x, "pattern")

 if (interactive() && requireNamespace("rstudioapi", quietly = TRUE)){
   print(x, markers = TRUE)
 }
 unlink(tf)  #cleanup

s-fleck/sifr documentation built on Oct. 15, 2023, 5:04 a.m.