replace_code: Replace Code

Description Usage Arguments

View source: R/replace_code.R

Description

Replace code in .R scripts. Be sure to use version control as wrong patterns might result in unintended replacements. Note that you should specify the pattern by stingr::regex(pattern, dotall = TRUE) to also match line terminators (all lines of a script are pasted into a single string).

Usage

1
2
replace_code(file, pattern, replacement,
  ending = ".*[.](r|R|s|S|q)([.](lnk|LNK))*$", all = FALSE)

Arguments

file

A path to an .R script or a directory containing .R scripts.

pattern

Pattern to look for.

The default interpretation is a regular expression, as described in stringi::stringi-search-regex. Control options with regex().

Match a fixed string (i.e. by comparing only bytes), using fixed(). This is fast, but approximate. Generally, for matching human text, you'll want coll() which respects character matching rules for the specified locale.

replacement

A character vector of replacements. Should be either length one, or the same length as string or pattern. References of the form \1, \2, etc will be replaced with the contents of the respective matched group (created by ()).

To perform multiple replacements in each element of string, pass a named vector (c(pattern1 = replacement1)) to str_replace_all. Alternatively, pass a function to replacement: it will be called once for each match and its return value will be used to replace the match.

To replace the complete string with NA, use replacement = NA_character_.

all

If TRUE, replacement is done using str_replace_all, if FALSE using str_replace.


DavidBarke/QWUtils documentation built on Jan. 13, 2020, 11:52 a.m.