find_pattern_in: Find string pattern in (text) file

View source: R/find_pattern.R

find_pattern_inR Documentation

Find string pattern in (text) file

Description

goto_pattern_in present from 1.6.0

Usage

find_pattern_in(
  file_contents,
  basedir = ".",
  dir_recursive = TRUE,
  reader = readLines,
  include.comments = FALSE,
  comment.char = NULL,
  use.OS = FALSE,
  file_pattern = "\\.(R|r)(nw|md)?$",
  file_contents_perl = TRUE,
  file_contents_fixed = FALSE,
  file_contents_ignore_case = FALSE,
  file.ext = NULL,
  which_lines = c("first", "all")
)

goto_pattern_in(file_contents, ...)

Arguments

file_contents

A perl-regular expression as a search query.

basedir

The root of the directory tree in which files will be searched recursively.

dir_recursive

(logical, default: TRUE) Search within subdirectories of basedir?

reader

A function, akin to base::readLines, the default, that accepts a filename and returns a character vector.

include.comments

If FALSE, the default, comments (i.e. anything after a \#) are not searched.

comment.char

If include.comments is FALSE, what character marks a comment character? By default, NULL, which sets the correct comment symbol for R and TeX files.

use.OS

Use the operating system to determine file list. Only available on Windows. If it fails, a fall-back option (using dir) is used.

file_pattern

A regular expression passed to list.files(pattern = file.ext). By default, "\.(R|r)(nw|md)?$", i.e. all R and Sweave files. (Does not have to be a file extension.)

file_contents_perl

(logical, default: TRUE) Should file_contents be interpreted as a perl regex?

file_contents_fixed

(logical, default: FALSE) Should file_contents be interpreted as a fixed regex?

file_contents_ignore_case

(logical, default: FALSE) As in grep.

file.ext

A file extension passed to the operating system if use.OS is used.

which_lines

One of "first" and "all". If "first" only the first match in any file is returned in the result; if "all", all matches are.

...

Arguments passed to find_pattern_in.

Details

For convenience, if file_contents appears to be a directory and basedir does not, the arguments are swapped, but with a warning.

Value

A data.table, showing the matches per file.

goto_pattern_in additionally prompts for a row of the returned results. Using the rstudioapi, if available, RStudio will jump to the file and line number.


hutils documentation built on April 13, 2022, 5:23 p.m.