Skip to contents

Find and replace in files across a directory

Usage

findAndReplace(
  pattern,
  replacement,
  filePattern = "\\.(r|R)$",
  dir = getwd(),
  recursive = FALSE
)

Arguments

pattern

character(1). Pattern string, supporting regular expressions.

replacement

character(1). Replacement string.

filePattern

character(1). File pattern matching string. Defaults to matching against R files.

dir

character(1). Directory path.

recursive

logical(1). Search recursively?

Value

Invisibly return file paths.

Note

Updated 2022-05-31.

Requires BiocParallel package to be installed.

Examples

unlink("testdata", recursive = TRUE)
dir.create(file.path("testdata", "subdir"), recursive = TRUE)
writeLines(
    text = "print(\"foo\")",
    con = file.path("testdata", "aaa.R"),
)
writeLines(
    text = "print(\"foo\")",
    con = file.path("testdata", "subdir", "bbb.R"),
)
out <- findAndReplace(
    pattern = "foo",
    replacement = "bar",
    dir = "testdata",
    recursive = TRUE
)
print(out)
#> [1] "/Users/mike/git/monorepo/r-packages/r-aciddevtools/docs/reference/testdata/aaa.R"       
#> [2] "/Users/mike/git/monorepo/r-packages/r-aciddevtools/docs/reference/testdata/subdir/bbb.R"
print(readLines(out[[1L]]))
#> [1] "print(\"bar\")"
unlink("testdata", recursive = TRUE)