Replace string pattern in text file

replace_pattern_in(file_contents, replace, basedir = ".",
  dir_recursive = TRUE, reader = readLines,
  file_pattern = "\\.(R|r)(nw|md)?$", file_contents_perl = TRUE,
  file_contents_fixed = FALSE, file_contents_ignore_case = FALSE,
  writer = writeLines)

Arguments

file_contents

Character string containing a regular expression to be matched in the given character vector. Passed to pattern in gsub.

replace

The replacement, passed to replacement in gsub.

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.

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.

writer

A function that will rewrite the file from the character vector read in.